Skip to content

Commit c877f6f

Browse files
authored
Merge pull request OmkarPathak#73 from OmkarPathak/dev
Cleanup tests, add line2.contains_point
2 parents d11440e + 633400b commit c877f6f

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

imgs/test_geometry/test_extrapolated_intersection/as_test_one_moving_one_stationary_along_path_no_intr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ def setup_fig01():
1313
def setup_fig02():
1414
fig, ax, renderer = create_newfig('{}02'.format(func_code), xlim=(-2, 12), ylim=(-1, 10))
1515

16-
create_moving_polygon(fig, ax, renderer, ((11, 5), (8, 8), (7, 7), (6, 3), (9, 3), (11, 5)), (-1, -3))
17-
create_still_polygon(fig, ax, renderer, ((3.5, 8.5), (1.5, 8.5), (1.5, 8.5), (-0.5, 7.5), (0.5, 3.5), (1.5, 2.5), (4.5, 2.5), (5.5, 6.5)))
16+
create_moving_polygon(fig, ax, renderer, ((11, 5), (8, 8), (7, 7), (6, 3), (9, 3)), (-1, -3))
17+
create_still_polygon(fig, ax, renderer, ((3.5, 8.5), (1.5, 8.5), (-0.5, 7.5), (0.5, 3.5), (1.5, 2.5), (4.5, 2.5), (5.5, 6.5)))
1818
return fig, ax, '{}02_{}'.format(func_code, func_name)
1919

2020
def setup_fig03():
2121
fig, ax, renderer = create_newfig('{}03'.format(func_code), xlim=(-3, 9), ylim=(-1, 15))
2222

2323
create_moving_polygon(fig, ax, renderer, ((0.5, 9.0), (-1.5, 8.0), (-1.5, 6.0), (1.5, 5.0), (2.5, 5.0), (2.5, 9.0)), (0, 5))
24-
create_still_polygon(fig, ax, renderer, ((7.0, 6.0), (4.0, 5.0), (4.0, 3.0), (6.0, 2.0), (8.0, 3.0), (7.0, 6.0)))
24+
create_still_polygon(fig, ax, renderer, ((7.0, 6.0), (4.0, 5.0), (4.0, 3.0), (6.0, 2.0), (8.0, 3.0)))
2525
return fig, ax, '{}03_{}'.format(func_code, func_name)
2626

2727
def setup_fig04():
2828
fig, ax, renderer = create_newfig('{}04'.format(func_code), xlim=(-2, 12), ylim=(-3, 10))
2929

3030
create_moving_polygon(fig, ax, renderer, ((5.5, 4.5), (3.5, -1.5), (9.5, -1.5), (10.5, 0.5)), (-4, 0))
31-
create_still_polygon(fig, ax, renderer, ((7.5, 8.5), (7.5, 8.5), (6.5, 5.5), (7.5, 4.5), (9.5, 4.5), (10.5, 7.5)))
31+
create_still_polygon(fig, ax, renderer, ((7.5, 8.5), (6.5, 5.5), (7.5, 4.5), (9.5, 4.5), (10.5, 7.5)))
3232
return fig, ax, '{}04_{}'.format(func_code, func_name)
3333

3434
run_or_export(setup_fig01, setup_fig02, setup_fig03, setup_fig04)

imgs/test_geometry/test_extrapolated_intersection/at_test_one_moving_one_stationary_along_path_touching.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
def setup_fig01():
77
fig, ax, renderer = create_newfig('{}01'.format(func_code), xlim=(-1, 16), ylim=(-1, 11))
8-
create_moving_polygon(fig, ax, renderer, ((3, 10), (2, 10), (1, 8), (2, 6), (2, 6), (5, 6), (7, 8)), (8, 0))
8+
create_moving_polygon(fig, ax, renderer, ((3, 10), (2, 10), (1, 8), (2, 6), (5, 6), (7, 8)), (8, 0))
99
create_still_polygon(fig, ax, renderer, ((10, 5), (8, 6), (6, 5), (6, 4), (7, 2), (10, 4)))
1010

