def test_make3(self): t1 = Triangle(0, 0, 6, 0, 2, 4) t2 = Triangle(6, 0, 0, 12, 2, 4) t3 = Triangle(0, 12, 0, 0, 2, 4) result = self.t1.make3() self.assertTrue(t1 in result) self.assertTrue(t2 in result) self.assertTrue(t3 in result)
def test_mapoverlay4(self): M1 = PlanarMap(Triangle(3, 0, 6, 6, 0, 6)) #M1 = PlanarMap(Triangle(3., 0., 6., 6., 0., 6.)) M2 = PlanarMap(Triangle(3, 8, 0, 2, 6, 2)) M3 = M1.map_overlay(M2) self.assertEqual(M3.v(), 12) self.assertEqual(M3.e(), 18) self.assertEqual(M3.f(), 8)
def test_make4(self): t1 = Triangle(0, 0, 3, 0, 0, 6) t2 = Triangle(6, 0, 3, 6, 3, 0) t3 = Triangle(0, 12, 0, 6, 3, 6) t4 = Triangle(3, 0, 3, 6, 0, 6) result = self.t1.make4() self.assertTrue(t1 in result) self.assertTrue(t2 in result) self.assertTrue(t3 in result) self.assertTrue(t4 in result)
def legalize(self, point, segment): #print ( "legalize {} {}".format(point, segment) ) # Trzeba znalezc trojkaty sasiadujace z krawedzia. tlist = self.tc.search(segment.center()) if len(tlist) == 1: # przypadek (i) de Berga # Nie przekrecamy krawedzi duzego trojkata, jest legalna. #print ( "big triangle segment" ) assert segment.pt1 in self.big_nodes and segment.pt2 in self.big_nodes return #print ( "tlist {}".format(tlist) ) assert len(tlist) == 2 t1 = tlist.pop() t2 = tlist.pop() if point in t2: # chcemy point in t1 t1, t2 = t2, t1 pt3 = t2.third_node(segment.pt1, segment.pt2) illegal = t1.in_circumcircle(pt3) if pt3 in self.big_nodes: #print ( "{} in big_nodes".format(pt3) ) # Trzeba sprawdzic, czy koniec krawedzi nie jest z big triangle. if segment.pt1 in self.big_nodes: # przypadek (iv) de Berga # Dokladnie dwa indeksy sa ujemne, jeden z krawedzi. illegal = False if segment.pt1 < pt3 else True #print ( "illegal 2 {}".format(illegal) ) elif segment.pt2 in self.big_nodes: # przypadek (iv) de Berga # Dokladnie dwa indeksy sa ujemne, jeden z krawedzi. illegal = False if segment.pt2 < pt3 else True #print ( "illegal 2 {}".format(illegal) ) else: # przypadek (iii) de Berga # Dokladnie jeden indeks ujemny (pt3), ale nie przy krawedzi. illegal = False # krawedz jest legalna #print ( "illegal 1 {}".format(illegal) ) else: # jeden koniec krawedzi moze byc z big triangle if segment.pt1 in self.big_nodes or segment.pt2 in self.big_nodes: # Przypadek (iii) de Berga, jeden indeks ujemny z krawedzi. illegal = True #print ( "illegal 1 {}".format(illegal) ) else: # cztery indeksy sa dodatnie, przypadek (ii) de Berga, pass # procedujemy normalnie if illegal: if orientation(point, segment.pt1, pt3) * orientation( point, segment.pt2, pt3) > 0: # Czworokat wklesly! Nie przekrecamy! #print ( "concave quadrilateral!" ) illegal = False if illegal: # jezeli krawedz nielegalna # Przekrecamy krawedz (edge flipping). #print ( "segment flip" ) self.tc.remove(t1) self.tc.remove(t2) self.tc.insert(Triangle(segment.pt1, point, pt3)) self.tc.insert(Triangle(segment.pt2, point, pt3)) self.legalize(point, Segment(segment.pt1, pt3)) self.legalize(point, Segment(segment.pt2, pt3))
def test_mapoverlay5(self): M1 = PlanarMap(Triangle(0, 0, 3, 1, 0, 2)) M2 = PlanarMap(Rectangle(1, 0, 2, 2)) M3 = M1.map_overlay(M2) self.assertEqual(M3.v(), 11) self.assertEqual(M3.e(), 15) self.assertEqual(M3.f(), 6)
def run(self): for (p1, p2, p3) in itertools.combinations(self.point_list, 3): # Counterclockwise orientation of points. if orientation(p1, p2, p3) < 0: p2, p3 = p3, p2 elif orientation(p1, p2, p3) == 0: # collinear points continue triangle = Triangle(p1, p2, p3) if all(not triangle.in_circumcircle(p4) for p4 in self.point_list if p4 != p1 and p4 != p2 and p4 != p3): self.tc.insert(triangle)
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))
def test_triangulation(self): p1 = Point(-1, -1) p2 = Point(1, -1) p3 = Point(2, 1) p4 = Point(-1, 1) point_list = [p1, p2, p3, p4] algorithm = BowyerWatson(point_list) algorithm.run() self.assertEqual(len(algorithm.tc), 2) self.assertTrue(Triangle(p1, p2, p4) in algorithm.tc) self.assertTrue(Triangle(p2, p3, p4) in algorithm.tc) #print ( "tc {}".format(algorithm.tc) ) G = algorithm.tc.to_graph() #G.show() self.assertEqual(G.v(), 4) self.assertEqual(G.e(), 5) self.assertTrue(G.has_edge(Edge(p1, p2))) self.assertTrue(G.has_edge(Edge(p1, p4))) self.assertTrue(G.has_edge(Edge(p2, p4))) self.assertTrue(G.has_edge(Edge(p2, p3))) self.assertTrue(G.has_edge(Edge(p3, p4)))
def add_to_triangulation(self, point): #print ( "add_to_triangulation {}".format(point) ) bad_triangles = list() for triangle in self.tc.itertriangles(): if triangle.in_circumcircle(point): bad_triangles.append(triangle) sdict = dict() for triangle in bad_triangles: # Sprawdzanie krawedzi. for segment in triangle.itersegments(): sdict[segment] = sdict.get(segment, 0) + 1 for triangle in bad_triangles: self.tc.remove(triangle) for segment in sdict: if sdict[segment] == 1: self.tc.insert(Triangle(point, segment.pt1, segment.pt2))
def add_to_triangulation(self, point): #print ( "add_to_triangulation {}".format(point) ) # Szukamy trojkata do ktorego wpada punkt. # Punkt moze byc na krawedzi, na granicy dwoch trojkatow. tlist = self.tc.search(point) # tlist moze zawierac jeden lub dwa trojkaty (wspolna krawedz). #print ( "tlist {}".format(tlist) ) if len(tlist) == 1: # punkt we wnetrzu trojkata # UWAGA Punkt moze trafic na krawedz big triangle, # a wtedy bedzie degeneracja. # Chyba ze zrobimy jeszcze wiekszy big triangle. t1 = tlist.pop() self.tc.remove(t1) self.tc.insert(Triangle(t1.pt1, t1.pt2, point)) self.tc.insert(Triangle(t1.pt2, t1.pt3, point)) self.tc.insert(Triangle(t1.pt3, t1.pt1, point)) self.legalize(point, Segment(t1.pt1, t1.pt2)) self.legalize(point, Segment(t1.pt2, t1.pt3)) self.legalize(point, Segment(t1.pt3, t1.pt1)) elif len(tlist) == 2: # punkt na krawedzi t1 = tlist.pop() t2 = tlist.pop() # Trzeba znalezc wspolna krawedz. segment = t1.common_segment(t2) assert point in segment # Konce wspolnej krawedzi. q1 = segment.pt1 q2 = segment.pt2 q3 = t1.third_node(q1, q2) q4 = t2.third_node(q1, q2) self.tc.remove(t1) self.tc.remove(t2) self.tc.insert(Triangle(q1, q3, point)) self.tc.insert(Triangle(q2, q3, point)) self.tc.insert(Triangle(q1, q4, point)) self.tc.insert(Triangle(q2, q4, point)) self.legalize(point, Segment(q1, q3)) self.legalize(point, Segment(q2, q3)) self.legalize(point, Segment(q1, q4)) self.legalize(point, Segment(q2, q4)) else: raise ValueError("more than 2 points in tlist")
def setUp(self): self.t1 = Triangle(0, 0, 2, 0, 0, 2) self.t2 = Triangle(2, 0, 2, 2, 0, 2) self.tc = TriangleCollection()
def test_init_triangle(self): M1 = PlanarMap(Triangle(0, 0, 1, 0, 1, 1)) self.assertEqual(M1.v(), 3) self.assertEqual(M1.e(), 3) self.assertEqual(M1.f(), 2)
def test_move(self): self.assertEqual(self.t1.move(1, 2), Triangle(1, 2, 7, 2, 1, 14)) self.assertEqual(self.t1.move(Point(1, 2)), Triangle(1, 2, 7, 2, 1, 14)) self.assertRaises(ValueError, Triangle.move, self.t1, 1)
def test_cmp(self): self.assertEqual(Triangle(), Triangle(0, 0, 1, 0, 0, 1)) self.assertTrue(self.t1 == Triangle(0, 0, 6, 0, 0, 12)) self.assertFalse(self.t1 == self.t2) self.assertTrue(self.t1 != self.t2) self.assertFalse(self.t1 != Triangle(0, 0, 6, 0, 0, 12))
def test_orientation(self): self.assertEqual(self.t1.orientation(), 1) self.assertEqual(Triangle(0, 0, 1, 1, 1, 0).orientation(), -1)
def setUp(self): self.t1 = Triangle(0, 0, 6, 0, 0, 12) self.t2 = Triangle(3, 0, 9, 0, 6, 6) self.t3 = Triangle(Point(0, 0), Point(2, 0), Point(1, 2)) self.t4 = Triangle(Fraction(1, 2), Fraction(2, 3), Fraction(3, 4), Fraction(4, 5), Fraction(5, 6), Fraction(6, 7))
from planegeometry.structures.circles import Circle plt.axis([0, 1, 0, 1]) ax = plt.gca() rectangle1 = Rectangle(0.1, 0.3, 0.9, 0.7) # rectangle1 = plt.Rectangle((0.1, 0.3), 0.8, 0.4, fill=False, color='b') rectangle2 = plt.Rectangle((0.6, 0.1), 0.2, 0.1, fill=False, color='m') for segment in rectangle1.itersegments(): x = [segment.pt1.x, segment.pt2.x] y = [segment.pt1.y, segment.pt2.y] #plt.plot(x, y, 'bo-') plt.plot(x, y, 'b.-') triangle1 = Triangle(0.2, 0.2, 0.8, 0.4, 0.6, 0.8) for segment in triangle1.itersegments(): x = [segment.pt1.x, segment.pt2.x] y = [segment.pt1.y, segment.pt2.y] #plt.plot(x, y, 'ro-') plt.plot(x, y, 'r.-') #circle1 = Circle(0.4, 0.6, 0.2) circle1 = plt.Circle((0.4, 0.6), 0.2, color='g', fill=False) ax.add_patch(circle1) ax.add_patch(rectangle2) plt.title("Random figures") plt.xlabel("x")