-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmappoint.cpp
executable file
·99 lines (82 loc) · 2.38 KB
/
cmappoint.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
#include "cmappoint.h"
#include <cmath>
uint qHash(const CMapPoint &point) {
return (point.x << 11) | point.y;
}
#include <QtDebug>
QDebug operator<<(QDebug dbg, const CMapPoint &point) {
dbg.nospace() << "CMapPoint(" << point.x << "," << point.y << ")";
return dbg.maybeSpace();
}
QDebug operator<<(QDebug dbg, const CLine &line) {
const char *angleString = "_";
switch (line.angle) {
case INVALID_LINE_ANGLE: angleString = "INVALID_LINE_ANGLE"; break;
case VERTICAL: angleString = "VERTICAL"; break;
case HORIZONTAL: angleString = "HORIZONTAL"; break;
case NW_SE: angleString = "NW_SE"; break;
case NE_SW: angleString = "NE_SW"; break;
}
dbg.nospace() <<
"CLine(" << angleString << "," <<
line.start << "," << line.end << ")";
return dbg.maybeSpace();
}
void CLine::snapToPoints(const CMapPoint &anchor, const CMapPoint &other) {
int xDiff = anchor.cartesianX() - other.cartesianX();
int yDiff = anchor.cartesianY() - other.cartesianY();
double angle = std::atan2(double(yDiff), double(xDiff)) * (180 / 3.14159265);
// 0: North West
// 45: North
// 90: North East
// 135: East
// 180: South East
// -135: South
// -90: South West
// -45: West
// This code is kind of crap. Oh well. It works.. hopefully?
int rawDirection = int(floor((angle + 22.5) / 45.0));
OrdinalDirection direction;
if (rawDirection >= 0)
direction = (OrdinalDirection)rawDirection;
else
direction = (OrdinalDirection)(8 + rawDirection);
int cartLength = std::floor(std::sqrt(double((xDiff*xDiff) + (yDiff*yDiff))));
int isoXDiff = (anchor.x - other.x) / 2;
int isoYDiff = (anchor.y - other.y) / 2;
int isoLength = std::floor(std::sqrt(double((isoXDiff*isoXDiff) + (isoYDiff*isoYDiff))));
CMapPoint endPoint = anchor;
switch (direction) {
// Simple isometric directions
case NORTH_WEST:
for (int i = 0; i < cartLength; i++)
endPoint.moveNW();
break;
case NORTH_EAST:
for (int i = 0; i < cartLength; i++)
endPoint.moveNE();
break;
case SOUTH_EAST:
for (int i = 0; i < cartLength; i++)
endPoint.moveSE();
break;
case SOUTH_WEST:
for (int i = 0; i < cartLength; i++)
endPoint.moveSW();
break;
// Cardinal directions
case WEST:
endPoint.x -= (isoLength * 2);
break;
case EAST:
endPoint.x += (isoLength * 2);
break;
case NORTH:
endPoint.y -= (isoLength * 2);
break;
case SOUTH:
endPoint.y += (isoLength * 2);
break;
}
tryAssign(anchor, endPoint);
}