/ Published in: C
This is an implementation of the Bresenham's Line Algorithm in Computer Graphics Design in the C programming language.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Shubham Mehta, Write a program to implement Bresenham's Line Algorithm. #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd, gm; float x1, x2, y1, y2, dx, dy, p, xk, yk, slope; clrscr(); gd=DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); putpixel(x1, y1, RED); slope=(y2-y1)/(x2-x1); dx=x2-x1; dy=y2-y1; xk=x1; yk=y1; if(slope<1) { p=(2*dy)-dx; for( ; x1<=x2; x1++) { if(p<0) { putpixel(++xk, yk, RED); p+=(2*dy); } else { putpixel(++xk, ++yk, RED); p=p+(2*dy)-(2*dx); } } } else { p=(2*dx)-dy; for( ; y1<=y2; y1++) { if(p<0) { putpixel(xk, ++yk, RED); p+=(2*dx); } else { putpixel(++xk, ++yk, RED); p=p+(2*dx)-(2*dy); } } } closegraph(); }