0% found this document useful (0 votes)
89 views

Program 1: Implement Brenham's Line Drawing Algorithm For All Types of Slope

The document describes an implementation of Bresenham's line drawing algorithm in C++ for drawing lines of all slopes between two points. It includes functions to initialize the display, draw a pixel, draw a line between two points using the Bresenham algorithm, and display the line. The main function gets the x and y coordinates of the two endpoints from the user, initializes the GLUT library, and calls the display functions to draw the line.

Uploaded by

VISHAL KOUL
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)
89 views

Program 1: Implement Brenham's Line Drawing Algorithm For All Types of Slope

The document describes an implementation of Bresenham's line drawing algorithm in C++ for drawing lines of all slopes between two points. It includes functions to initialize the display, draw a pixel, draw a line between two points using the Bresenham algorithm, and display the line. The main function gets the x and y coordinates of the two endpoints from the user, initializes the GLUT library, and calls the display functions to draw the line.

Uploaded by

VISHAL KOUL
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
You are on page 1/ 4

Program 1: Implement Brenham’s line drawing algorithm for all

types Of slope

PROGRAM

# include<GL/glut.h>
# include <stdio.h>
int x1, y1, x2, y2;
void myInit()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}

void draw_pixel(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void draw_line(int x1, int x2, int y1, int y2)


{
int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x,y;

dx = x2-x1;
dy = y2-y1;
if (dx < 0) dx = -dx;
if (dy< 0) dy = -dy;
incx = 1;
if (x2 < x1)
incx = -1;
incy = 1;
if (y2 < y1) incy = -1;
x = x1; y = y1;
if (dx >dy) {
draw_pixel(x, y);
e = 2 * dy-dx;
inc1 = 2*(dy-dx);
inc2 = 2*dy;
for (i=0; i<dx; i++) {
if (e >= 0) {
y += incy;
e += inc1;
}
else
e += inc2;
x += incx;
draw_pixel(x, y);
}

} else {
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}

void myDisplay() {
draw_line(x1, x2, y1, y2);
glFlush();
}

void main(int argc, char **argv) {

printf( "Enter (x1, y1, x2, y2)\n");


scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy