0% found this document useful (0 votes)
95 views3 pages

1.BLDA New

The document describes Bresenham's line drawing algorithm for drawing lines on a 2D graph between any two given points. It includes code to initialize the window, get user input for start and end points, implement the line drawing algorithm using incremental decision making to determine the next pixel, and display the resulting line on screen. The algorithm handles lines of any slope by incrementally moving from the start point to the end point, tracking progress along the x and y axes.

Uploaded by

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

1.BLDA New

The document describes Bresenham's line drawing algorithm for drawing lines on a 2D graph between any two given points. It includes code to initialize the window, get user input for start and end points, implement the line drawing algorithm using incremental decision making to determine the next pixel, and display the resulting line on screen. The algorithm handles lines of any slope by incrementally moving from the start point to the end point, tracking progress along the x and y axes.

Uploaded by

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

1.

Implement Bresenham’s line drawing algorithm for all types of


slope.

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<GL/glut.h>
int xstart, ystart, xend, yend;
void init()
{
gluOrtho2D(0, 500, 0, 500);
}
void draw_pixel(int x, int y)
{
glColor3f(1, 0, 0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void LineBres(int xstart, int ystart, int xend, int yend)
{
int dx = abs(xend - xstart);
int dy = abs(yend - ystart);
int twody = 2 * dy, twodyminusdx = 2 * (dy - dx);
int p = 2 * dy - dx;
int x, y;
if (xstart > xend)
{
x = xend;
y = yend;
xend = xstart;
}
else
{
x = xstart;
y = ystart;
}
draw_pixel(x, y);
while (x < xend)
{
x++;
if (p < 0)
p += twody;
else
{
y++;
p += twodyminusdx;
}
draw_pixel(x, y);
}
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
LineBres(xstart, ystart, xend, yend);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
printf("Enter the starting coordinate (x1, y1)\n");
scanf("%d%d", &xstart, &ystart);
printf("Enter the ending coordinate (x2, y2)\n");
scanf("%d%d",&xend, &yend);
glutInit(&argc, argv);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutCreateWindow("Bresenham's Line Drawing");
init();
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Output:

You might also like