0% found this document useful (0 votes)
3 views2 pages

Bresenham's Line Drawing Code

Uploaded by

dhirajs1098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Bresenham's Line Drawing Code

Uploaded by

dhirajs1098
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <graphics.

h>

#include <conio.h>

#include <stdio.h>

#include <math.h>

// Bresenham's Line Drawing Algorithm

void bresenhamLine(int x1, int y1, int x2, int y2) {

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int p = 2 * dy - dx;

int x, y;

if (x1 > x2) {

x = x2;

y = y2;

x2 = x1;

} else {

x = x1;

y = y1;

putpixel(x, y, WHITE);

while (x < x2) {

x++;

if (p < 0) {

p += 2 * dy;

} else {

y++;

p += 2 * dy - 2 * dx;

}
putpixel(x, y, WHITE);

int main() {

int gd = DETECT, gm;

int x1, y1, x2, y2;

initgraph(&gd, &gm, (char*)"");

printf("Enter coordinates (x1,y1,x2,y2): ");

scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

bresenhamLine(x1, y1, x2, y2);

getch();

closegraph();

return 0;

You might also like