-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathMapRotate.cpp
More file actions
338 lines (295 loc) · 10.5 KB
/
MapRotate.cpp
File metadata and controls
338 lines (295 loc) · 10.5 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* ****************************************************************** **
** MapRotate.cpp: implementation of the MapRotate class.
**
** ****************************************************************** */
#include <stdlib.h>
#include "stdafx.h"
#include "MapRotate.h"
#include <strsafe.h>
/* *************************************************************************** **
** Rotate::Rotate()
**
** Constructor
**
** *************************************************************************** */
Rotate::Rotate()
{
curExtent.left = 0;
curExtent.top = 0;
curExtent.right = 0;
curExtent.bottom = 0;
xAxisDiff = 0;
yAxisDiff = 0;
_curHeight = 0;
_curWidth = 0;
_lastError = true;
_hBMBuffer = NULL;
}
/* *************************************************************************** **
** Rotate::~Rotate()
**
** Destructor
**
** *************************************************************************** */
Rotate::~Rotate()
{
}
/* *************************************************************************** **
** bool Rotate::calcRotatedExtent(long curViewWidth, long curViewHeight)
**
** Function to caculate the rotated extent for the current rotation angle.
**
** *************************************************************************** */
void Rotate::calcRotatedExtent(long curViewWidth, long curViewHeight)
{
setupWorldTransform(curViewWidth, curViewHeight);
}
/* *************************************************************************** **
** bool Rotate::getOriginalPixelPoint(long rotatedX, long rotatedY, long *origX, long *origY)
**
** Function to calculate the real (unrotated XY's) from the
** mouse xy's passed to function (rotated xy's).
**
** *************************************************************************** */
void Rotate::getOriginalPixelPoint(long rotatedX, long rotatedY, long *origX, long *origY)
{
LONG xLen = (_curWidth / 2) - rotatedX;
LONG yLen = (_curHeight / 2) - rotatedY;
float angle1 = atan2((float) yLen, (float) xLen);
float diffAngle = angle1 + (float)((degAngle * pi_) / 180.0);
float radLen = sqrtf((float)((xLen * xLen) + (yLen * yLen)));
float yNew1 = (float)(_curHeight / 2.0) - (radLen * sinf(diffAngle));
float xNew1 = (float)(_curWidth / 2.0) - (radLen * cosf(diffAngle));
*origX = (long)xNew1;
*origY = (long)yNew1;
}
/* *************************************************************************** **
** bool Rotate::getNewPixelPoint(long origX, long origY, long *rotatedX, long *rotatedY)
**
** Function to calculate the new (rotated XY's) from the
** current xy's passed to function.
**
** *************************************************************************** */
void Rotate::getNewPixelPoint(long origX, long origY, long *rotatedX, long *rotatedY)
{
LONG xLen = (_curWidth / 2) - origX;
LONG yLen = (_curHeight / 2) - origY;
float angle1 = atan2((float) yLen, (float) xLen);
float tmpA1 = angle1 * (float)(180.00 / pi_);
float diffAngle = angle1 - (float)((degAngle * pi_) / 180.0);
float tmpdiffA = diffAngle * (float)(180.00 / pi_);
float radLen = sqrtf((float)((xLen * xLen) + (yLen * yLen)));
float yNew1 = (float)(_curHeight / 2.0) + (radLen * sinf(diffAngle));
float xNew1 = (float)(_curWidth / 2.0) + (radLen * cosf(diffAngle));
*rotatedX = (long)xNew1;
*rotatedY = (long)yNew1;
}
/* *************************************************************************** **
** bool Rotate::setupRotateBackbuffer(HDC rotateBuffer, HDC controlDC,
** OLE_COLOR backColor)
**
** Function to set up a backbuffer for rotating the output.
**
** *************************************************************************** */
bool Rotate::setupRotateBackbuffer(HDC rotateBuffer, HDC controlDC, OLE_COLOR backColor)
{
RECT rotateExtent;
// Set Graphics mode to advanced, (clearer output)
_saveGraphicsMode = GetGraphicsMode(rotateBuffer);
if (SetGraphicsMode(rotateBuffer, GM_ADVANCED) == 0)
{
_lastError = GetLastError();
return false;
}
// Setup the XFORM structure
setupWorldTransform(_curWidth, _curHeight);
// bitmap for scribbling on
if ((_hBMBuffer = CreateCompatibleBitmap(controlDC, rotatedWidth, rotatedHeight)) == NULL)
{
_lastError = GetLastError();
return false;
}
SelectObject(rotateBuffer, _hBMBuffer);
rotateExtent.left = 0;
rotateExtent.top = 0;
rotateExtent.right = rotatedWidth;
rotateExtent.bottom = rotatedHeight;
HBRUSH fillBrush = CreateSolidBrush(backColor);
FillRect(rotateBuffer, &(rotateExtent), fillBrush);
DeleteObject(fillBrush);
// Save current setting, then apply new
if (GetWorldTransform(rotateBuffer, &_savedXform))
{
if (SetWorldTransform(rotateBuffer, &_curXform) == 0)
{
_lastError = GetLastError();
return false;
}
}
else
{
_lastError = GetLastError();
return false;
}
return true;
}
/* ***************************************************************************
** bool Rotate::setupWorldTransform(HDC rotateBuffer)
**
** Function to setup the WorldTransform on the backbuffer.
**
** *************************************************************************** */
void Rotate::setupWorldTransform(long bufWidth, long bufHeight)
{
float x1, x2, x3, y1, y2, y3;
float minX, minY, maxX, maxY;
float rotCenX, rotCenY;
float origRotX, origRotY, newRotX, newRotY;
float newWidth, newHeight;
origRotX = (float)(bufWidth / 2.0);
origRotY = (float)(bufHeight / 2.0);
x1 = (float)(bufHeight * _sine);
y1 = (float)(bufHeight * _cosine);
x2 = (float)(bufWidth * _cosine) + (float)(bufHeight * _sine);
y2 = (float)(bufHeight * _cosine) - (float)(bufWidth * _sine);
x3 = (float)(bufWidth * _cosine);
y3 = (float)(-bufWidth * _sine);
minX = min(0,min(x1, min(x2, x3)));
minY = min(0,min(y1, min(y2, y3)));
maxX = max(0, max(x1, max(x2, x3)));
maxY = max(0, max(y1, max(y2, y3)));
calcRotatedPoint(origRotX, origRotY, minX, minY, &rotCenX, &rotCenY);
xAxisDiff = abs(rotCenX - origRotX);
yAxisDiff = abs(rotCenY - origRotY);
newRotX = origRotX + xAxisDiff;
newRotY = origRotY + yAxisDiff;
newWidth = bufWidth + (float)(2 * xAxisDiff);
newHeight = bufHeight + (float)(2.0 * yAxisDiff);
x1 = newHeight * _sine;
y1 = newHeight * _cosine;
x2 = newWidth * _cosine + newHeight * _sine;
y2 = newHeight * _cosine - newWidth * _sine;
x3 = newWidth * _cosine;
y3 = -newWidth * _sine;
minX = min(0,min(x1, min(x2, x3)));
minY = min(0,min(y1, min(y2, y3)));
maxX = max(0, max(x1, max(x2, x3)));
maxY = max(0, max(y1, max(y2, y3)));
calcRotatedPoint(newRotX, newRotY, minX, minY, &rotCenX, &rotCenY);
_curXform.eM11 = _cosine;
_curXform.eM12 = -_sine;
_curXform.eM21 = _sine;
_curXform.eM22 = _cosine;
_curXform.eDx = -minX - (rotCenX - newRotX) - xAxisDiff;
_curXform.eDy = -minY - (rotCenY - newRotY) - yAxisDiff;
rotatedWidth = Round(maxX - minX);
rotatedHeight = Round(maxY - minY);
}
/* ***************************************************************************
** bool Rotate::resetWorldTransform(HDC rotateBuffer)
**
** Function to reset WorldTransform on the backbuffer.
**
** *************************************************************************** */
bool Rotate::resetWorldTransform(HDC rotateBuffer)
{
// Reset World Transform to previous value (if not BitBlt fails , invalid parameter)
if (SetWorldTransform(rotateBuffer, &_savedXform) != 0)
{
if (SetGraphicsMode(rotateBuffer, _saveGraphicsMode) == 0)
{
_lastError = GetLastError();
return false;
}
}
else
{
_lastError = GetLastError();
return false;
}
return true;
}
/* ***************************************************************************
** bool Rotate::cleanupRotation(HDC rotateBuffer)
**
** Function to cleanup up any DC's.
**
** *************************************************************************** */
bool Rotate::cleanupRotation(HDC rotateBuffer)
{
if (_hBMBuffer != NULL )
{
if (DeleteObject(_hBMBuffer) == 0)
{
_lastError = GetLastError();
return false;
}
}
return true;
}
/* *************************************************************************** **
** void Rotate::calcRotatedPoint(float ptX, float ptY, long minX, long minY,
** float *rotPtX, float *rotPtY)
**
** Function to calculate a rotated point
**
** *************************************************************************** */
void Rotate::calcRotatedPoint(float ptX, float ptY, float minX, float minY, float *rotPtX, float *rotPtY)
{
*rotPtX = (ptX * _cosine) + (ptY * _sine) + (-minX);
*rotPtY = (ptX * -_sine) + (ptY * _cosine) + (-minY);
}
/* ***************************************************************************
** void Rotate::drawCenter(HDC tmpBuffer)
**
** Function to draw cross hairs in center of map. Used in testing.
**
** *************************************************************************** */
void Rotate::drawCenter(HDC tmpBuffer, COLORREF color)
{
POINT pts[2];
HPEN penOld = NULL;
HPEN penBlue = NULL;
penBlue = CreatePen(PS_SOLID, 1, color);
penOld = (HPEN) SelectObject(tmpBuffer, penBlue);
pts[0].x = (int)(_curWidth / 2 - 100);
pts[0].y = (int)(_curHeight / 2);
pts[1].x = (int)(_curWidth / 2 + 100);
pts[1].y = (int)(_curHeight / 2);
Polyline(tmpBuffer,(CONST POINT *) &pts, 2);
pts[0].x = (int)(_curWidth / 2);
pts[0].y = (int)(_curHeight / 2 - 100);
pts[1].x = (int)(_curWidth / 2);
pts[1].y = (int)(_curHeight / 2 + 100);
Polyline(tmpBuffer,(CONST POINT *) &pts, 2);
if (penOld != NULL)
{
SelectObject(tmpBuffer, penOld);
DeleteObject(penBlue);
}
}
/* ***************************************************************************
** long Rotate::Round(float val)
**
** Function to float value to integer
**
** *************************************************************************** */
inline long Rotate::Round(float val)
{
if (val > 0)
return((long)(val + 0.5));
else
return((long)(val - 0.5));
}
void Rotate::AdjustRect(CRect& rect)
{
CRect rectTmp = rect;
long tmpX = 0, tmpY = 0;
this->getOriginalPixelPoint(rect.left, rect.top, &tmpX, &tmpY);
rectTmp.TopLeft().x = tmpX;
rectTmp.TopLeft().y = tmpY;
this->getOriginalPixelPoint(rect.right, rect.bottom, &tmpX, &tmpY);
rectTmp.BottomRight().x = tmpX;
rectTmp.BottomRight().y = tmpY;
rect = rectTmp;
}