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
|
# -*- coding: utf-8 -*-
#
# AWL simulator - 2D geometry
#
# Copyright 2017 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim_loader.common import *
from awlsim.common.datatypehelpers import * #+cimport
__all__ = [ "Point2D", "Vect2D", "Inter2D", "LineSeg2D", ]
class Base2D(object):
EPSILON = 0.000001
__slots__ = ()
def __eq__(self, other):
return self is other
def __ne__(self, other):
return not self.__eq__(other)
__hash__ = None
def __bool__(self):
return True
def __nonzero__(self): # Python 2 compat
return self.__bool__()
class BaseXY2D(Base2D):
"""2D X/Y base object
"""
__slots__ = ( "x", "y", )
def __init__(self, x=0, y=0):
self.x = x
self.y = y
@property
def xInt(self):
if isInteger(self.x):
return self.x
return int(round(self.x))
@property
def yInt(self):
if isInteger(self.y):
return self.y
return int(round(self.y))
def __eq__(self, other):
return self is other or\
(other is not None and\
abs(self.x - other.x) < self.EPSILON and\
abs(self.y - other.y) < self.EPSILON)
def __bool__(self):
return bool(self.x or self.y)
class Point2D(BaseXY2D):
"""2D point.
"""
def __repr__(self):
return "Point2D(x=%f, y=%f)" % (self.x, self.y)
class Vect2D(BaseXY2D):
"""2D vector.
"""
def __repr__(self):
return "Vect2D(x=%f, y=%f)" % (self.x, self.y)
class Inter2D(Base2D):
"""Intersection information for two 2D line segments.
"""
__slots__ = ( "point", "vect", "__intersects", )
def __init__(self, point=None, vect=None, intersects=False):
"""point => Intersection point, or None.
vect => Intersection vector.
None or Vect2D(0, 0), if the intersection is only in one point.
intersects => True, if there is an intersection.
"""
self.point = point
self.vect = vect or Vect2D()
self.__intersects = intersects
def __eq__(self, other):
return self is other or\
(other is not None and\
self.point == other.point and\
self.vect == other.vect and\
self.__intersects == other.__intersects)
def __bool__(self):
return self.intersects
@property
def intersects(self):
"""Returns True, if there is an intersection.
"""
return self.__intersects and\
self.point is not None
@property
def lineSeg(self):
"""Get the line segment of the intersection.
Returns None, if self.point is None.
"""
if self.point is None:
return None
return LineSeg2D(self.pointA, self.pointB)
@property
def pointA(self):
"""Get the starting point of the intersection.
Returns None, if there is no starting point.
"""
return self.point
@property
def pointB(self):
"""Get the end point of the intersection.
Returns None, if there is no end point.
"""
if self.point is None:
return None
return Point2D(self.point.x + self.vect.x,
self.point.y + self.vect.y)
def __repr__(self):
return "Inter2D(point=%s, vect=%s, intersects=%s)" % (
self.point, self.vect, self.__intersects)
class LineSeg2D(Base2D):
"""Line segment in 2D space.
"""
__slots__ = ( "pointA", "pointB", )
@classmethod
def fromCoords(cls, x0, y0, x1, y1):
return cls(Point2D(x0, y0), Point2D(x1, y1))
def __init__(self, pointA, pointB):
self.pointA = pointA
self.pointB = pointB
def __eq__(self, other):
return self is other or\
(other is not None and\
self.pointA == other.pointA and\
self.pointB == other.pointB)
def __bool__(self):
"""Returns True, if the segment is of non-zero length.
"""
return bool(self.vect)
@property
def isHorizontal(self):
"""Returns True, if the line segment is parallel to the X axis.
"""
return self.pointA.y == self.pointB.y
@property
def isVertical(self):
"""Returns True, if the line segment is parallel to the Y axis.
"""
return self.pointA.x == self.pointB.x
@property
def slope(self):
"""Get the slope of the line segment.
Raises ZeroDivisionError if the line segment is vertical.
"""
return float(self.pointB.y - self.pointA.y) / \
(self.pointB.x - self.pointA.x)
@property
def intercept(self):
"""Get the Y value of the Y axis crossing of this line.
Raises ZeroDivisionError if the line segment is vertical.
"""
return self.pointA.y - (self.pointA.x * self.slope)
@property
def vect(self):
"""Get the line segment vector.
"""
return Vect2D(-self.pointA.x + self.pointB.x,
-self.pointA.y + self.pointB.y)
@staticmethod
def __inRect(x, y, diaPointA, diaPointB):
"""Check if point (x,y) is within an axis-aligned rect
with the diagonal (diaPointA,diaPointB).
"""
diaMinX, diaMaxX = min(diaPointA.x, diaPointB.x),\
max(diaPointA.x, diaPointB.x)
diaMinY, diaMaxY = min(diaPointA.y, diaPointB.y),\
max(diaPointA.y, diaPointB.y)
return (x >= diaMinX and x <= diaMaxX and\
y >= diaMinY and y <= diaMaxY)
def __intersectionAligned(self, other):
"""Get the intersection (if any) of two aligned line segments.
'self' and 'other' must be aligned in order for this to
return correct results.
"""
def find(selfPointA, selfPointB, otherPointA, otherPointB):
for interA in (selfPointA, selfPointB):
if not self.__inRect(interA.x, interA.y,
otherPointA, otherPointB):
continue
for interB in (otherPointA, otherPointB,
selfPointA, selfPointB):
if interA == interB:
continue
if not self.__inRect(interB.x, interB.y,
selfPointA, selfPointB) or\
not self.__inRect(interB.x, interB.y,
otherPointA, otherPointB):
continue
return Inter2D(point=Point2D(interA.x, interA.y),
vect=Vect2D(interB.x - interA.x,
interB.y - interA.y),
intersects=True)
return Inter2D(point=Point2D(interA.x, interA.y),
intersects=True)
return None
inter = find(self.pointA, self.pointB,
other.pointA, other.pointB)
if inter is None:
# Swap self and other
inter = find(other.pointA, other.pointB,
self.pointA, self.pointB)
if inter is None:
return Inter2D()
return inter
def __intersectionVertical(self, other):
"""Get the intersection of a vertical line segment 'self'
and a non-vertical line segment 'other'.
'self' must be vertical in order for this to
return correct results.
"""
x = self.pointA.x
y = (x - other.pointA.x) * other.slope + other.pointA.y
return Inter2D(point=Point2D(x, y),
intersects=self.__inRect(x, y, self.pointA, self.pointB) and\
self.__inRect(x, y, other.pointA, other.pointB))
def intersection(self, other):
"""Get the intersection of this line segment
with another line segment.
Returns an Inter2D.
"""
try:
selfVert, otherVert = self.isVertical, other.isVertical
if selfVert and otherVert:
# Both line segments are vertical.
# If they are aligned, they might overlap.
if self.pointA.x == other.pointA.x:
return self.__intersectionAligned(other)
return Inter2D()
elif selfVert:
# self is vertical. other is not vertical.
return self.__intersectionVertical(other)
elif otherVert:
# self is not vertical. other is vertical.
return other.__intersectionVertical(self)
# Get the intersection of two arbitrary
# non-vertical line segments.
selfSlope, otherSlope = self.slope, other.slope
selfInter, otherInter = self.intercept, other.intercept
try:
x = (otherInter - selfInter) / \
(selfSlope - otherSlope)
y = selfSlope * x + selfInter
assert(abs(y - (otherSlope * x + otherInter)) < self.EPSILON)
except ZeroDivisionError:
# The line segments are parallel.
# If they have the same intercept they are aligned
# and might overlap.
if abs(selfInter - otherInter) < self.EPSILON:
return self.__intersectionAligned(other)
return Inter2D()
return Inter2D(point=Point2D(x, y),
intersects=self.__inRect(x, y, self.pointA, self.pointB) and\
self.__inRect(x, y, other.pointA, other.pointB))
except ZeroDivisionError:
pass
return Inter2D()
def __repr__(self):
return "LineSeg2D(pointA=%s, pointB=%s)" % (
self.pointA, self.pointB)
|