-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGS_line.cpp
More file actions
138 lines (121 loc) · 4.31 KB
/
GS_line.cpp
File metadata and controls
138 lines (121 loc) · 4.31 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
#include "proj.h"
void drawGSPoint(point p, float i, int color) {
i = 1-i;
if (i < 0) {i = 0;}
// set point color
float red_c = 0.0f, green_c = 0.0f, blue_c = 0.0f;
if (color == GS_RED) red_c = 1.0f;
else if (color == GS_GREEN) green_c = 1.0f;
else if (color == GS_BLUE) blue_c = 1.0f;
else {red_c=1.0f; green_c=1.0f; blue_c=1.0f;}
glColor3f(red_c * i, green_c * i, blue_c * i);
// set point size and plot
glBegin(GL_POINTS);
glPointSize(1);
glVertex2d(p.x, p.y);
glEnd();
}
int negate(int self, int other) {
return 2 * other - self;
}
point swap(int x, int y, int x0, int y0) {
return {x0 + (y-y0), y0 + (x-x0)};
}
point transformPoint(int x, int y, double s, int x0, int x1, int y0) {
if (x0<=x1) {
if (s > 1) return swap(x, y, x0, y0);
else if (-1 <= s && s < 0) return {x, negate(y, y0)};
else if (s < -1) {
point temp = swap(x, y, x0, y0);
return {temp.x, negate(temp.y, y0)};
}
} else {
if (s >= 0 && s <= 1) return {negate(x,x0), negate(y,y0)};
else if (1 < s) {
point temp = swap(x, y, x0, y0);
return {negate(temp.x, x0), negate(temp.y, y0)};
}
else if (-1 <= s && s < 0) return {negate(x,x0), y};
else {
point temp = swap(x, y, x0, y0);
return {negate(temp.x, x0), temp.y};
}
}
return {x, y};
}
point transform_p1(int x0, int y0, int x1, int y1, double s) {
if (x0<=x1) {
if (s > 1) return swap(x1, y1, x0, y0);
else if (-1 <= s && s < 0) return {x1, negate(y1, y0)};
else if (s < -1) {
point temp = swap(x1, y1, x0, y0);
return {negate(temp.x, x0), temp.y};
}
} else {
if (s >= 0 && s <= 1) return {negate(x1, x0), negate(y1, y0)};
else if (1 < s) {
point temp = swap(x1, y1, x0, y0);
return {negate(temp.x, x0), negate(temp.y, y0)};
}
else if (-1 <= s && s < 0) return {negate(x1, x0), y1};
else {
point temp = swap(x1, y1, x0, y0);
return {temp.x, negate(temp.y, y0)};
}
}
return {x1, y1};
}
int checkStipple(unsigned short &s) {
if ((s & 1) == 1) {
s = (s >> 1) | 0b1000000000000000;
return 1;
} else {
s = s >> 1;
return 0;
}
}
void GSLine(int x0, int y0, int x1, int y1, int color, unsigned short stipple, unsigned int thickness) {
int x0T = x0, x1T = x1, y0T = y0, y1T = y1;
float dxT = x1-x0, dyT = y1-y0; // calculate and save true slope
double slope = dyT/dxT;
point p1 = transform_p1(x0, y0, x1, y1, slope);// transform initial points
float dx = p1.x-x0, dy = p1.y-y0; // redo dx and dy calculations
float d = 2 * dy - dx;
float deltaE = 2 * dy, deltaNE = 2 * (dy-dx);
float tvdx = 0;
float L = 2.0 * sqrt(dx * dx + dy * dy);
float Dyp, DypPlus, DypMinus;
int x = x0, y = y0;
if (checkStipple(stipple)) {
drawGSPoint(transformPoint(x, y, slope, x0T, x1T, y0T), 0, color);
for (int i = 1; i < thickness; i++) {
drawGSPoint(transformPoint(x, y+i, slope, x0T, x1T, y0T), 0, color);
drawGSPoint(transformPoint(x, y-i, slope, x0T, x1T, y0T), 0, color);
}
drawGSPoint(transformPoint(x, y+thickness, slope, x0T, x1T, y0T), .5, color);
drawGSPoint(transformPoint(x, y-thickness, slope, x0T, x1T, y0T), .5, color);
}
while (x < p1.x) {
if (d <= 0){ // E
tvdx = d+dx;
d = d + deltaE;
} else { // NE
tvdx = d-dx;
d = d + deltaNE;
y++;
}
x++;
Dyp = abs((tvdx)/L);
DypPlus = abs((2*dx - tvdx)/L);
DypMinus = abs((2*dx + tvdx)/L);
if (checkStipple(stipple)) {
drawGSPoint(transformPoint(x, y, slope, x0T, x1T, y0T), 0, color);
for (int th = 1; th < thickness; th++) {
drawGSPoint(transformPoint(x, y+th, slope, x0T, x1T, y0T), 0, color);
drawGSPoint(transformPoint(x, y-th, slope, x0T, x1T, y0T), 0, color);
}
drawGSPoint(transformPoint(x, y+thickness, slope, x0T, x1T, y0T), DypPlus, color);
drawGSPoint(transformPoint(x, y-thickness, slope, x0T, x1T, y0T), DypMinus, color);
}
}
}