コード例 #1
0
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)]
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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)
コード例 #5
0
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)
コード例 #6
0
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)]
コード例 #7
0
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)]
コード例 #8
0
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
コード例 #9
0
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
コード例 #10
0
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)
コード例 #11
0
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)
コード例 #12
0
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)]
コード例 #13
0
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
コード例 #14
0
        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
コード例 #15
0
    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)
コード例 #16
0
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
コード例 #17
0
    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
コード例 #18
0
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
コード例 #19
0
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)
コード例 #20
0
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)
コード例 #21
0
ファイル: runner.py プロジェクト: chahinMalek/cg-framework
def translate_coord_from_canvas(x: int, y: int) -> Point:
    return Point(x - CENTER, -(y - CENTER))
コード例 #22
0
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)
コード例 #23
0
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)
コード例 #24
0
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
コード例 #25
0
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
コード例 #26
0
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
コード例 #27
0
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()
コード例 #28
0
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)
コード例 #29
0
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))))
コード例 #30
0
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)