-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDDA Line Drawing Algorithm.cpp
88 lines (77 loc) · 1.68 KB
/
DDA Line Drawing Algorithm.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//int centerX, centerY, radius;
int X_1, Y_1, X_2, Y_2, midX, midY;
int visited[10000][10000];
int xx[] = {0,0,1,-1};
int yy[] = {1,-1,0,0};
void putpixel(int x, int y)
{
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}
void DDAAlgorithm()
{
int dx, dy, steps;
float xinc, yinc, X, Y;
dx = X_2 - X_1;
dy = Y_2 - Y_1;
steps = max(abs(dx), abs(dy));
xinc = dx/steps;
yinc = dy/steps;
X = X_1;
Y = Y_1;
for(int i=0; i<=steps; i++)
{
putpixel(floor(X), floor(Y));
X+= xinc;
Y+= yinc;
}
}
void drawAxis()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (int i=-500; i<=500; i++)
{
glVertex2d(i,0);
glVertex2d(0,i);
}
glEnd();
glFlush();
}
void display(){
glBegin(GL_POINTS);
glPointSize(1.0);
glColor3f(1.0, 0.0, 0.0);
drawAxis();
DDAAlgorithm();
glFlush();
glEnd();
}
int main(int argc, char **argv)
{
cout<<"Enter the value of X1, Y1, X2 and Y2:"<<endl;
cin>>X_1>>Y_1>>X_2>>Y_2;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(450,100);
glutCreateWindow("DDA Line Drawing Algorithm");
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,500,-100,500);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}