-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbresenhamsample.cpp
More file actions
141 lines (108 loc) · 3.45 KB
/
bresenhamsample.cpp
File metadata and controls
141 lines (108 loc) · 3.45 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Q: To plot points that make up the line with endpoints (x0,y0) and (xn,yn) using Bresenham's 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 printBresenhamLine(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("Breseham's 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
printBresenhamLine((ld)3,(ld)2, (ld)15,(ld)10);
// | m | < 1
printBresenhamLine((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
printBresenhamLine((ld)-3,(ld)-2, (ld)-15,(ld)-10);
// | m | < 1
printBresenhamLine((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
printBresenhamLine((ld)3,(ld)-2, (ld)15,(ld)-10);
// | m | < 1
printBresenhamLine((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
printBresenhamLine((ld)-3,(ld)2, (ld)-15,(ld)10);
// | m | < 1
printBresenhamLine((ld)-2,(ld)3, (ld)-10,(ld)15);
glEnd();
}
void printBresenhamLine(ld x1, ld y1, ld x2, ld y2) {
// m : slope;
ld pad = PADDING, scale = SCALE;
x1 = x1*scale + pad;
x2 = x2*scale + pad;
y1 = y1*scale + pad;
y2 = y2*scale + pad;
ld dx, dy;
ld x, y, xEnd, p, mirrorLine;
bool printMirror = false;
dx = abs(x2-x1);
dy = abs(y2-y1);
p = 2*dy - dx;
if(x1 > x2) swap(x1,x2), swap(y1, y2);
x = x1;
y = y1;
xEnd = x2;
glVertex2d(x,y);
if(y1 > y2) {
mirrorLine = y;
printMirror = true;
y2 = y1 + (y1 - y2);
}
while(x < xEnd) {
x ++;
if(p < 0) {
p += 2*dy;
} else {
y ++;
p = 2*(dy-dx);
}
if(printMirror) glVertex2d(x,mirrorLine - (y-mirrorLine));
else glVertex2d(x,y);
}
}