def test_point_belongs_to_line_negative(): l1 = line_segment_from_coordinates(5, 0, 10, 3) p1 = Point(20, 6) assert not point_belongs_to_line(l1, p1) l3 = line_segment_from_coordinates(0, 0, 0, 10) p3 = Point(1, 10) assert not point_belongs_to_line(l3, p3)
def test_line_segment_equal(): p1 = Point(0, 1) p2 = Point(10, 20) l1 = LineSegment(p1, p2) l2 = LineSegment(p1, p2) l3 = LineSegment(p2, p1) assert l1 == l2 assert l1 == l3
def test_vector_angle(): v1 = Vector(Point(0, 0), Point(0, 10)) v2 = Vector(Point(0, 0), Point(20, 0)) assert v1.angle(v2) == 90 v2 = Vector(Point(0, 0), Point(0, -10)) assert v1.angle(v2) == 180 v2 = Vector(Point(0, 0), Point(-10, 0)) assert v1.angle(v2) == 90 # even it should be 270 v2 = Vector(Point(0, 0), Point(0, 10)) assert v1.angle(v2) == 0
def test_point_belongs_to_line_positive(): l1 = line_segment_from_coordinates(5, 0, 15, 6) p1 = Point(10, 3) assert point_belongs_to_line(l1, p1) l2 = line_segment_from_coordinates(0, 0, 0, 10) p2 = Point(0, 5) assert point_belongs_to_line(l2, p2) # Point outside line segment but on the same line l3 = line_segment_from_coordinates(5, 0, 10, 3) p3 = Point(15, 6) assert point_belongs_to_line(l3, p3) l4 = line_segment_from_coordinates(0, 0, 0, 10) p4 = Point(0, 12) assert point_belongs_to_line(l4, p4)
def add_point_to_polygon(self, event): point = Point(event.x, event.y) self.plot_point(point, radius=2) if len(self.polygon_points): self.answer_label['text'] = '' prev_point = self.polygon_points[-1] self.plot_line(point, prev_point) self.polygon_points.append(point)
def point_belongs_to_a_polygon(z_point: Point, polygon: List[Point]): # just a sanity check for the easiest case for polygon_point in polygon: if polygon_point == z_point: return True # test point the same as a polygon point # check intersection of each polygon edge and point-infinity line distant_coordinate = int(sys.maxsize**.5 / 1000) point_ray = LineSegment(z_point, Point(distant_coordinate, z_point.y)) # identify intersection point for each edge intersected_points = [] for idx in range(len(polygon)): next_idx = (idx + 1) % len(polygon) edge = LineSegment(polygon[idx], polygon[next_idx]) intersection_point = lines_segment_intersection_point(point_ray, edge) if intersection_point: if Point(*intersection_point) == z_point: return True intersected_points.append(intersection_point) return len(intersected_points) % 2
def test_point_belongs_to_a_polygon_false(): """ _________ | /\\ | | / \\ | |/ \\| """ polygon = [ Point(0, 0), Point(0, 10), Point(10, 10), Point(10, 0), Point(5, 9), ] # point lies on "inside" concave part of polygon p1 = Point(5, 5) assert not point_belongs_to_a_polygon(p1, polygon) p2 = Point(5, 0) assert not point_belongs_to_a_polygon(p2, polygon) p3 = Point(12, 0) assert not point_belongs_to_a_polygon(p3, polygon)
def test_PointsPolygonChecker_false(): """ _________ | | | | | | | | --------- """ points_polygon_checker = PointsPolygonChecker([ Point(0, 0), Point(10, 0), Point(10, 10), Point(0, 10), ]) # point lies on "inside" concave part of polygon assert not points_polygon_checker(Point(11, 11)) assert not points_polygon_checker(Point(5, 43)) assert not points_polygon_checker(Point(12, 0))
def test_point_belongs_to_a_polygon_true(): """ _________ | | | | | | | | --------- """ polygon = [Point(0, 0), Point(0, 10), Point(10, 10), Point(10, 0)] # point exist inside tha polygon p1 = Point(5, 5) assert point_belongs_to_a_polygon(p1, polygon) # point belongs to a polygon edge p2 = Point(0, 5) assert point_belongs_to_a_polygon(p2, polygon) # point is the same as on of polygon edges p3 = Point(10, 10) assert point_belongs_to_a_polygon(p3, polygon)
def test_PointsPolygonChecker_true(): """ _________ | | | | | | | | --------- """ points_polygon_checker = PointsPolygonChecker([ Point(0, 0), Point(10, 0), Point(10, 10), Point(0, 10), ]) # point exist inside tha polygon assert points_polygon_checker(Point(5, 5)) # point belongs to a polygon edge assert points_polygon_checker(Point(0, 5)) # point is the same as on of polygon edges assert points_polygon_checker(Point(10, 10))
def get_triangle_centroid(p1: Point, p2: Point, p3: Point): centroid_x = (p1.x + p2.x + p3.x) / 3 centroid_y = (p1.y + p2.y + p3.y) / 3 return Point(centroid_x, centroid_y)
def test_point__neg__(): assert -Point(10, 20) == Point(-10, -20)
def test_points_and_line_segments_turns_are_the_same(): p1 = Point(5, 10) p2 = Point(5, 20) p3 = Point(15, 15) assert points_turn_value(p1, p2, p3) == line_segments_turn_value( LineSegment(p1, p2), LineSegment(p1, p3))
def test_turn_left(): assert 1 == turn(Point(5, 10), Point(5, 20), Point(0, 15))
def test_vectors_properties(): Vector(Point(10, 0), Point(0, 0)).len == 10 Vector(Point(0, 10), Point(0, 0)).len == 10 Vector(Point(0, 0), Point(10, 0)).len == 10 Vector(Point(0, 0), Point(0, 10)).len == 10 Vector(Point(0, 0), Point(3, 4)).len == 5 Vector(Point(1, 2), Point(4, 6)).len == 5
def test_points_equal(): assert Point(10, 20) == Point(10, 20) assert Point(10, 20) == Point(10.0, 20.0) assert Point(2.13, 14.17) == Point(2.13, 14.17)
def test_line_segment_impossible_to_create(): p1 = Point(0, 1) with pytest.raises(ValueError): LineSegment(p1, p1)
def test_line_segment_not_equal(): l1 = LineSegment(Point(1, 2), Point(3, 4)) l2 = LineSegment(Point(5, 6), Point(7, 8)) assert l1 != l2
def test_point__add__(): assert Point(12, 15) == (Point(2, 10) + Point(10, 5))
def test_polar_angle(): ans1 = polar_angle(Point(0, 0), Point(-1, -1)) ans2 = polar_angle(Point(1, 1), Point(0, 0)) assert ans1 == ans2 == pytest.approx(-2.356194490192345)
def test_line_segment_from_coordinates(): p1_x, p1_y, p2_x, p2_y = list(range(1, 5)) line_1 = line_segment_from_coordinates(p1_x, p1_y, p2_x, p2_y) line_2 = LineSegment(Point(x=p1_x, y=p1_y), Point(x=p2_x, y=p2_y)) assert line_1 == line_2
def plot_point_event(self, event): self.plot_point(event, color='black', radius=3) self.ploted_points.append(Point(event.x, event.y))
def test_points_not_equal(): assert Point(0, 1) != Point(0, 2) assert Point(1, 0) != Point(2, 0)
def plot_z_point(self, event): if self.z_point: self.plot_point(self.z_point, color='white', radius=3) point = Point(event.x, event.y) self.z_point = point self.plot_point(point, color="green", radius=3)
def test_turn_right(): assert -1 == turn(Point(5, 10), Point(5, 20), Point(15, 15))
def test_turn_colinear(): assert 0 == turn(Point(5, 10), Point(5, 20), Point(5, 30))
def test_point__sub__(): assert Point(12, 15) - Point(2, 10) == Point(10, 5)