1111
return fig, ax, '{}01_{}'.format(func_code, func_name)
1212

1313
def setup_fig02():
1414
fig, ax, renderer = create_newfig('{}02'.format(func_code), xlim=(-8, 7), ylim=(-2, 12))
15-
create_moving_polygon(fig, ax, renderer, ((5, 5), (4, 5), (2, 0), (4, -1), (6, 0), (5, 5)), (-5, 0))
15+
create_moving_polygon(fig, ax, renderer, ((5, 5), (4, 5), (2, 0), (4, -1), (6, 0)), (-5, 0))
1616
create_still_polygon(fig, ax, renderer, ((2, 11), (-2, 8), (2, 5), (3, 6), (3, 11)))
1717

1818
return fig, ax, '{}02_{}'.format(func_code, func_name)
@@ -26,8 +26,8 @@ def setup_fig03():
2626

2727
def setup_fig04():
2828
fig, ax, renderer = create_newfig('{}04'.format(func_code), xlim=(-1, 14), ylim=(-3, 11))
29-
create_moving_polygon(fig, ax, renderer, ((4.5, 4), (0.5, 2), (0.5, 1), (0.5, 0), (2.5, -2), (3.5, -2), (5.5, -1), (4.5, 4)), (6.7492919018596025, 4.29500393754702))
30-
create_still_polygon(fig, ax, renderer, ((8, 8.5), (5, 9.5), (4, 8.5), (6, 5.5), (6, 5.5)))
29+
create_moving_polygon(fig, ax, renderer, ((4.5, 4), (0.5, 2), (0.5, 1), (0.5, 0), (2.5, -2), (3.5, -2), (5.5, -1)), (6.7492919018596025, 4.29500393754702))
30+
create_still_polygon(fig, ax, renderer, ((8, 8.5), (5, 9.5), (4, 8.5), (6, 5.5)))
3131

3232
return fig, ax, '{}04_{}'.format(func_code, func_name)
3333

pygorithm/geometry/line2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,51 @@ def are_parallel(line1, line2):
397397

398398
return math.isclose(line1.slope, line2.slope)
399399

