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

DDA LINE Algorithm

This C++ program uses OpenGL to implement the Digital Differential Analyzer (DDA) algorithm for drawing a line between two specified points. It prompts the user to input the coordinates of the line's endpoints, calculates the necessary increments, and renders the line on a graphical window. The program initializes the OpenGL context and enters the main loop to display the drawn line.

Uploaded by

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

DDA LINE Algorithm

This C++ program uses OpenGL to implement the Digital Differential Analyzer (DDA) algorithm for drawing a line between two specified points. It prompts the user to input the coordinates of the line's endpoints, calculates the necessary increments, and renders the line on a graphical window. The program initializes the OpenGL context and enters the main loop to display the drawn line.

Uploaded by

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

#include <iostream>

#include <cmath>
#include <GL/glut.h>

using namespace std;

double X1, Y1, X2, Y2;

float round_value(float v) {
return floor(v + 0.5);
}

void LineDDA() {
double dx = (X2 - X1);
double dy = (Y2 - Y1);
double steps;
float xInc, yInc, x = X1, y = Y1;

steps = (abs(dx) > abs(dy)) ? abs(dx) : abs(dy);


xInc = dx / (float)steps;
yInc = dy / (float)steps;

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2d(x, y);

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


x += xInc;
y += yInc;
glVertex2d(round_value(x), round_value(y));
}

glEnd();
glFlush();
}

void Init() {
glClearColor(1.0, 1.0, 1.0, 0);
glColor3f(0.0, 0.0, 0.0);
gluOrtho2D(0, 640, 0, 480);
}

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


cout << "Enter two end points of the line to be drawn:\n";
cout << "Enter Point 1 (X1, Y1): ";
cin >> X1 >> Y1;
cout << "Enter Point 2 (X2, Y2): ";
cin >> X2 >> Y2;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("DDA Line Drawing - C++ OpenGL");

Init();
glutDisplayFunc(LineDDA);
glutMainLoop();
return 0;
}

You might also like