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

DDA Line Drawing Algorithm Using OpenGL

The document describes the Digital Differential Analyzer (DDA) line drawing algorithm. It includes the C code implementation of the algorithm. The algorithm takes in four points as input - the starting and ending points of the line segment. It then calculates the increment along the x and y axes for each pixel. It plots the line segment by incrementing x and y by the calculated increments in a loop from the starting to ending point. The code initializes the window, calls the display function containing the DDA algorithm on each refresh, and runs the main GLUT loop.

Uploaded by

kevifev398
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

DDA Line Drawing Algorithm Using OpenGL

The document describes the Digital Differential Analyzer (DDA) line drawing algorithm. It includes the C code implementation of the algorithm. The algorithm takes in four points as input - the starting and ending points of the line segment. It then calculates the increment along the x and y axes for each pixel. It plots the line segment by incrementing x and y by the calculated increments in a loop from the starting to ending point. The code initializes the window, calls the display function containing the DDA algorithm on each refresh, and runs the main GLUT loop.

Uploaded by

kevifev398
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

DDA Line Drawing Algorithm

#include<GL/glut.h>
#include <GL/gl.h>
#include<stdlib.h>
#include<stdio.h>

#define ROUND(x) ((int)(x+0.5))


int xa,xb,ya,yb;
void init(void){

glClearColor (0.0, 0.0, 0.0, 1.0);


glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);

}
void display (void){

//DDA Algorithm starts here


int dx = xb-xa,dy = yb-ya,steps,k;
float xIncrement,yIncrement, x = xa, y = ya;

if(abs(dx)>abs(dy))
steps = abs(dx);
else steps = abs(dy);

xIncrement = dx/(float)steps;
yIncrement = dy/(float)steps;

glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);

glBegin(GL_POINTS);

glVertex2s(ROUND(x),ROUND(y));

for(k=0;k<steps;k++){

x = x+xIncrement;
y = y+yIncrement;
glVertex2s(ROUND(x),ROUND(y));

}
glEnd();

glutSwapBuffers();
}
int main(int argc, char** argv){

printf("Enter the points\n");


scanf("%d %d %d %d",&xa,&ya,&xb,&yb);

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Simple DDA ");
init ();

glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like