Skip to content

Commit b7a63fb

Browse files
committed
Update to python 3.6 from 3.4
Remove the _approx functions that I was using to delay updating * pygorithm/geometry/axisall.py - remove _approx * pygorithm/geometry/line2.py - remove _approx * pygorithm/geometry/polygon2.py - remove _approx
1 parent 05adb5d commit b7a63fb

File tree

3 files changed

+18
-70
lines changed

3 files changed

+18
-70
lines changed

pygorithm/geometry/axisall.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ def __init__(self, axis, point1, point2):
6464
self.min = min(point1, point2)
6565
self.max = max(point1, point2)
6666

67-
@staticmethod
68-
def _approx(a, b):
69-
"""
70-
Same as math.isclose but supports python < 3.5
71-
72-
:param a: first numeric
73-
:type a: :class:`numbers.Number`
74-
:param b: second numeric
75-
:type b: :class:`numbers.Number`
76-
:returns: if the are close
77-
:rtype: bool
78-
"""
79-
80-
if hasattr(math, 'isclose'):
81-
return math.isclose(a, b)
82-
return abs(a - b) <= 1e-09 * max(abs(a), abs(b))
83-
8467
@staticmethod
8568
def intersects(line1, line2):
8669
"""
@@ -108,9 +91,9 @@ def intersects(line1, line2):
10891
:rtype: (bool, bool)
10992
"""
11093

111-
if AxisAlignedLine._approx(line1.max, line2.min):
94+
if math.isclose(line1.max, line2.min):
11295
return True, False
113-
elif AxisAlignedLine._approx(line1.min, line2.max):
96+
elif math.isclose(line1.min, line2.max):
11497
return True, False
11598
elif line1.max < line2.min:
11699
return False, False
@@ -146,9 +129,9 @@ def find_intersection(line1, line2):
146129
:rtype: (bool, (:class:`numbers.Number` or None, :class:`numbers.Number`, :class:`numbers.Number`) or None)
147130
"""
148131

149-
if AxisAlignedLine._approx(line1.max, line2.min):
132+
if math.isclose(line1.max, line2.min):
150133
return True, (None, line2.min, line2.min)
151-
elif AxisAlignedLine._approx(line1.min, line2.max):
134+
elif math.isclose(line1.min, line2.max):
152135
return True, (None, line1.min, line1.min)
153136
elif line1.max < line2.min or line2.max < line1.min:
154137
return False, None
@@ -183,7 +166,7 @@ def contains_point(line, point):
183166
:rtype: (bool, bool)
184167
"""
185168

186-
if AxisAlignedLine._approx(line.min, point) or AxisAlignedLine._approx(line.max, point):
169+
if math.isclose(line.min, point) or math.isclose(line.max, point):
187170
return True, False
188171
elif point < line.min or point > line.max:
189172
return False, False

pygorithm/geometry/line2.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -377,23 +377,6 @@ def calculate_y_intercept(self, offset):
377377
# y = mx + b -> b = y - mx
378378
return self.start.y + offset.y - self.slope * (self.start.x + offset.x)
379379

