-
Notifications
You must be signed in to change notification settings - Fork 1
/
hitsur.cpp
290 lines (240 loc) · 6.51 KB
/
hitsur.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "StdAfx.h"
HitSur::HitSur(const HDC hdc, const float zoom, const float offx, const float offy, const int width, const int height, const int hitx, const int hity, ISelect * const pbackground) : Sur(hdc, zoom, offx, offy, width, height)
{
m_pselected = pbackground;
m_hitx = hitx;
m_hity = hity;
SetObject(NULL);
}
HitSur::~HitSur()
{
}
void HitSur::Line(const float x, const float y, const float x2, const float y2)
{
if (m_pcur == NULL)
return;
const int x_1 = SCALEXf(x);
const int y_1 = SCALEYf(y);
const int x_2 = SCALEXf(x2);
const int y_2 = SCALEYf(y2);
if (abs(x_2 - x_1) > abs(y_2 - y_1))
{
int lineY = m_hity + 4;
if (x_1 > x_2)
{
if (m_hitx >= x_2 && m_hitx <= x_1)
lineY = ((y_1 - y_2)*(m_hitx - x_2)) / (x_1 - x_2) + y_2;
}
else
if (m_hitx >= x_1 && m_hitx <= x_2)
lineY = ((y_2 - y_1)*(m_hitx - x_1)) / (x_2 - x_1) + y_1;
if (m_hity + 4 > lineY && m_hity < lineY + 4)
{
m_pselected = m_pcur;
return;
}
}
else if (abs(x_2 - x_1) < abs(y_2 - y_1))
{
int lineX = m_hitx + 4;
if (y_1 > y_2)
{
if (m_hity >= y_2 && m_hity <= y_1)
lineX = ((x_1 - x_2)*(m_hity - y_2)) / (y_1 - y_2) + x_2;
}
else
if (m_hity >= y_1 && m_hity <= y_2)
lineX = ((x_2 - x_1)*(m_hity - y_1)) / (y_2 - y_1) + x_1;
if (m_hitx + 4 > lineX && m_hitx < lineX + 4)
{
m_pselected = m_pcur;
return;
}
}
}
void HitSur::Rectangle(const float x, const float y, const float x2, float y2)
{
if (m_pcur == NULL)
return;
int ix = SCALEXf(x);
int iy = SCALEYf(y);
int ix2 = SCALEXf(x2);
int iy2 = SCALEYf(y2);
if (ix > ix2)
{
const int temp = ix;
ix = ix2;
ix2 = temp;
}
if (iy > iy2)
{
const int temp = iy;
iy = iy2;
iy2 = temp;
}
if (m_hitx >= ix && m_hitx <= ix2 && m_hity >= iy && m_hity <= iy2)
{
m_pselected = m_pcur;
}
}
void HitSur::Rectangle2(const int x, const int y, const int x2, const int y2)
{
if (m_pcur == NULL)
{
return;
}
}
void HitSur::Ellipse(const float centerx, const float centery, const float radius)
{
if (m_pcur == NULL)
return;
const int ix = SCALEXf(centerx);
const int iy = SCALEYf(centery);
const long long ir = SCALEDf(radius);
const long long dx = m_hitx - ix;
const long long dy = m_hity - iy;
const long long dist = dx*dx + dy*dy;
if (dist <= ir*ir)
{
m_pselected = m_pcur;
}
}
void HitSur::Ellipse2(const float centerx, const float centery, const int radius)
{
if (m_pcur == NULL)
return;
const int ix = SCALEXf(centerx);
const int iy = SCALEYf(centery);
const long long dx = m_hitx - ix;
const long long dy = m_hity - iy;
const long long dist = dx*dx + dy*dy;
if (dist <= radius*radius)
{
m_pselected = m_pcur;
}
}
void HitSur::Polygon(const Vertex2D * const rgv, const int count)
{
if (m_pcur == NULL)
return;
int x1 = SCALEXf(rgv[count - 1].x);
int y1 = SCALEYf(rgv[count - 1].y);
bool hx1 = (m_hitx >= x1);
bool hy1 = (m_hity > y1);
int crosscount = 0; // count of lines which the hit point is to the left of
for (int i = 0; i < count; ++i)
{
const int x2 = x1;
const int y2 = y1;
const bool hx2 = hx1;
const bool hy2 = hy1;
x1 = SCALEXf(rgv[i].x);
y1 = SCALEYf(rgv[i].y);
hx1 = (m_hitx >= x1);
hy1 = (m_hity > y1);
if ((y1 == y2) ||
(!hy1 && !hy2) || (hy1 && hy2) || // if out of y range, forget about this segment
(hx1 && hx2)) // Hit point is on the right of the line
continue;
if (!hx1 && !hx2)
{
crosscount ^= 1;
continue;
}
if (x2 == x1)
{
if (!hx2)
crosscount ^= 1;
continue;
}
// Now the hard part - the hit point is in the line bounding box
if (x2 - (y2 - m_hity)*(x1 - x2) / (y1 - y2) > m_hitx)
crosscount ^= 1;
}
if (crosscount & 1)
{
m_pselected = m_pcur;
}
}
// copy-pasted from above
void HitSur::Polygon(const std::vector<RenderVertex> &rgv)
{
if (m_pcur == NULL)
return;
int x1 = SCALEXf(rgv[rgv.size() - 1].x);
int y1 = SCALEYf(rgv[rgv.size() - 1].y);
bool hx1 = (m_hitx >= x1);
bool hy1 = (m_hity > y1);
int crosscount = 0; // count of lines which the hit point is to the left of
for (size_t i = 0; i < rgv.size(); ++i)
{
const int x2 = x1;
const int y2 = y1;
const bool hx2 = hx1;
const bool hy2 = hy1;
x1 = SCALEXf(rgv[i].x);
y1 = SCALEYf(rgv[i].y);
hx1 = (m_hitx >= x1);
hy1 = (m_hity > y1);
if ((y1 == y2) ||
(!hy1 && !hy2) || (hy1 && hy2) || // if out of y range, forget about this segment
(hx1 && hx2)) // Hit point is on the right of the line
continue;
if (!hx1 && !hx2)
{
crosscount ^= 1;
continue;
}
if (x2 == x1)
{
if (!hx2)
crosscount ^= 1;
continue;
}
// Now the hard part - the hit point is in the line bounding box
if (x2 - (y2 - m_hity)*(x1 - x2) / (y1 - y2) > m_hitx)
crosscount ^= 1;
}
if (crosscount & 1)
{
m_pselected = m_pcur;
}
}
void HitSur::PolygonImage(const std::vector<RenderVertex> &rgv, HBITMAP hbm, const float left, const float top, const float right, const float bottom, const int bitmapwidth, const int bitmapheight)
{
Polygon(rgv);
}
void HitSur::Polyline(const Vertex2D * const rgv, const int count)
{
if (m_pcur == NULL)
return;
for (int i = 0; i < count - 1; ++i)
Line(rgv[i].x, rgv[i].y, rgv[i + 1].x, rgv[i + 1].y);
}
void HitSur::Lines(const Vertex2D * const rgv, const int count)
{
if (m_pcur == NULL)
return;
for (int i = 0; i < count * 2; i += 2)
Line(rgv[i].x, rgv[i].y, rgv[i + 1].x, rgv[i + 1].y);
}
void HitSur::Arc(const float x, const float y, const float radius, const float pt1x, const float pt1y, const float pt2x, const float pt2y)
{
//Ellipse(x, y, radius);
}
void HitSur::Image(const float x, const float y, const float x2, const float y2, HDC hdcSrc, const int width, const int height)
{
}
void HitSur::SetObject(ISelect * const psel)
{
m_pcur = psel;
}
void HitSur::SetFillColor(const int rgb)
{
}
void HitSur::SetBorderColor(const int rgb, const bool dashed, const int width)
{
}
void HitSur::SetLineColor(const int rgb, const bool dashed, const int width)
{
}