예제 #1
0
 def test_property(self):
     self.assertEqual(self.segment1.source, Point(0, 0))
     self.assertEqual(self.segment1.target, Point(2, 0))
     self.assertEqual(self.segment2.source, Point(0, 0))
     self.assertEqual(self.segment2.target, Point(1, 1))
     self.assertEqual(self.segment1.weight, self.segment1.length())
     self.assertEqual(self.segment2.weight, self.segment2.length())
예제 #2
0
 def test_iterpoints(self):
     self.polygon1 = Polygon(0, 0, 1, 0, 1, 1, 0, 1)
     L = list(self.polygon1.iterpoints())
     self.assertEqual(L[0], Point(0, 0))
     self.assertEqual(L[1], Point(1, 0))
     self.assertEqual(L[2], Point(1, 1))
     self.assertEqual(L[3], Point(0, 1))
예제 #3
0
 def test_init(self):
     self.assertRaises(ValueError, Segment, 0, 1)
     self.assertRaises(ValueError, Segment, 0, 1, 2)
     self.assertRaises(ValueError, Segment, Point(0, 1), 2)
     self.assertRaises(ValueError, Segment, 2, Point(0, 1))
     self.assertRaises(ValueError, Segment, 0, 1, 0, 1)
     self.assertRaises(ValueError, Segment, Point(0, 1), Point(0, 1))
예제 #4
0
 def test_init(self):
     self.assertRaises(ValueError, Polygon, 0, 1, 2, 3)
     self.assertRaises(ValueError, Polygon, 0, 1, 2, 3, 4, 5, 6)
     self.assertRaises(ValueError, Polygon, Point(0, 1), Point(2, 3))
     self.assertRaises(ValueError, Polygon, Point(0, 1), Point(2, 3),
                       Point(2, 3))
     self.assertRaises(ValueError, Polygon, 0, 0, 1, 0, 1, 1, 0, 0)
예제 #5
0
 def test_simple_convex_polygon(self):
     point_list = [Point(1, 0), Point(3, 1), Point(1, 2), Point(0, 1)]
     polygon = Polygon(*point_list)
     # punkt w srodku
     point = Point(1, 1)
     self.assertEqual(crossing_number(polygon, point), 1)
     self.assertEqual(crossing_number2(polygon, point), 1)
     self.assertEqual(winding_number(polygon, point), 1)
     # punkt na zewnatrz
     point = Point(3, 2)
     self.assertEqual(crossing_number(polygon, point), 0)
     self.assertEqual(crossing_number2(polygon, point), 0)
     self.assertEqual(winding_number(polygon, point), 0)
     # punkt na zewnatrz, promien przecina wierzcholek gorny
     point = Point(0, 2)
     self.assertEqual(crossing_number(polygon, point), 0)
     self.assertEqual(crossing_number2(polygon, point), 0)
     self.assertEqual(winding_number(polygon, point), 0)
     # punkt na zewnatrz, promien przecina wierzcholek dolny ROZNICA
     point = Point(0, 0)
     self.assertEqual(crossing_number(polygon, point), 2)
     self.assertEqual(winding_number(polygon, point), 0)
     # punkt na lewej dolnej krawedzi (nalezy)
     point = Point(Fraction(1, 2), Fraction(1, 2))
     self.assertEqual(crossing_number(polygon, point), 1)
     self.assertEqual(crossing_number2(polygon, point), 1)
     self.assertEqual(winding_number(polygon, point), 1)
     # punkt na prawej gornej krawedzi (nie nalezy)
     point = Point(2, Fraction(3, 2))
     self.assertEqual(crossing_number(polygon, point), 0)
     self.assertEqual(crossing_number2(polygon, point), 0)
     self.assertEqual(winding_number(polygon, point), 0)
예제 #6
0
 def setUp(self):
     self.M = PlanarMap()
     A, B, C, D = Point(0, 1), Point(1, 1), Point(0, 0), Point(1, 0)
     self.M.add_first_edge(Segment(A, B))
     self.M.add_leaf(Segment(A, C))
     self.M.add_leaf(Segment(B, D))
     self.M.add_chord(Segment(C, B))
예제 #7
0
 def test_exceptions(self):
     self.assertRaises(AssertionError, self.M.add_edge,
                       Edge(Point(0, 0), Point(1, 0)))
     #self.assertRaises(ValueError, self.M.add_edge, Segment(Point(0, 0), Point(0, 0)))
     self.assertRaises(ValueError, self.M.add_edge,
                       Segment(Point(1, 0), Point(1, 1)))
     self.assertRaises(AssertionError, self.M.add_node, "A")
