📄 bresenham.c
字号:
#include <stdint.h>//#include <stdlib.h>#include "bresenham.h"// Bresenham Line Algorithm// used by myLinevoid setPixel(uint32_t* restrict surface, uint32_t x, uint32_t y, uint32_t stride) { // PLOT x,y point on surface surface[ ( y * stride ) + x ] = 0x00;}// Bresenham Line Algorithmvoid drawLine(uint32_t* restrict surface, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t stride) { /* int32_t x = 0; int32_t y = 0; int32_t error = 0; int32_t delta = 0; int32_t step = 0; int32_t dx = 0; int32_t dy = 0; int32_t incx = 0; int32_t incy = 0; x = x1; y = y1; dy = y2 - y1; dx = x2 - x1; if(dx > 0) incx = 1; else incx = -1; if(dy > 0) incy = 1; else incy = -1; if(abs(dy) < abs(dx)){ error = -abs(dx); delta = 2*abs(dy); step = 2*error; while( x != 2 ){ setPixel(surface, x, y, stride); x+= incx; error = error + delta; if(error > 0) { y += incy; error += step; } } } else { error = -abs(dy); delta = 2*abs(dx); step = 2*error; while( y != y2){ setPixel(surface, x, y, stride); y += incy; error = error + delta; if (error > 0){ x += incx; error +=step; } } } setPixel(surface, x2, y2, stride);*/ int x, y; int dx, dy; int incx, incy; int balance; if (x2 >= x1) { dx = x2 - x1; incx = 1; } else { dx = x1 - x2; incx = -1; } if (y2 >= y1) { dy = y2 - y1; incy = 1; } else { dy = y1 - y2; incy = -1; } x = x1; y = y1; if (dx >= dy) { dy <<= 1; balance = dy - dx; dx <<= 1; while (x != x2) { setPixel(surface,x, y, stride); if (balance >= 0) { y += incy; balance -= dx; } balance += dy; x += incx; } setPixel(surface,x, y, stride); } else { dx <<= 1; balance = dx - dy; dy <<= 1; while (y != y2) { setPixel(surface,x, y, stride); if (balance >= 0) { x += incx; balance -= dy; } balance += dx; y += incy; } setPixel(surface,x, y, stride); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -