def test_constructor_errors(): with pytest.raises(AssertionError): Line('not a point', Point(1, 0)) with pytest.raises(AssertionError): Line(Point(0, 0), 'not a point') with pytest.raises(AssertionError): Line(Point(1, 0), Point(1, 0))
def test_intersection_errors(): with pytest.raises(AssertionError): line.intersection( 'not a line', Line(Point(0, 0), Point(1, 0)), ) with pytest.raises(AssertionError): line.intersection( Line(Point(0, 0), Point(1, 0)), 'not a line', )
def circumcenter(triangle): assert isinstance(triangle, Triangle) # translate to origin t_a = triangle.a - triangle.c t_b = triangle.b - triangle.c # compute points on perpendicular bisectors pb_a_1 = Point.scale(t_a, 0.5) pb_a_2 = pb_a_1 + Point(t_a.y, -t_a.x) pb_b_1 = Point.scale(t_b, 0.5) pb_b_2 = pb_b_1 + Point(t_b.y, -t_b.x) # find intersection of bisectors trans_circumcenter = line.intersection( Line(pb_a_1, pb_a_2), Line(pb_b_1, pb_b_2), ) # if intersection not found, that indicates the triangle was degenerate assert trans_circumcenter is not None # un-translate result return trans_circumcenter + triangle.c
def intersection(line_1, line_2): # based on the solution provided on this wikipedia article: # https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line assert isinstance(line_1, Line) assert isinstance(line_2, Line) interm_a = (line_1.a.x * line_1.b.y) - (line_1.a.y * line_1.b.x) interm_b = (line_2.a.x * line_2.b.y) - (line_2.a.y * line_2.b.x) interm_c = line_1.a.x - line_1.b.x interm_d = line_2.a.x - line_2.b.x interm_e = line_1.a.y - line_1.b.y interm_f = line_2.a.y - line_2.b.y denom = (interm_c * interm_f) - (interm_d * interm_e) if denom == 0: return None return Point( ((interm_a * interm_d) - (interm_b * interm_c)) / denom, ((interm_a * interm_f) - (interm_b * interm_e)) / denom, )
line.intersection( 'not a line', Line(Point(0, 0), Point(1, 0)), ) with pytest.raises(AssertionError): line.intersection( Line(Point(0, 0), Point(1, 0)), 'not a line', ) @pytest.mark.parametrize( ('line_1', 'line_2', 'expected'), [ (Line(Point(2, 0), Point(1, 0)), Line(Point(-1, -1), Point( -2, -2)), Point(0, 0)), # intersect; horizontal x diagonal (Line(Point(0, 2), Point(0, 1)), Line(Point(-1, -1), Point( -2, -2)), Point(0, 0)), # intersect; vertical x diagonal (Line(Point(1, 1), Point(2, 2)), Line(Point(1, -1), Point( 2, -2)), Point(0, 0)), # intersect; diagonal x diagonal 1 (Line(Point(0, 0), Point(6, 10)), Line(Point(0, 2), Point( -3, -1)), Point(3, 5)), # intersect; diagonal x diagonal 2 (Line(Point(1, 1), Point(2, 2)), Line(Point(3, 1), Point( 4, 2)), None), # parallel; diagonal (Line(Point(0, 1), Point(1, 1)), Line(Point(0, 3), Point( 1, 3)), None), # parallel; horizontal (Line(Point(1, 1), Point(1, 2)), Line(Point(3, 1), Point( 3, 2)), None), # parallel; vertical (Line(Point(0, 1), Point(0, 2)), Line(Point(0, 3), Point( 0, 4)), None), # coinciding lines
def test_point_scale(p, c, expected): assert Point.scale(p, c) == expected
def test_sub_type_match(): with pytest.raises(AssertionError): Point(0, 0) - 'not a point'
import pytest from src.geo.point import Point @pytest.mark.parametrize(('p1', 'p2', 'expected'), [ (Point(0, 0), Point(0, 0), True), (Point(1, 0), Point(1, 0), True), (Point(0, 1), Point(0, 1), True), (Point(1, 1), Point(1, 1), True), (Point(5, 3), Point(5, 3), True), (Point(-5, -3), Point(-5, -3), True), (Point(1, 0), Point(0, 0), False), (Point(0, 1), Point(0, 0), False), (Point(0, 0), Point(1, 0), False), (Point(0, 0), Point(0, 1), False), (Point(5, 0), Point(3, 0), False), (Point(0, 5), Point(0, 3), False), (Point(1, 0), Point(-1, 0), False), (Point(0, 1), Point(0, -1), False), (Point(0, 0), 'Not a point', False), ]) def test_point_eq(p1, p2, expected): assert (p1 == p2) == expected assert (p2 == p1) == expected assert (p1 != p2) == (not expected) assert (p2 != p1) == (not expected) @pytest.mark.parametrize(('p1', 'p2', 'expected'), [ (Point(0, 0), Point(0, 0), Point(0, 0)),
def test_circumcenter_errors(): with pytest.raises(AssertionError): triangle.circumcenter('not a triangle') with pytest.raises(AssertionError): triangle.circumcenter(Triangle(Point(0, 0), Point(1, 0), Point(2, 0)))
Point( 0, 0), 'not a point', Point( 1, 0), ) with pytest.raises(AssertionError): Triangle( Point( 0, 0), Point( 1, 0), 'not a point', ) @pytest.mark.parametrize( ('t', 'expected'), [ ( Triangle(Point( 2, 0), Point( 0, 2), Point( 0, 0)), Point( 1, 1) ), # right triangle at origin ( Triangle(Point(-1, 2), Point( 1, 2), Point( 0, 0)), Point( 0, 1.25) ), # acute triangle at origin ( Triangle(Point(-2, 1), Point( 2, 1), Point( 0, 0)), Point( 0, 2.5) ), # obtuse triangle at origin ( Triangle(Point( 1, -3), Point(-1, -1), Point(-1, -3)), Point( 0, -2) ), # right triangle translated ( Triangle(Point( 3, 7), Point( 5, 7), Point( 4, 5)), Point( 4, 6.25) ), # acute triangle translated ( Triangle(Point( 5, 1), Point( 9, 1), Point( 7, 0)),