예제 #8
0
 def setUp(self):
     self.r1 = Rectangle(0, 0, 5, 6)
     self.r2 = Rectangle(3, 4, 7, 8)
     self.r3 = Rectangle(6, 7, 8, 8)
     self.r4 = Rectangle(Point(1, 2), Point(3, 4))
     self.r5 = Rectangle(Fraction(1, 2), Fraction(2, 3), Fraction(3, 4),
                         Fraction(4, 5))
예제 #9
0
 def test_add_first_edge(self):
     M1 = PlanarMap()
     A, B, C, D = Point(0, 1), Point(1, 1), Point(0, 0), Point(1, 0)
     # not segment
     self.assertRaises(AssertionError, M1.add_first_edge, Edge(B, D))
     M1.add_first_edge(Segment(A, B))
     # second segment
     self.assertRaises(AssertionError, M1.add_first_edge, Segment(B, D))
예제 #10
0
 def test_contains(self):
     self.assertTrue(Point(1, 1) in self.r1)
     self.assertFalse(Point(1, 7) in self.r1)
     self.assertFalse(Point(7, 1) in self.r1)
     self.assertRaises(ValueError, Rectangle.__contains__, self.r1, 1)
     # segment in rectangle
     self.assertTrue(Segment(1, 1, 2, 2) in self.r1)
     self.assertFalse(Segment(1, 1, 7, 7) in self.r1)
예제 #11
0
 def test_init(self):
     self.assertRaises(ValueError, Circle, 1, 2)
     self.assertRaises(ValueError, Circle, 1, Point(2, 2))
     self.assertRaises(ValueError, Circle, 0, 0, -1)
     self.assertRaises(ValueError, Circle, Point(0, 0), -1)
     self.assertEqual(self.c1.pt, Point(0, 0))
     self.assertEqual(self.c1.radius, 2)
     self.assertEqual(self.c3.pt, Point(1, 2))
     self.assertEqual(self.c3.radius, 3)
예제 #12
0
 def test_mul(self):
     self.assertAlmostEqual(self.p1 * self.p2, 27.06)
     self.assertAlmostEqual(self.p1 * 2, Point(6.8, 11.2))
     self.assertAlmostEqual(2 * self.p1, Point(6.8, 11.2))
     self.assertAlmostEqual(Point(2, 3) * 1.5, Point(3.0, 4.5))
     self.assertAlmostEqual(1.5 * Point(2, 3), Point(3.0, 4.5))
     self.assertAlmostEqual(self.p1.cross(self.p2), -18.06)
     self.assertAlmostEqual(Point(1, 0).cross(Point(0, 1)), 1)
     self.assertAlmostEqual(Point(0, 1).cross(Point(1, 0)), -1)
예제 #13
0
 def test_third_node(self):
     p1 = Point(0, 0)
     p2 = Point(6, 0)
     p3 = Point(0, 12)
     self.assertEqual(self.t1.third_node(p1, p2), p3)
     self.assertEqual(self.t1.third_node(p1, p3), p2)
     self.assertEqual(self.t1.third_node(p3, p2), p1)
     self.assertRaises(KeyError, Triangle.third_node, self.t1, p1,
                       Point(1, 1))
예제 #14
0
 def test_divide_edge(self):
     M1 = PlanarMap()
     A, B, C, D = Point(0, 2), Point(2, 2), Point(0, 0), Point(2, 0)
     AB = Segment(A, B)
     M1.add_first_edge(AB)
     #M1.divide_edge(AB, Point(1, 2))
     M1.divide_edge(Segment(A, B), Point(1, 2))  # it works
     self.assertEqual(M1.v(), 3)
     self.assertEqual(M1.e(), 2)
     self.assertEqual(M1.f(), 1)
예제 #15
0
 def test_hash(self):
     aset = set()
     aset.add(self.p1)
     aset.add(self.p1)   # ignored
     self.assertEqual(len(aset), 1)
     aset.add(self.p2)
     self.assertEqual(len(aset), 2)
     aset.add(Point(1, 2))
     aset.add(Point(1.0, 2.0))   # ignored, float
     self.assertEqual(len(aset), 3)
예제 #16
0
 def test_polygon8(self): # trojkat
     L = [Point(0, 0), Point(2, 0), Point(1, 2)]
     expected = [(Point(0, 0), Point(2, 0)),
         (Point(0, 0), Point(1, 2)),
         (Point(2, 0), Point(1, 2))]
     result = list(iter_all_antipodal_pairs(L))
     self.assertEqual(result, expected)
