def test_delaunay_triangulation() -> None: """ Tests if delaunay_triangulation method works correctly. """ a, b, c, d = [Point(4, 3), Point(-1, 2), Point(1, 1), Point(2, 1)] polygon = Polygon([a, b, c, d]) triangulation = delaunay_triangulation(polygon) assert triangulation.triangles == [Triangle(c, a, d), Triangle(c, a, b)]
def create_integration_point_set(): const_value = 1 / sqrt(3) integration_point_set = [ Point(-const_value, -const_value), Point(const_value, -const_value), Point(const_value, const_value), Point(-const_value, const_value) ] return integration_point_set
def test_area() -> None: """ Tests if area method works correctly. One common and one edge case """ point_1 = Point(x=3, y=4) point_2 = Point(x=3, y=0) point_3 = Point(x=0, y=0) assert Triangle(first=point_1, second=point_1, third=point_1).area() == 0 assert Triangle(first=point_1, second=point_2, third=point_3).area() == 6
def test_empty_circle() -> None: """ Tests if empty_circle method works correctly. """ a, b, c, d = [Point(4, 3), Point(-1, 2), Point(1, 1), Point(2, 1)] poly = Polygon([a, b, c]) poly.make_simple() poly.points.append(d) assert empty_circle(poly)
def test_find_pair_edge() -> None: """ Tests if find pair edge method works correctly. """ a, b, c, d = [Point(4, 3), Point(-1, 2), Point(1, 1), Point(2, 1)] triangles = [Triangle(b, c, d), Triangle(b, a, d)] triangulation = Triangulation(triangles) pair = find_pair_edge(triangulation, LineSegment(b, d)) assert pair == (LineSegment(c, a), triangles)
def test_add_triangles() -> None: """ Tests if add_triangles method works correctly. """ a, b, c = [Point(4, 3), Point(-1, 2), Point(1, 1)] d = Point(2, 1) triangles = [Triangle(b, c, a)] triangulation = Triangulation(triangles) add_triangles(triangulation, d, [b, c, a]) assert triangulation.triangles == [Triangle(b, c, a), Triangle(a, c, d)]
def test_sort_other_points() -> None: """ Tests if sort_other_points method works correctly. """ start_point = Point(0, 0) other_points = [Point(4, 3), Point(-1, 2), Point(1, 1), Point(2, 2)] sort_points = sort_other_points(other_points, start_point) assert sort_points == [Point(1, 1), Point(-1, 2), Point(2, 2), Point(4, 3)]
def test_eq() -> None: """ Tests if overridden method __eq__ works correctly """ point_1 = Point(x=1, y=2) point_2 = Point(x=2, y=-4) point_3 = Point(x=3, y=6) triangle_1 = Triangle(first=point_1, second=point_2, third=point_3) triangle_2 = Triangle(first=point_1, second=point_2, third=point_3) triangle_3 = Triangle(first=point_3, second=point_1, third=point_2) assert triangle_1 == triangle_2 assert not triangle_1 == triangle_3
def test__eq__() -> None: """ Tests if overriden __eq__ method works correctly """ point_1 = Point(1, 2) point_2 = Point(-2, -4) point_3 = Point(3, 3) point_4 = Point(0, 0) line_segment_1 = LineSegment(first=point_1, second=point_2) line_segment_2 = LineSegment(first=point_1, second=point_2) line_segment_3 = LineSegment(first=point_3, second=point_4) assert line_segment_1 == line_segment_2 assert not line_segment_1 == line_segment_3
def test_contains_point() -> None: """ Tests if contains_point determines relationship between line_segment and point correctly. Tests both common and edge cases. """ point_1 = Point(1, 2) point_2 = Point(-2, -4) point_3 = Point(3, 3) point_4 = Point(0, 0) line_segment = LineSegment(first=point_1, second=point_2) assert line_segment.does_contain(point_1) assert line_segment.does_contain(point_2) assert not line_segment.does_contain(point_3) assert line_segment.does_contain(point_4)
def test_does_intersect() -> None: """ Tests if does_intersect works correctly. Tests both common and edge cases. """ line_segment_1 = LineSegment(first=Point(1, 1), second=Point(4, 4)) reversed_segment = LineSegment(first=Point(4, 4), second=Point(1, 1)) line_segment_2 = LineSegment(first=Point(1, 1), second=Point(-2, -4)) line_segment_3 = LineSegment(first=Point(3, 3), second=Point(5, 5)) line_segment_4 = LineSegment(first=Point(1, 0), second=Point(5, -5)) assert line_segment_1.does_intersect_or_touch(reversed_segment) assert line_segment_1.does_intersect_or_touch(line_segment_2) assert line_segment_1.does_intersect_or_touch(line_segment_3) assert not line_segment_1.does_intersect_or_touch(line_segment_4)
def test_new_edges() -> None: """ Tests if new_edges method works correctly. """ a, b, c, d = [Point(4, 3), Point(-1, 2), Point(1, 1), Point(2, 1)] t_points = [a, b, c, d] t_edges = [ LineSegment(b, c), LineSegment(c, d), LineSegment(b, d), LineSegment(b, a), LineSegment(a, d) ] point = Point(1, 3) assert new_edges(t_points, t_edges, point) == [LineSegment(point, a), LineSegment(point, b)]
def create_integration_point_bc_set(): integration_point_set = [ Point(-1 / sqrt(3), -1), Point(1 / sqrt(3), -1), Point(1, -1 / sqrt(3)), Point(1, 1 / sqrt(3)), Point(1 / sqrt(3), 1), Point(-1 / sqrt(3), 1), Point(-1, 1 / sqrt(3)), Point(-1, -1 / sqrt(3)) ] return integration_point_set
def _sort_key(_point: Point) -> Tuple[float, float, float]: """ HELPER. Defines sorting order of points. Args: _point: Point object. Returns: Tuple consisting of comparison oreder priority (slope, -y, x) """ return _point.slope(start), -_point.y, _point.x
def _radial_key(_point: Point): """ Defines comparison key for sorting. Args: _point: Some point. Returns: Euclidean distance from starting point. """ return _point.euclidean_dist_squared(start_point)
def test_euclidean_dist_squared() -> None: """ Tests if euclidean_dist_squared works correctly. """ point_1 = Point(x=3, y=4) point_2 = Point(x=0, y=0) point_3 = Point(x=3, y=0) point_4 = Point(x=6, y=4) assert point_1.euclidean_dist_squared(point_1) == 0 assert point_1.euclidean_dist_squared(point_2) == 25 assert point_1.euclidean_dist_squared(point_3) == 16 assert point_1.euclidean_dist_squared(point_4) == 9
def does_contain(self, point: Point) -> bool: """ Determines if point "lies" in polygon (self) Args: point: Point object to be tested Returns: True if point is in polygon, False otherwise """ # NOTE: PSEUDO_INF used instead of float("inf") of numpy.inf because # usage of these methods gave incorrect results. Float comparison error? ray = LineSegment(first=point, second=Point(PSEUDO_INF, point.y)) num_of_int, on_edge = self.intersection_count(ray) return num_of_int % 2 != 0 or on_edge
def test_slope() -> None: """ Tests if slope function works correctly. """ point_1 = Point(x=3, y=4) point_2 = Point(x=0, y=0) assert point_1.slope(point_1) == float('inf') assert point_1.slope(point_2) == 4 / 3 assert point_2.slope(point_1) == 4 / 3
def test_does_contain() -> None: """ Tests if does contain method works correctly. Two common and one edge case """ point_1 = Point(x=3, y=4) point_2 = Point(x=3, y=0) point_3 = Point(x=0, y=0) point_in = Point(x=2, y=2) point_out = Point(x=0, y=5) point_edge = Point(x=2, y=0) triangle = Triangle(first=point_1, second=point_2, third=point_3) assert triangle.does_contain(point_in) assert not triangle.does_contain(point_out) assert triangle.does_contain(point_edge)
def test_does_intersect() -> None: """ Tests if Polygon class method does_intersect works correctly. Two common and one edge case. """ polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) line_segment_intersect = LineSegment(Point(0, 2), Point(2, 2)) line_segment_no_intersect = LineSegment(Point(0, 0), Point(-5, 2)) line_segment_match = LineSegment(polygon.points[4], polygon.points[5]) assert polygon.does_intersect(line_segment_intersect) assert not polygon.does_intersect(line_segment_no_intersect) assert polygon.does_intersect(line_segment_match)
def translate_coord_from_canvas(x: int, y: int) -> Point: return Point(x - CENTER, -(y - CENTER))
def test_is_empty() -> None: """ Tests if is_empty method works correctly. """ point_1 = Point(1, 2) point_2 = Point(2, -4) point_3 = Point(3, 6) triangle = Triangle(first=point_1, second=point_2, third=point_3) points_out = [ Point(0, 0), Point(2, 5), Point(7, 9), Point(3, 8), Point(4, 4), Point(0, 5) ] points_in = [ Point(2, 2), Point(2, 4), Point(0, 0), Point(2, 7), Point(6, 3), Point(-5, 5) ] assert triangle.is_empty(points_out) assert not triangle.is_empty(points_in)
def test_does_contain() -> None: """ Tests if Polygon class method does_contain works correctly. Two common and one edge case. """ polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) point_in = Point(4, 4) point_out = Point(0, 0) point_edge = Point(5, 3) assert polygon.does_contain(point_in) assert not polygon.does_contain(point_out) assert polygon.does_contain(point_edge)
def test_orientation() -> None: """ Tests if Polygon class method orientation works correctly. """ polygon_1 = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) polygon_2 = Polygon([ Point(1, 1), Point(2, 3), Point(2, 5), Point(5, 3), Point(4, 8), Point(5, 6), Point(4, 1), Point(3, 4) ]) assert polygon_1.orientation() == 1 assert polygon_2.orientation() == -1
def test_make_convex() -> None: """ Tests if Polygon class method make_convex works correctly. """ polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) convex = Polygon([ Point(1, 1), Point(4, 1), Point(5, 3), Point(5, 6), Point(4, 8), Point(2, 5) ]) polygon.make_convex_hull() assert convex == polygon
def test_make_simple() -> None: """ Tests if Polygon class method make_simple works correctly. """ polygon_1 = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) simple = Polygon([ Point(4, 1), Point(5, 3), Point(5, 6), Point(3, 4), Point(2, 3), Point(4, 8), Point(2, 5), Point(1, 1) ]) polygon_1.make_simple() assert simple == polygon_1
def test_is_convex() -> None: """ Tests if Polygon class method is_convex works correctly. """ polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) convex = Polygon( [Point(1, 1), Point(3, 3), Point(0, 5), Point(-1, 4), Point(-1, 2)]) assert not polygon.is_convex() assert convex.is_convex()
def test_number_of_intersections() -> None: """ Tests if Polygon class method number_of_intersections works correctly. """ line_segment_intersect = LineSegment(first=Point(0, 0), second=Point(10, 10)) line_segment_no_intersect = LineSegment(first=Point(0, 0), second=Point(-10, -10)) polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) assert polygon.intersection_count(line_segment_intersect) == (6, False) assert polygon.intersection_count(line_segment_no_intersect) == (0, False)
from db.dal import save_triangulations from structures.point import Point from structures.polygon import Polygon from triangulations.triangulations import all_triangulations _5gon = Polygon([Point(4, 2), Point(1, 0), Point(2, 4), Point(0, 2), Point(3, 0)]) _6gon = Polygon([Point(0, 3), Point(3, 0), Point(3, 6), Point(1, 1), Point(6, 2), Point(5, 4)]) _7gon = Polygon([Point(3, 1), Point(4, 6), Point(1, 1), Point(0, 4), Point(2, 8), Point(2, 0), Point(0, 3), ]) _8gon = Polygon([Point(0, 0), Point(4, 4), Point(2, 3), Point(5, 2), Point(0, 1), Point(1, 0), Point(4, 1), Point(5, 3)]) _9gon = Polygon([Point(0, 0), Point(4, 4), Point(2, 1), Point(3, 9), Point(5, 5), Point(0, 3), Point(3, 2), Point(4, 8), Point(1, 5)]) _10gon = Polygon([Point(1, 0), Point(5, 4), Point(2, 0), Point(3, 8), Point(5, 3), Point(0, 3), Point(4, 2), Point(4, 6), Point(0, 5), Point(1, 7)]) _11gon = Polygon([Point(1, 1), Point(5, 4), Point(2, 0), Point(3, 8), Point(4, 1), Point(0, 3), Point(5, 3), Point(4, 6), Point(0, 6), Point(2, 9), Point(1, 9)]) print("{}: {}".format(len(_5gon.points), len(all_triangulations(_5gon)))) print("{}: {}".format(len(_6gon.points), len(all_triangulations(_6gon))))
def test_is_empty() -> None: """ Tests if Polygon class method is_empty works correctly. Two common and one edge case. """ polygon = Polygon([ Point(1, 1), Point(5, 6), Point(5, 3), Point(2, 5), Point(4, 8), Point(2, 3), Point(4, 1), Point(3, 4) ]) no_points = [] points_out = [Point(0, 0), Point(10, 10), Point(0, 6), Point(-1, 12)] points_in = [Point(0, 0), Point(4, 4), Point(0, 6), Point(-1, 12)] assert polygon.is_empty(no_points) assert polygon.is_empty(points_out) assert not polygon.is_empty(points_in)