-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
198 lines (145 loc) · 5.6 KB
/
utils.py
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
# -*- coding: utf-8 -*-
"""Utilities.
Cells coordinates are represented in tuple (x, y).
"""
import re
import textwrap
import math
import itertools
def wrap_tagged(text, width, **kwargs):
"""Wrap text which contains BearLibTerminal tags like '[color=red]something[/color]'.
Can pass kwargs to textwrap.wrap().
drop_whitespace=False by default."""
lines = text.splitlines()
wrapped_lines = []
# handling bearlibterminal's tag with textwrap, it was hard for me!
# 1. find and extract tag and its positions
# 2. textwrap (w/o tag strings)
# 3. insert extracted tags at original position
pattern = r'\[.*?\]' # tag syntax
for line_ in lines:
iterator = re.finditer(pattern, line_)
tags = []
for match in iterator:
tags.append((match.start(), match.group()))
line_ = re.sub(pattern, '', line_)
wrapped_line = textwrap.wrap(line_,
width,
drop_whitespace=False,
**kwargs)
compensation = 0 # we need this because textwrap does not count tag strings
for start, group in tags:
# cur = start + compensation
cur = start
num = cur // (width + compensation)
# insert
wrapped_line[num] = group.join(
[wrapped_line[num][:cur], wrapped_line[num][cur:]])
compensation += len(group)
wrapped_lines.extend(wrapped_line)
return wrapped_lines
def degree_between(src_x, src_y, dst_x, dst_y):
"""Return degree between two coordinates.
Return value: [0, 360)
Examples:
- (1, 0), (0, 1) -> 90
- (1, 0), (0, -1) -> 270
"""
dist = distance(src_x, src_y, dst_x, dst_y)
# avoid zero dividing
if dist == 0:
raise ValueError(
'Function degree_between() receives two identical points.')
# degree in [-90, 90]
degree = math.degrees(math.asin((dst_y - src_y) / dist))
# imagine graph
if dst_x - src_x < 0:
degree = 180 - degree
elif degree < 0: # delta x > 0 and...
degree += 360
return degree
def degree_difference(deg1, deg2):
"""Return a difference between deg1 and deg2.
deg1: (-inf, inf)
deg2: (-inf, inf)
return value: [0, 180)
"""
if deg1 - deg2 > 0:
dif = math.fmod(deg1 - deg2, 360)
else:
dif = math.fmod(deg2 - deg1, 360)
return 360 - dif if dif >= 180 else dif
def in_angle(src_x, src_y, dst_x, dst_y, direction, angle_range):
"""Return True if the destination point is in given angle.
direction: [0, 360) (like where you look at)
angle_range: [0, 360) (like how wide your vision is)
If destination is identical to source, return False.
"""
if src_x == dst_x and src_y == dst_y:
return False
if degree_difference(direction, degree_between(src_x, src_y, dst_x,
dst_y)) < angle_range / 2:
return True
return False
def sector(src_x, src_y, dst_x, dst_y, angle, radius, bnd_x=1000, bnd_y=1000):
"""Return a list of cells which shapes sector (fan shaped figure).
direction: float in [0, 360)
angle: float in [0, 360)
radius: float in [0, inf)
"""
left = max(src_x - radius, 0)
right = min(src_x + radius + 1, bnd_x)
bottom = max(src_y - radius, 0)
top = min(src_y + radius + 1, bnd_y)
# return empty list if given two identical points
# degree_between() raise an error if given those points
if src_x == dst_x and src_y == dst_y:
return []
direction = degree_between(src_x, src_y, dst_x, dst_y)
cells = []
# limit search area within square
for cur_x, cur_y in itertools.product(range(left, right),
range(bottom, top)):
if (distance(src_x, src_y, cur_x, cur_y) <= radius
and in_angle(src_x, src_y, cur_x, cur_y, direction, angle)):
cells.append((cur_x, cur_y))
return cells
def disk(center_x, center_y, radius, bnd_x=1000, bnd_y=1000):
# Return a list of cells which shapes a disk.
left = max(center_x - radius, 0)
right = min(center_x + radius + 1, bnd_x)
bottom = max(center_y - radius, 0)
top = min(center_y + radius + 1, bnd_y)
cells = []
# limit search area within square
for cur_y in range(bottom, top):
for cur_x in range(left, right):
if distance(center_x, center_y, cur_x, cur_y) <= radius:
cells.append((cur_x, cur_y))
return cells
def frame(center_x, center_y, radius, bnd_x=1000, bnd_y=1000):
"""Return a list of cells which shapes a frame.
You can specify boundary.
"""
left = center_x - radius
right = center_x + radius
bottom = center_y + radius
top = center_y - radius
cells = []
#→ ↓
#↑ ←
if 0 <= top < bnd_y: # topleft to topright
cells.extend([(x, top) for x in range(left, right) if 0 <= x < bnd_x])
if 0 <= right < bnd_x: # topright to bottomright
cells.extend([(right, y) for y in range(top, bottom)
if 0 <= y < bnd_y])
if 0 <= bottom < bnd_y: # bottomright to bottomleft
cells.extend([(x, bottom) for x in range(right, left, -1)
if 0 <= x < bnd_x])
if 0 <= left < bnd_x: # bottomleft to topleft
cells.extend([(left, y) for y in range(bottom, top, -1)
if 0 <= y < bnd_y])
return cells
def distance(src_x, src_y, dst_x, dst_y):
# Return distance between two points.
return math.sqrt((dst_x - src_x)**2 + (dst_y - src_y)**2)