예제 #17
0
 def insert_big_triangle(self):
     big = max(max(abs(point.x), abs(point.y)) for point in self.point_list)
     m = 4  # najlepiej typu int
     # m = 3 sugeruje de Berg, ale moze byc na styk, jezeli beda punkty
     # Point(big, -big) lub Point(-big, big).
     p1 = Point(m * big, 0)
     p2 = Point(0, m * big)
     p3 = Point(-m * big, -m * big)
     # Counterclockwise orientation of points.
     self.big_nodes = set([p1, p2, p3])
     self.tc.insert(Triangle(p1, p2, p3))
예제 #18
0
 def test_add_leaf(self):
     M1 = PlanarMap()
     A, B, C, D = Point(0, 1), Point(1, 1), Point(0, 0), Point(1, 0)
     # empty map
     self.assertRaises(AssertionError, M1.add_leaf, Segment(B, D))
     M1.add_first_edge(Segment(A, B))
     M1.add_leaf(Segment(A, C))
     # adding chord
     self.assertRaises(AssertionError, M1.add_leaf, Segment(B, C))
     # adding the same leaf
     self.assertRaises(AssertionError, M1.add_leaf, Segment(A, C))
예제 #19
0
 def test_contains(self):
     self.assertTrue(Point(Fraction(1, 2), Fraction(1, 2)) in self.polygon1)
     self.assertTrue(Point(0, 0) in self.polygon1)
     self.assertTrue(Point(-1, 0) not in self.polygon1)
     self.assertTrue(Point(-1, 1) not in self.polygon1)
     self.assertTrue(Point(1, 1) not in self.polygon1)
     self.assertTrue(Point(1, 1) in self.polygon2)
     self.assertTrue(Point(0, 2) not in self.polygon2)
     self.assertTrue(Point(-1, 0) not in self.polygon2)
     self.assertTrue(Point(1, 0) in self.polygon2)
     self.assertTrue(Point(Fraction(1, 2), 1) in self.polygon2)
예제 #20
0
 def test_cmp(self):
     polygon3 = Polygon(0, 0, 2, 0, 1, 2)
     self.assertEqual(polygon3, self.polygon2)
     self.assertNotEqual(polygon3, self.polygon1)
     p1, p2, p3, p4 = Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)
     p5 = Point(2, 2)
     self.assertNotEqual(self.polygon1, Polygon(p1, p2, p3))  # different n
     self.assertNotEqual(self.polygon1, Polygon(p1, p2, p3, p5,
                                                p4))  # different n
     self.assertNotEqual(self.polygon1, Polygon(p4, p1, p2, p5))  # p5!=p3
     self.assertNotEqual(self.polygon1, Polygon(p2, p1, p4, p5))  # p5!=p3
     self.assertEqual(self.polygon1, Polygon(p3, p4, p1, p2))  # orient=1
     self.assertEqual(self.polygon1, Polygon(p3, p2, p1, p4))  # orient=-1
예제 #21
0
 def test_query(self):
     p1 = Point(0.4, 0.4)
     p2 = Point(0.6, 0.6)
     self.qt1.insert(p1)
     self.qt1.insert(p2)
     result = self.qt1.query(Rectangle(0.0, 0.0, 0.2, 0.2))
     self.assertEqual(result, [])
     result = self.qt1.query(Rectangle(0.5, 0.5, 0.9, 0.9))
     self.assertEqual(result, [p2])
     result = self.qt1.query(Rectangle(0.3, 0.3, 0.7, 0.7))
     self.assertEqual(result, [p1, p2])
     result = self.qt1.query(Rectangle(1.0, 1.0, 2.0, 2.0))
     self.assertEqual(result, [])
예제 #22
0
 def circumcenter(self):
     """Return the circumcenter for the triangle.
     
     https://en.wikipedia.org/wiki/Circumscribed_circle#Circumcircle_equations
     """
     a, b, c = self.pt1, self.pt2, self.pt3
     d = 2 * ( a.cross(b) - a.cross(c) + b.cross(c) )
     x = ((a*a)*(b.y - c.y) + (b*b)*(c.y - a.y) + (c*c)*(a.y - b.y))
     y = ((a*a)*(c.x - b.x) + (b*b)*(a.x - c.x) + (c*c)*(b.x - a.x))
     if isinstance((x+y+d), float):
         return Point(x / float(d), y / float(d))
     else:
         return Point(Fraction(x, d), Fraction(y, d))