380-
@staticmethod
381-
def _approx(a, b):
382-
"""
383-
Same as math.isclose but supports python < 3.5
384-
385-
:param a: first numeric
386-
:type a: :class:`numbers.Number`
387-
:param b: second numeric
388-
:type b: :class:`numbers.Number`
389-
:returns: if the are close
390-
:rtype: bool
391-
"""
392-
393-
if hasattr(math, 'isclose'):
394-
return math.isclose(a, b)
395-
return abs(a - b) <= 1e-09 * max(abs(a), abs(b))
396-
397380
@staticmethod
398381
def are_parallel(line1, line2):
399382
"""
@@ -412,7 +395,7 @@ def are_parallel(line1, line2):
412395
if line1.vertical and line2.vertical:
413396
return True
414397

415-
return Line2._approx(line1.slope, line2.slope)
398+
return math.isclose(line1.slope, line2.slope)
416399

417400
@staticmethod
418401
def find_intersection(line1, line2, offset1 = None, offset2 = None):
@@ -467,7 +450,7 @@ def find_intersection(line1, line2, offset1 = None, offset2 = None):
467450

468451
if line1.vertical and line2.vertical:
469452
# Two vertical lines
470-
if not Line2._approx(l1_st_x, l2_st_x):
453+
if not math.isclose(l1_st_x, l2_st_x):
471454
return False, False, None
472455

473456
aal1 = axisall.AxisAlignedLine(None, l1_st_y, l1_en_y)
@@ -484,7 +467,7 @@ def find_intersection(line1, line2, offset1 = None, offset2 = None):
484467

485468
if line1.horizontal and line2.horizontal:
486469
# Two horizontal lines
487-
if not Line2._approx(l1_st_y, l2_st_y):
470+
if not math.isclose(l1_st_y, l2_st_y):
488471
return False, False, None
489472

490473
aal1 = axisall.AxisAlignedLine(None, l1_st_x, l1_en_x)
@@ -503,7 +486,7 @@ def find_intersection(line1, line2, offset1 = None, offset2 = None):
503486
# Two non-vertical, non-horizontal, parallel lines
504487
yintr1 = line1.calculate_y_intercept(offset1)
505488
yintr2 = line2.calculate_y_intercept(offset2)
506-
if not Line2._approx(yintr1, yintr2):
489+
if not math.isclose(yintr1, yintr2):
507490
return False, False, None
508491

509492
axis = line1.axis
@@ -544,7 +527,7 @@ def unshift_vec(vec):
544527

545528
pt = vector2.Vector2(l1_st_x, l2_st_y)
546529

547-
if Line2._approx(l2_st_y, l1_min) or Line2._approx(l2_st_y, l2_max) or Line2._approx(l1_st_x, l2_min) or Line2._approx(l2_st_y, l2_max):
530+
if math.isclose(l2_st_y, l1_min) or math.isclose(l2_st_y, l2_max) or math.isclose(l1_st_x, l2_min) or math.isclose(l2_st_y, l2_max):
548531
return True, False, pt
549532
else:
550533
return False, True, pt
@@ -556,7 +539,7 @@ def unshift_vec(vec):
556539
l1_min = min(l1_st_y, l1_en_y) if offset1 is not None else line1.min_y
557540
l1_max = max(l1_st_y, l1_en_y) if offset1 is not None else line1.max_y
558541

559-
if Line2._approx(line2_y_at_line1_x, l1_min) or Line2._approx(line2_y_at_line1_x, l1_max):
542+
if math.isclose(line2_y_at_line1_x, l1_min) or math.isclose(line2_y_at_line1_x, l1_max):
560543
return True, False, vector2.Vector2(l1_st_x, line2_y_at_line1_x)
561544
elif line2_y_at_line1_x < l1_min or line2_y_at_line1_x > l2_max:
562545
return False, False, None
@@ -571,7 +554,7 @@ def unshift_vec(vec):
571554
l1_min = min(l1_st_x, l1_en_x) if offset1 is not None else line1.min_x
572555
l1_max = max(l1_st_x, l1_en_x) if offset1 is not None else line1.max_x
573556

574-
if Line2._approx(line2_x_at_line1_y, l1_min) or Line2._approx(line2_x_at_line1_y, l1_max):
557+
if math.isclose(line2_x_at_line1_y, l1_min) or math.isclose(line2_x_at_line1_y, l1_max):
575558
return True, False, vector2.Vector2(line2_x_at_line1_y, l1_st_y)
576559
elif line2_x_at_line1_y < l1_min or line2_x_at_line1_y > l1_max:
577560
return False, False, None
@@ -593,8 +576,8 @@ def unshift_vec(vec):
593576
# Some caution needs to be taken here to ensure we do approximately before range
594577
# checks. It's possible for _approx(a, b) to be True and a < b to be True
595578

596-
on_edge1 = Line2._approx(intr_x, l1_st_x) or Line2._approx(intr_x, l1_en_x)
597-
on_edge2 = Line2._approx(intr_x, l2_st_x) or Line2._approx(intr_x, l2_en_x)
579+
on_edge1 = math.isclose(intr_x, l1_st_x) or math.isclose(intr_x, l1_en_x)
580+
on_edge2 = math.isclose(intr_x, l2_st_x) or math.isclose(intr_x, l2_en_x)
598581

599582
if on_edge1 and on_edge2:
600583
intr_y = line1.slope * intr_x + yintr1

pygorithm/geometry/polygon2.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, points, suppress_errors = False):
115115

116116
if not suppress_errors:
117117
for prev_pt in self.points:
118-
if Polygon2._approx(prev_pt.x, act_pt.x) and Polygon2._approx(prev_pt.y, act_pt.y):
118+
if math.isclose(prev_pt.x, act_pt.x) and math.isclose(prev_pt.y, act_pt.y):
119119
raise ValueError('Repeated points! points={} (repeated={})'.format(points, act_pt))
120120

121121

@@ -133,7 +133,7 @@ def __init__(self, points, suppress_errors = False):
133133
norm.x *= -1
134134
norm.y *= -1
135135

136-
already_contains = next((v for v in self.normals if Polygon2._approx(v.x, norm.x) and Polygon2._approx(v.y, norm.y)), None)
136+
already_contains = next((v for v in self.normals if math.isclose(v.x, norm.x) and math.isclose(v.y, norm.y)), None)
137137
if already_contains is None:
138138
self.normals.append(norm)
139139

@@ -164,24 +164,6 @@ def __init__(self, points, suppress_errors = False):
164164
if cross_product < -1e-09:
165165
raise ValueError('Detected concavity at index {} - {} cross {} = {}\nself={}'.format(middlepointin, vec1, vec2, cross_product, str(self)))
166166

167-
168-
@staticmethod
169-
def _approx(a, b):
170-
"""
171-
Same as math.isclose but supports python < 3.5
172-
173-
:param a: first numeric
174-
:type a: :class:`numbers.Number`
175-
:param b: second numeric
176-
:type b: :class:`numbers.Number`
177-
:returns: if the are close
178-
:rtype: bool
179-
"""
180-
181-
if hasattr(math, 'isclose'):
182-
return math.isclose(a, b)
183-
return abs(a - b) <= 1e-09 * max(abs(a), abs(b))
184-
185167
@classmethod
186168
def from_regular(cls, sides, length, start_rads = None, start_degs = None, center = None):
187169
"""
@@ -438,7 +420,7 @@ def contains_point(polygon, offset, point):
438420
cross = vec1.cross(vec2)
439421
_previous = curr
440422

441-
if Polygon2._approx(cross, 0):
423+
if math.isclose(cross, 0):
442424
return True, False
443425

444426
if cross > 0:
@@ -494,7 +476,7 @@ def find_intersection(poly1, poly2, offset1, offset2, find_mtv = True):
494476
for n in poly2.normals:
495477
found = False
496478
for old_n in poly1.normals:
497-
if Polygon2._approx(n.x, old_n.x) and Polygon2._approx(n.y, old_n.y):
479+
if math.isclose(n.x, old_n.x) and math.isclose(n.y, old_n.y):
498480
found = True
499481
break
500482
if not found:

0 commit comments

Comments
 (0)