forked from Whales/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.cpp
151 lines (140 loc) · 3.24 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
#include "line.h"
#include "map.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;
}
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) >> 1 > abs(dy) || dy == 0) {
return WEST;
} else if (abs(dy) >> 1 > abs(dx)) {
if (dy < 0)
return NORTH;
else
return SOUTH;
} else {
if (dy < 0)
return NORTHWEST;
else
return SOUTHWEST;
}
} else {
if (dx >> 1 > abs(dy) || dy == 0) {
return EAST;
} else if (abs(dy) >> 1 > 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 "WEIRD DIRECTION_NAME() BUG";
}