/*Bresenham line algorithm: draw an line from (x1, y1) to (x2, y2) */
static void line(u_int *pic_buf, int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int inc;
int pi;
inc = ((dx * dy) >= 0) ? 1 : -1;
if(abs(dx) > abs(dy)){
if(dx < 0){
swap(&x1, &x2);
swap(&y1, &y2);
dx = -dx;
dy = -dy;
}
dy = (dy > 0) ? dy : -dy;
pi = 2 * dy - dx;
while(x1++ <= x2){
draw_pixel(pic_buf, y1, x1 - 1, y1, x1);
if(pi < 0)
pi += 2 * dy;
else{
y1 += inc;
pi += 2 * (dy - dx);
}
}
}else{
if(dy < 0){
swap(&x1, &x2);
swap(&y1, &y2);
dx = -dx;
dy = -dy;
}
dx = (dx > 0)? dx : -dx;
pi = 2 * dx - dy;
while(y1++ < y2){
draw_pixel(pic_buf, y1 - 1, x1, y1 - 1, x1);
if(pi < 0)
pi += 2 * dx;
else{
x1 += inc;
pi += 2 *(dx - dy);
}
}
}
}
Bresenham_line_algorithm.c
最新推荐文章于 2021-07-16 15:40:40 发布