-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddasample.cpp
More file actions
125 lines (95 loc) · 3.08 KB
/
ddasample.cpp
File metadata and controls
125 lines (95 loc) · 3.08 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Q: To plot points that make up the line with endpoints (x0,y0) and (xn,yn) using DDA line drawing algorithm.
// Case 1: +ve slope Left to Right line
// Case 2: +ve slope Right to Left line
// Case 3: -ve slope Left to Right line
// Case 4: -ve slope Right to Left line
// Each case has two subdivisions
// (i) |m|<= 1
// (ii) |m|>1
// Note that all four cases of line drawing must be given as test cases.
#include<bits/stdc++.h>
#include<GL/glut.h>
using namespace std;
using ld = long double;
const int WINDOW_WIDTH = 850;
const int WINDOW_HEIGHT = 700;
void myInit();
void myDisplay();
void printLines();
void printDDALine(ld x1, ld y1, ld x2, ld y2);
const ld PADDING = 250;
const ld STEP = 1;
const ld SCALE = 5;
int main(int argc,char* argv[]) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
glutCreateWindow("DDA - Line Drawing Algorithm");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 1;
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT);
printLines();
glFlush();
}
void printLines() {
glBegin(GL_POINTS);
// Case 1: +ve slope Left to Right line
glColor3f(1.0f,0.0f,0.0f);
// | m | > 1
printDDALine((ld)3,(ld)2, (ld)15,(ld)10);
// | m | < 1
printDDALine((ld)2,(ld)3, (ld)10,(ld)15);
// Case 2: +ve slope Right to Left line
glColor3f(0.5f,0.5f,0.0f);
// | m | > 1
printDDALine((ld)-3,(ld)-2, (ld)-15,(ld)-10);
// | m | < 1
printDDALine((ld)-2,(ld)-3, (ld)-10,(ld)-15);
//Case 3: -ve slope Left to Right line
glColor3f(0.0f,1.0f,0.0f);
// | m | > 1
printDDALine((ld)3,(ld)-2, (ld)15,(ld)-10);
// | m | < 1
printDDALine((ld)2,(ld)-3, (ld)10,(ld)-15);
//Case 4: -ve slope Right to Left line
glColor3f(0.0f,0.5f,0.5f);
// | m | > 1
printDDALine((ld)-3,(ld)2, (ld)-15,(ld)10);
// | m | < 1
printDDALine((ld)-2,(ld)3, (ld)-10,(ld)15);
glEnd();
}
void printDDALine(ld x1, ld y1, ld x2, ld y2) {
ld pad = PADDING, scale = SCALE;
x1 = x1*scale + pad;
x2 = x2*scale + pad;
y1 = y1*scale + pad;
y2 = y2*scale + pad;
ld dx, dy, steps;
ld xInc, yInc, x, y;
dx = (x2-x1);
dy = (y2-y1);
if(abs(dx) > abs(dy)) steps = abs(dx);
else steps = abs(dy);
xInc = dx/steps;
yInc = dy/steps;
x = x1; y = y1;
glVertex2d(x, y);
for(long i=1;i<=steps;i++) {
x += xInc;
y += yInc;
glVertex2d(x, y);
}
}