forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.cpp
238 lines (222 loc) · 4.9 KB
/
line.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
#include "line.h"
#include <stdlib.h>
#define SGN(a) (((a)<0) ? -1 : 1)
std::vector <point> line_to(int x1, int y1, int x2, int y2, int t)
{
std::vector<point> ret;
int dx = x2 - x1;
int dy = y2 - y1;
int ax = abs(dx)<<1;
int ay = abs(dy)<<1;
int sx = SGN(dx);
int sy = SGN(dy);
if (dy == 0) sy = 0;
if (dx == 0) sx = 0;
point cur;
cur.x = x1;
cur.y = y1;
int xmin = (x1 < x2 ? x1 : x2), ymin = (y1 < y2 ? y1 : y2),
xmax = (x1 > x2 ? x1 : x2), ymax = (y1 > y2 ? y1 : y2);
xmin -= abs(dx);
ymin -= abs(dy);
xmax += abs(dx);
ymax += abs(dy);
if (ax == ay) {
do {
cur.y += sy;
cur.x += sx;
ret.push_back(cur);
} while ((cur.x != x2 || cur.y != y2) &&
(cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
} else if (ax > ay) {
do {
if (t > 0) {
cur.y += sy;
t -= ax;
}
cur.x += sx;
t += ay;
ret.push_back(cur);
} while ((cur.x != x2 || cur.y != y2) &&
(cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
} else {
do {
if (t > 0) {
cur.x += sx;
t -= ay;
}
cur.y += sy;
t += ax;
ret.push_back(cur);
} while ((cur.x != x2 || cur.y != y2) &&
(cur.x >= xmin && cur.x <= xmax && cur.y >= ymin && cur.y <= ymax));
}
return ret;
}
int trig_dist(int x1, int y1, int x2, int y2)
{
return int(sqrt(double(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0))));
}
int rl_dist(int x1, int y1, int x2, int y2)
{
int dx = abs(x1 - x2), dy = abs(y1 - y2);
if (dx > dy)
return dx;
return dy;
}
int rl_dist(point a, point b)
{
int dx = abs(a.x - b.x), dy = abs(a.y - b.y);
if (dx > dy)
return dx;
return dy;
}
double slope_of(std::vector<point> line)
{
double dX = line.back().x - line.front().x, dY = line.back().y - line.front().y;
if (dX == 0)
return SLOPE_VERTICAL;
return (dY / dX);
}
std::vector<point> continue_line(std::vector<point> line, int distance)
{
point start = line.back(), end = line.back();
double slope = slope_of(line);
int sX = (line.front().x < line.back().x ? 1 : -1),
sY = (line.front().y < line.back().y ? 1 : -1);
if (abs(slope) == 1) {
end.x += distance * sX;
end.y += distance * sY;
} else if (abs(slope) < 1) {
end.x += distance * sX;
end.y += int(distance * abs(slope) * sY);
} else {
end.y += distance * sY;
if (slope != SLOPE_VERTICAL)
end.x += int(distance / abs(slope)) * sX;
}
return line_to(start.x, start.y, end.x, end.y, 0);
}
direction direction_from(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
if (dx < 0) {
if (abs(dx) / 2 > abs(dy) || dy == 0) {
return WEST;
} else if (abs(dy) / 2 > abs(dx)) {
if (dy < 0)
return NORTH;
else
return SOUTH;
} else {
if (dy < 0)
return NORTHWEST;
else
return SOUTHWEST;
}
} else {
if (dx / 2 > abs(dy) || dy == 0) {
return EAST;
} else if (abs(dy) / 2 > dx || dx == 0) {
if (dy < 0)
return NORTH;
else
return SOUTH;
} else {
if (dy < 0)
return NORTHEAST;
else
return SOUTHEAST;
}
}
}
std::string direction_name(direction dir)
{
switch (dir) {
case NORTH: return "north";
case NORTHEAST: return "northeast";
case EAST: return "east";
case SOUTHEAST: return "southeast";
case SOUTH: return "south";
case SOUTHWEST: return "southwest";
case WEST: return "west";
case NORTHWEST: return "northwest";
}
return "BUG. (line.cpp:direction_name)";
}
std::string direction_name_short(direction dir)
{
switch (dir) {
case NORTH: return "N ";
case NORTHEAST: return "NE";
case EAST: return "E ";
case SOUTHEAST: return "SE";
case SOUTH: return "W ";
case SOUTHWEST: return "SW";
case WEST: return "W ";
case NORTHWEST: return "NW";
}
return "Bug. (line.cpp:direction_name_short)";
}
float rl_vec2d::norm(){
if (fabs(x) > fabs(y))
return fabs(x);
return fabs(y);
}
rl_vec2d rl_vec2d::normalized(){
rl_vec2d ret;
if (is_null()){ // shouldn't happen?
ret.x = ret.y = 1;
return ret;
}
float n = norm();
ret.x = x/n;
ret.y = y/n;
return ret;
}
float rl_vec2d::dot_product (rl_vec2d &v){
float dot = x*v.x + y*v.y;
// this is messy, but the dot of normalized rl_vecs should somehow be max 1
float tot1 = fabs(x) + fabs(y);
float tot2 = fabs(v.x) + fabs(v.y);
dot /= ((tot1+tot2)/2);
dot *= norm() * v.norm();
return dot;
}
bool rl_vec2d::is_null(){
return !(x || y);
}
// scale.
rl_vec2d rl_vec2d::operator* (const float rhs){
rl_vec2d ret;
ret.x = x * rhs;
ret.y = y * rhs;
return ret;
}
// subtract
rl_vec2d rl_vec2d::operator- (const rl_vec2d &rhs){
rl_vec2d ret;
ret.x = x - rhs.x;
ret.y = y - rhs.y;
return ret;
}
// unary negation
rl_vec2d rl_vec2d::operator- (){
rl_vec2d ret;
ret.x = -x;
ret.y = -y;
return ret;
}
rl_vec2d rl_vec2d::operator+ (const rl_vec2d &rhs){
rl_vec2d ret;
ret.x = x + rhs.x;
ret.y = y + rhs.y;
return ret;
}
rl_vec2d rl_vec2d::operator/ (const float rhs){
rl_vec2d ret;
ret.x = x / rhs;
ret.y = y / rhs;
return ret;
}