-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7. DDA_algo.cpp
58 lines (54 loc) · 1.17 KB
/
7. DDA_algo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "./freeglut-3.2.1/include/GL/freeglut.h"
#include<iostream>
#include<math.h>
using namespace std;
GLdouble x1, yfirst, x2, y2;
void dda()
{
GLdouble dx, dy, steps, xinc, yinc, x, y;
dx = x2-x1;
dy = y2-yfirst;
x = x1;
y = yfirst;
steps = (abs(dx)>abs(dy))?abs(dx):abs(dy);
xinc = dx/steps;
yinc = dy/steps;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2d(x, y);
for(int k = 0; k<steps; k++)
{
x = x+xinc;
y = y+yinc;
glVertex2d(x, y);
}
glEnd();
glFlush();
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0, 0.0, 0.0);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
int main(int argc, char **argv)
{
cout<<"Enter two points for line drawing...\n";
cout<<"Enter the starting co-ordinate(x1, yfirst): ";
cin>>x1>>yfirst;
cout<<"Enter the end co-ordinate(x2, y2): ";
cin>>x2>>y2;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(640, 480);
glutCreateWindow("Line using DDA");
init();
glutDisplayFunc(dda);
glutMainLoop();
return 0;
}