-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawInterWindow.cpp
More file actions
176 lines (153 loc) · 4.18 KB
/
DrawInterWindow.cpp
File metadata and controls
176 lines (153 loc) · 4.18 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "DrawInterWindow.h"
void DrawInterWindow::printHelp() {
printf("You can rotate cube by mouse and arrows\n"
"You can increase and decrease cube by mouse, Z and X\n");
}
I2Point DrawInterWindow::project(R3Point &pt) {
R3Point prPoint = curRot(pt);
int prX = winSize.x / 5;
int prY = winSize.y / 5;
return I2Point(round(prX*((-1./2)*prPoint.getX() + prPoint.getY()) + 2*prX),
round(prY*((sqrt(3)/2)*prPoint.getX() - prPoint.getZ()) + 3*prY));
}
void DrawInterWindow::setPlane(const InterPlane &plane) {
this->plane = plane;
}
void DrawInterWindow::onExpose(XEvent&) {
winSize.x = getWindowRect().width();
winSize.y = getWindowRect().height();
setForeground(getBackground());
fillRectangle(m_RWinRect);
draw();
}
void DrawInterWindow::onKeyPress(XEvent& event) {
Rotation E;
R3Point Oz(0, 0, 1);
R3Point Oy(0, 1, 0);
double phi = M_PI / 60;
KeySym key;
char keyName[256];
XLookupString(&(event.xkey), keyName, 255, &key, 0);
switch ((int) key) {
case 113: // q
destroyWindow();
break;
case 32: // space
curRot = E;
break;
case 65361: //left
curRot = E.rotate(Oz, -phi) * curRot;
break;
case 65362: //up
curRot = E.rotate(Oy, -phi) * curRot;
break;
case 65363: //right
curRot = E.rotate(Oz, phi) * curRot;
break;
case 65364: //down
curRot = E.rotate(Oy, phi) * curRot;
break;
case 122: //Z
curRot = E.increase(1.03) * curRot;
case 120: //X
curRot = E.decrease(1.03) * curRot;
default:
break;
}
//printf("Key: %d\n", (int) key);
redraw();
}
void DrawInterWindow::onButtonPress(XEvent& event) {
Rotation E;
switch (event.xbutton.button) {
case Button4:
//printf("Scrolled up\n");
curRot = E.increase(1.03) * curRot;
redraw();
break;
case Button5:
//printf("Scrolled down\n");
curRot = E.decrease(1.03) * curRot;
redraw();
break;
default:
curMousePos = I2Point(event.xbutton.x, event.xbutton.y);
break;
}
}
void DrawInterWindow::onMotionNotify(XEvent& event) {
if (event.xmotion.state & Button1Mask) {
I2Point oldMousePos = curMousePos;
curMousePos.x = event.xbutton.x;
curMousePos.y = event.xbutton.y;
int dx = curMousePos.x - oldMousePos.x;
int dy = curMousePos.y - oldMousePos.y;
Rotation E;
R3Point Oz(0, 0, 1);
R3Point Oy(0, 1, 0);
double phi = M_PI / 180;
curRot = E.rotate(Oz, dx * phi) * curRot;
curRot = E.rotate(Oy, dy * phi) * curRot;
redraw();
draw();
}
}
void DrawInterWindow::draw() {
setBackground("LightGray");
drawAxes();
setForeground("green");
drawInter();
setForeground("SeaGreen");
drawCube();
}
void DrawInterWindow::drawAxes() {
R3Point zero = {0, 0, 0};
R3Point Ox = {2, 0, 0};
R3Point Oy = {0, 2, 0};
R3Point Oz = {0, 0, 2};
setForeground("red");
drawLine(project(zero), project(Ox));
setForeground("blue");
drawLine(project(zero), project(Oy));
setForeground("black");
drawLine(project(zero), project(Oz));
}
void DrawInterWindow::drawCube() {
R3Point p1 = {0, 0, 0};
R3Point p2 = {0, 0, 1};
R3Point p3 = {0, 1, 0};
R3Point p4 = {0, 1, 1};
R3Point p5 = {1, 0, 0};
R3Point p6 = {1, 0, 1};
R3Point p7 = {1, 1, 0};
R3Point p8 = {1, 1, 1};
drawLine(project(p1), project(p2));
drawLine(project(p1), project(p3));
drawLine(project(p1), project(p5));
drawLine(project(p3), project(p4));
drawLine(project(p4), project(p2));
drawLine(project(p2), project(p6));
drawLine(project(p3), project(p7));
drawLine(project(p5), project(p6));
drawLine(project(p5), project(p7));
drawLine(project(p6), project(p8));
drawLine(project(p7), project(p8));
drawLine(project(p4), project(p8));
}
void DrawInterWindow::drawInter() {
R3Point *pts = plane.interWithCube();
for(int i = 0; i < 10; i++)
for(int j = i+1; j < 11; j++)
for(int k = j+1; k < 12; k++) {
if (pts[i].inCube() && pts[j].inCube()
&& pts[k].inCube()) {
I2Point *buf = new I2Point[3];
buf[0] = project(pts[i]);
buf[1] = project(pts[j]);
buf[2] = project(pts[k]);
fillPolygon(buf, 3);
delete [] buf;
}
}
delete [] pts;
}