Bresenham's Line Drawing Algorithm -
i made code. code handles case when dx , dy greater 0. do, when 1 of them less 0. algorithm says m should taken absolute , x , y should decremented. how decrement i(x) in second loop?
#include <stdio.h> #include <math.h> #include <conio.h> #include <graphics.h> void illuminate (int x, int y, int col) { putpixel(x+320,240-y,col); } void main (void) { int i,x1,y1,x2,y2,j; float dx,dy,m,e; int gdriver=detect,gmode; initgraph(&gdriver,&gmode, "c:\\turboc3\\bgi"); printf("\nenter coordinates of initial point\n"); scanf("%d%d",&x1,&y1); printf("\nenter coordinates of final point\n"); scanf("%d%d",&x2,&y2); dx = x2 - x1; dy = y2 - y1; m = abs(dy)/abs(dx); j = y1; e = m - 1; line(320,0,320,480); line(0,240,640,240); if ( dx >= 0 && dy >= 0 ) { (i=x1;i<x2-1;i++) { illuminate(i,j,4); if ( e>= 0 ) { j = j+1; e = e-1.0; } e = e+m; } } else { (i=x1;i<x2-1;i++) { illuminate(i,j,4); if (e>=0) { j = j-1; e = e-1.0; } e = e+m; } } getch(); }
one easy way handle case "draw" mirror image of line respect x axis in container , mirror again. don't have way of thinking may figure our should put negation. code in case when dx
positive or negative similar , differs @ few signs.
Comments
Post a Comment