static void plot_segment_gray(int *img, int w, int h, float a[2], float b[2], int c) { int p[2] = {round(a[0]), round(a[1])}; int q[2] = {round(b[0]), round(b[1])}; struct plot_data d = { img, w, h, c }; traverse_segment(p[0], p[1], q[0], q[1], plot_pixel, &d); }
// generic function to traverse a segment between two pixels void traverse_segment(int px, int py, int qx, int qy, void (*f)(int,int,void*), void *e) { if (px == qx && py == qy) f(px, py, e); else if (qx + qy < px + py) // bad quadrants traverse_segment(qx, qy, px, py, f, e); else { if (qx - px > qy - py || px - qx > qy - py) { // horizontal float slope = (qy - py)/(float)(qx - px); for (int i = 0; i < qx-px; i++) f(i+px, lrint(py + i*slope), e); } else { // vertical float slope = (qx - px)/(float)(qy - py); for (int j = 0; j <= qy-py; j++) f(lrint(px + j*slope), j+py, e); } } }
// draw a segment between two points void traverse_segment(int px, int py, int qx, int qy, void (*f)(int,int,void*), void *e) { if (px == qx && py == qy) f(px, py, e); else if (qx + qy < px + py) // bad quadrants traverse_segment(qx, qy, px, py, f, e); else { if (abs(qx - px) > qy - py) { // horitzontal float slope = (qy - py); slope /= (qx - px); assert(px < qx); assert(fabs(slope) <= 1); for (int i = 0; i < qx-px; i++) f(i+px, lrint(py + i*slope), e); } else { // vertical float slope = (qx - px); slope /= (qy - py); assert(abs(qy - py) >= abs(qx - px)); assert(py < qy); assert(fabs(slope) <= 1); for (int j = 0; j <= qy-py; j++) f(lrint(px+j*slope), j+py, e); } } }
// function to draw a red segment static void plot_segment_red(struct image_rgba *f, double x0, double y0, double xf, double yf) { traverse_segment(x0, y0, xf, yf, plot_pixel_red, f); }
// function to draw a green segment static void plot_segment_green(struct FTR *f, double x0, double y0, double xf, double yf) { traverse_segment(x0, y0, xf, yf, plot_pixel_green, f); }