예제 #23
0
 def setUp(self):
     # kwadrat, orientacja +1
     self.polygon1 = Polygon(0, 0, 1, 0, 1, 1, 0, 1)
     # trojkat, orientacja +1
     self.polygon2 = Polygon(Point(0, 0), Point(2, 0), Point(1, 2))
     # o-o   nie jest wypukly, orientacja clockwise -1
     # | |
     # | o-o
     # |   |
     # o---o
     self.polygon3 = Polygon(0, 0, 0, 2, 1, 2, 1, 1, 2, 1, 2, 0)
     # o-o   nie jest prosty
     #  X
     # o-o
     self.polygon4 = Polygon(0, 0, 1, 0, 0, 1, 1, 1)
예제 #24
0
 def __init__(self, *arguments):
     """Make a circle in the plane."""
     if len(arguments) == 0:
         self.pt = Point(0, 0)
         self.radius = 1
     elif len(arguments) == 2:
         self.pt, self.radius = arguments
         if not isinstance(self.pt, Point):
             raise ValueError("the first argument is not a point")
     elif len(arguments) == 3:
         x, y, self.radius = arguments
         self.pt = Point(x, y)
     else:
         raise ValueError("bad number of arguments")
     if self.radius < 0:
         raise ValueError("radius negative")
예제 #25
0
 def setUp(self):
     s = 5
     assert s > 3
     self.point_list = []
     A = set([0, s-1])
     for i in range(s):
         for j in range(s):
             if (i in A) and (j in A): # odrzucamy narozniki
                 continue
             self.point_list.append(Point(i, j))
     self.convex_hull = [
         Point(1, 0), Point(s-2, 0), Point(s-1, 1), Point(s-1, s-2),
         Point(s-2, s-1), Point(1, s-1), Point(0, s-2), Point(0, 1)]
예제 #26
0
def make_point_list(n):
    """Prepare a point list."""
    point_list = []
    for _ in range(n):
        point_list.append(Point(
            Fraction(size * random.random()).limit_denominator(),
            Fraction(size * random.random()).limit_denominator()))
    return point_list
예제 #27
0
 def test_init(self):
     self.assertRaises(ValueError, Triangle, 0, 0, 1, 1, 3, 3)
     self.assertRaises(ValueError, Triangle, 0, 0, 1, 1, 3)
     self.assertRaises(ValueError, Triangle, 0, 0, 1, 1)
     self.assertRaises(ValueError, Triangle, Point(1, 1), Point(2, 2), 3)
     self.assertEqual(self.t1.pt1, Point(0, 0))
     self.assertEqual(self.t1.pt2, Point(6, 0))
     self.assertEqual(self.t1.pt3, Point(0, 12))
     self.assertEqual(self.t3.pt1, Point(0, 0))
     self.assertEqual(self.t3.pt2, Point(2, 0))
     self.assertEqual(self.t3.pt3, Point(1, 2))
예제 #28
0
 def test_nearest(self):
     p1 = Point(0.1, 0.1)
     p2 = Point(0.2, 0.2)
     p3 = Point(0.3, 0.3)
     p4 = Point(0.4, 0.4)
     p5 = Point(0.5, 0.5)
     p6 = Point(0.6, 0.6)
     for pt in (p1, p2, p3, p4, p5, p6):
         self.qt1.insert(pt)
     result = self.qt1.nearest(Point(0.7, 0.7))
     self.assertEqual(result, p6)
     result = self.qt1.nearest(Point(0.31, 0.31))
     self.assertEqual(result, p3)
     result = self.qt1.nearest(Point(0.36, 0.36))
     self.assertEqual(result, p4)
예제 #29
0
 def move(self, *arguments):   # przesuniecie o (x, y)
     """Return a new moved triangle."""
     if len(arguments) == 1 and isinstance(arguments[0], Point):
         pt1 = arguments[0]
         return Triangle(*((pt1 + pt2) for pt2 in (self.pt1, self.pt2, self.pt3)))
     elif len(arguments) == 2:
         pt1 = Point(*arguments)
         return Triangle(*((pt1 + pt2) for pt2 in (self.pt1, self.pt2, self.pt3)))
     else:
         raise ValueError("bad arguments")
예제 #30
0
 def move(self, *arguments):  # przesuniecie o (x, y)
     """Return a new moved polygon."""
     if len(arguments) == 1 and isinstance(arguments[0], Point):
         pt1 = arguments[0]
         return Polygon(*((pt1 + pt2) for pt2 in self.point_list))
     elif len(arguments) == 2:
         pt1 = Point(*arguments)
         return Polygon(*((pt1 + pt2) for pt2 in self.point_list))
     else:
         raise ValueError("bad arguments")