400+
@staticmethod
401+
def contains_point(line, point, offset = None):
402+
"""
403+
Determine if the line contains the specified point.
404+
405+
Optionally, specify an offset for the line. Being
406+
on the line is determined using `math.isclose`.
407+
408+
:param line: the line
409+
:type line: :class:`pygorithm.geometry.line2.Line2`
410+
:param point: the point
411+
:type point: :class:`pygorithm.geometry.vector2.Vector2`
412+
:param offset: the offset of the line or None for the origin
413+
:type offset: :class:`pygorithm.geometry.vector2.Vector2` or None
414+
:returns: if the point is on the line
415+
:rtype: bool
416+
"""
417+
418+
if line.vertical:
419+
x = line.start.x + offset.x if offset is not None else line.start.x
420+
if not math.isclose(point.x, x, abs_tol=1e-07):
421+
return False
422+
ymin = line.min_y + offset.y if offset is not None else line.min_y
423+
ymax = line.max_y + offset.y if offset is not None else line.max_y
424+
if math.isclose(point.y, ymin, abs_tol=1e-07) or math.isclose(point.y, ymax, abs_tol=1e-07):
425+
return True
426+
return point.y > ymin and point.y < ymax
427+
428+
xmin = line.min_x + offset.x if offset is not None else line.min_x
429+
xmax = line.max_x + offset.x if offset is not None else line.max_x
430+
431+
if not (math.isclose(point.x, xmin, abs_tol=1e-07) or point.x > xmin):
432+
return False
433+
434+
if not (math.isclose(point.x, xmax, abs_tol=1e-07) or point.x < xmax):
435+
return False
436+
437+
ystart = line.start.y + offset.y if offset is not None else line.start.y
438+
if line.horizontal:
439+
return math.isclose(ystart, point.y, abs_tol=1e-07)
440+
441+
yint = line.calculate_y_intercept(offset)
442+
yatx = line.slope * point.x + yint
443+
return math.isclose(point.y, yatx, abs_tol=1e-07)
444+
400445
@staticmethod
401446
def find_intersection(line1, line2, offset1 = None, offset2 = None):
402447
"""

tests/test_geometry.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ def test_are_parallel(self):
340340

341341
_line = line2.Line2(vector2.Vector2(5, 4), vector2.Vector2(3, 1))
342342
self.assertTrue(line2.Line2.are_parallel(self.line_1_1_3_4, _line))
343+
344+
def test_contains_point(self):
345+
self.assertFalse(line2.Line2.contains_point(self.line_origin_1_1, self.vec_1_1, self.vec_1_2))
346+
self.assertTrue(line2.Line2.contains_point(self.line_origin_1_1, self.vec_1_1))
347+
self.assertTrue(line2.Line2.contains_point(self.line_1_1_3_4, vector2.Vector2(2, 2.5)))
348+
self.assertFalse(line2.Line2.contains_point(self.line_1_1_3_4, vector2.Vector2(2, 2.5), vector2.Vector2(1, 0)))
349+
self.assertTrue(line2.Line2.contains_point(line2.Line2(vector2.Vector2(-3, -3), vector2.Vector2(6, 3)), vector2.Vector2(3, 1)))
343350

344351
def _find_intr_fuzzer(self, v1, v2, v3, v4, exp_touching, exp_overlap, exp_intr, number_fuzzes = 3):
345352
for i in range(number_fuzzes):
@@ -1806,7 +1813,7 @@ def _calc_one_moving_one_stat_distlimit_fuzzer(self, poly1tup, vel1tuporvec, pol
18061813
return intr, msg
18071814

18081815
def test_one_moving_one_stationary_distlimit_no_intr(self):
1809-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1816+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
18101817

18111818
# am01
18121819
intr, msg = fn(((0, 3), (1, 3), (1, 2), (0, 2)), (1, 0), ((2, 0), (3, 1), (4, 0)), 4)
@@ -1821,18 +1828,18 @@ def test_one_moving_one_stationary_distlimit_no_intr(self):
18211828
self.assertFalse(intr, msg=msg)
18221829

18231830
# am04
1824-
intr, msg = fn(((4, 1.75), (5, 2.5), (6, 2.5), (4, 1.25)), (-2, 1), ((4.175), (5, 2.5), (6, 2.5), (4, 1.25)), self.pt(-2, 1).magnitude())
1831+
intr, msg = fn(((4, 1.75), (5, 2.5), (6, 2.5), (4, 1.25)), (-2, 1), ((4, 1.75), (5, 2.5), (6, 2.5), (4, 1.25)), self.pt(-2, 1).magnitude())
18251832
self.assertFalse(intr, msg=msg)
18261833

18271834
def test_one_moving_one_stationary_distlimit_touching(self):
1828-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1835+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
18291836

18301837
# an01
1831-
intr, msg = fn(((0, 3), (1, 3), (1, 2), (0, 2)), (5, -1.25), ((3, 1), (4, 1), (3, 0), (4, 0)), self.pt(5, -1.25).magnitude())
1838+
intr, msg = fn(((0, 3), (1, 3), (1, 2), (0, 2)), (5, -1.25), ((3, 1), (4, 1), (4, 0), (3, 0)), self.pt(5, -1.25).magnitude())
18321839
self.assertFalse(intr, msg=msg)
18331840

18341841
# an02
1835-
intr, msg = fn(((1, 3), (2, 3), (1, 2), (2, 2)), (4, 0), ((1, 0), (2, 1), (4, 2), (5, 0)), 4)
1842+
intr, msg = fn(((1, 3), (2, 3), (2, 2), (1, 2)), (4, 0), ((1, 0), (2, 1), (4, 2), (5, 0)), 4)
18361843
self.assertFalse(intr, msg=msg)
18371844

18381845
# an03
@@ -1844,7 +1851,7 @@ def test_one_moving_one_stationary_distlimit_touching(self):
18441851
self.assertFalse(intr, msg=msg)
18451852

18461853
def test_one_moving_one_stationary_distlimit_intr_at_start(self):
1847-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1854+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
18481855

18491856
# ao01
18501857
intr, msg = fn(((3, 3), (4, 3), (4, 1), (3, 1)), (2, 0), ((3, 1), (4, 1), (4, 0), (3, 0)), 2)
@@ -1863,7 +1870,7 @@ def test_one_moving_one_stationary_distlimit_intr_at_start(self):
18631870
self.assertTrue(intr, msg=msg)
18641871

18651872
def test_one_moving_one_stationary_distlimit_intr_later(self):
1866-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1873+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
18671874

18681875
# ap01
18691876
intr, msg = fn(((2, 3), (3, 3), (3, 2), (2, 2)), (5, 4), ((3, 5), (4, 5), (4, 4), (3, 4)), self.pt(5, 4).magnitude())
@@ -1882,7 +1889,7 @@ def test_one_moving_one_stationary_distlimit_intr_later(self):
18821889
self.assertTrue(intr, msg=msg)
18831890

18841891
def test_one_moving_one_stationary_distlimit_touch_at_limit(self):
1885-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1892+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
18861893

18871894
# aq01
18881895
intr, msg = fn(((0, 1), (1, 1), (1, 0), (0, 0)), (4, 3), ((3, 5), (4, 5), (4, 4), (3, 4)), self.pt(4, 3).magnitude())
@@ -1901,7 +1908,7 @@ def test_one_moving_one_stationary_distlimit_touch_at_limit(self):
19011908
self.assertFalse(intr, msg=msg)
19021909

19031910
def test_one_moving_one_stationary_distlimit_intr_after_limit(self):
1904-
fn = _calc_one_moving_one_stat_distlimit_fuzzer
1911+
fn = self._calc_one_moving_one_stat_distlimit_fuzzer
19051912

19061913
# ar01
19071914
intr, msg = fn(((0, 1), (1, 1), (1, 0), (0, 0)), (4, 3), ((5.5, 5.5), (6.5, 5.5), (6.5, 4.5), (5.5, 4.5)), self.pt(4, 3).magnitude())
@@ -1923,7 +1930,9 @@ def test_one_moving_one_stationary_distlimit_intr_after_limit(self):
19231930
def _calc_one_moving_one_stat_along_path_fuzzer(self, poly1tup, pos1tuporvec, pos2tuporvec, poly2tup, reverse=False):
19241931
# i generated a few polygons in the wrong order when making these tests
19251932
if reverse:
1933+
poly1tup = list(p for p in poly1tup)
19261934
poly1tup.reverse()
1935+
poly2tup = list(p for p in poly2tup)
19271936
poly2tup.reverse()
19281937

19291938
fn = self.extr_intr.calculate_one_moving_one_stationary_along_path
@@ -1956,34 +1965,34 @@ def test_one_moving_one_stationary_along_path_no_intr(self):
19561965
self.assertFalse(intr, msg=msg)
19571966

19581967
# as02
1959-
intr, msg = fn(((11, 5), (8, 8), (7, 7), (6, 3), (9, 3), (11, 5)), (0, 0), (-1, -3), ((3.5, 8.5), (1.5, 8.5), (1.5, 8.5), (-0.5, 7.5), (0.5, 3.5), (1.5, 2.5), (4.5, 2.5), (5.5, 6.5)), reverse=True)
1968+
intr, msg = fn(((11, 5), (8, 8), (7, 7), (6, 3), (9, 3)), (0, 0), (-1, -3), ((3.5, 8.5), (1.5, 8.5), (-0.5, 7.5), (0.5, 3.5), (1.5, 2.5), (4.5, 2.5), (5.5, 6.5)), reverse=True)
19601969
self.assertFalse(intr, msg=msg)
19611970

19621971
# as03
1963-
intr, msg = fn(((0.5, 9.0), (-1.5, 8.0), (-1.5, 6.0), (1.5, 5.0), (2.5, 5.0), (2.5, 9.0)), (0, 0), (0, 5), ((7.0, 6.0), (4.0, 5.0), (4.0, 3.0), (6.0, 2.0), (8.0, 3.0), (7.0, 6.0)), reverse=True)
1972+
intr, msg = fn(((0.5, 9.0), (-1.5, 8.0), (-1.5, 6.0), (1.5, 5.0), (2.5, 5.0), (2.5, 9.0)), (0, 0), (0, 5), ((7.0, 6.0), (4.0, 5.0), (4.0, 3.0), (6.0, 2.0), (8.0, 3.0)), reverse=True)
19641973
self.assertFalse(intr, msg=msg)
19651974

19661975
# as04
1967-
intr, msg = fn(((5.5, 4.5), (3.5, -1.5), (9.5, -1.5), (10.5, 0.5)), (0, 0), (-4, 0), ((7.5, 8.5), (7.5, 8.5), (6.5, 5.5), (7.5, 4.5), (9.5, 4.5), (10.5, 7.5)), reverse=True)
1976+
intr, msg = fn(((5.5, 4.5), (3.5, -1.5), (9.5, -1.5), (10.5, 0.5)), (0, 0), (-4, 0), ((7.5, 8.5), (6.5, 5.5), (7.5, 4.5), (9.5, 4.5), (10.5, 7.5)), reverse=True)
19681977
self.assertFalse(intr, msg=msg)
19691978

19701979
def test_one_moving_one_stationary_along_path_touching(self):
19711980
fn = self._calc_one_moving_one_stat_along_path_fuzzer
19721981

19731982
# at01
1974-
intr, msg = fn(((3, 10), (2, 10), (1, 8), (2, 6), (2, 6), (5, 6), (7, 8)), (0, 0), (8, 0), ((10, 5), (8, 6), (6, 5), (6, 4), (7, 2), (10, 4)), reverse=True)
1983+
intr, msg = fn(((3, 10), (2, 10), (1, 8), (2, 6), (5, 6), (7, 8)), (0, 0), (8, 0), ((10, 5), (8, 6), (6, 5), (6, 4), (7, 2), (10, 4)), reverse=True)
19751984
self.assertFalse(intr, msg=msg)
19761985

19771986
# at02
1978-
intr, msg = fn(((5, 5), (4, 5), (2, 0), (4, -1), (6, 0), (5, 5)), (0, 0), (-5, 0), ((2, 11), (-2, 8), (2, 5), (3, 6), (3, 11)), reverse=True)
1987+
intr, msg = fn(((5, 5), (4, 5), (2, 0), (4, -1), (6, 0)), (0, 0), (-5, 0), ((2, 11), (-2, 8), (2, 5), (3, 6), (3, 11)), reverse=True)
19791988
self.assertFalse(intr, msg=msg)
19801989

19811990
# at03
1982-
intr, msg = fn((9.5, 8.5), (8.5, 7.5), (9.5, 5), (10.5, 7)), (0, 0), (-9, -9), ((2, 5), (-1, 5), (-2, 3), (2, 1), (3, 2)), reverse=True)
1991+
intr, msg = fn(((9.5, 8.5), (8.5, 7.5), (9.5, 5), (10.5, 7)), (0, 0), (-9, -9), ((2, 5), (-1, 5), (-2, 3), (2, 1), (3, 2)), reverse=True)
19831992
self.assertFalse(intr, msg=msg)
19841993

19851994
# at04
1986-
intr, msg = fn(((4.5, 4), (0.5, 2), (0.5, 1), (0.5, 0), (2.5, -2), (3.5, -2), (5.5, -1), (4.5, 4)), (0, 0), (6.7492919018596025, 4.29500393754702), ((8, 8.5), (5, 9.5), (4, 8.5), (6, 5.5), (6, 5.5)), reverse=True)
1995+
intr, msg = fn(((4.5, 4), (0.5, 2), (0.5, 1), (0.5, 0), (2.5, -2), (3.5, -2), (5.5, -1)), (0, 0), (6.7492919018596025, 4.29500393754702), ((8, 8.5), (5, 9.5), (4, 8.5), (6, 5.5)), reverse=True)
19871996
self.assertFalse(intr, msg=msg)
19881997

19891998
def test_one_moving_one_stationary_along_path_intr_at_start(self):

0 commit comments

Comments
 (0)