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_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_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 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 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 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()
class TestTriangle(unittest.TestCase): 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)) 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)) def test_print(self): self.assertEqual(repr(self.t1), "Triangle(0, 0, 6, 0, 0, 12)") self.assertEqual(repr(self.t2), "Triangle(3, 0, 9, 0, 6, 6)") self.assertEqual(repr(self.t3), "Triangle(0, 0, 2, 0, 1, 2)") self.assertEqual( repr(self.t4), "Triangle(Fraction(1, 2), Fraction(2, 3), Fraction(3, 4), Fraction(4, 5), Fraction(5, 6), Fraction(6, 7))" ) 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_copy(self): t3 = self.t1.copy() self.assertEqual(t3, self.t1) self.assertNotEqual(id(t3), id(self.t1)) def test_center(self): self.assertEqual(self.t1.center(), Point(2, 4)) self.assertEqual(self.t2.center(), Point(6, 2)) def test_area(self): self.assertEqual(self.t1.area(), 36) self.assertEqual(self.t2.area(), 18) 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_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_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 test_hash(self): aset = set() aset.add(self.t1) aset.add(self.t1) # ignored self.assertEqual(len(aset), 1) aset.add(self.t2) self.assertEqual(len(aset), 2) def test_contains(self): self.assertTrue(Point(2, 2) in self.t1) self.assertTrue(Point(4, 4) in self.t1) self.assertTrue(Point(3, 0) in self.t1) self.assertTrue(Point(0, 8) in self.t1) self.assertFalse(Point(6, 6) in self.t1) self.assertFalse(Point(7, 0) in self.t1) self.assertFalse(Point(0, 13) in self.t1) self.assertRaises(ValueError, Triangle.__contains__, self.t1, 1) # segment in tringle self.assertTrue(Segment(1, 1, 2, 2) in self.t1) self.assertFalse(Segment(1, 1, 6, 6) in self.t1) def test_orientation(self): self.assertEqual(self.t1.orientation(), 1) self.assertEqual(Triangle(0, 0, 1, 1, 1, 0).orientation(), -1) def test_gnu(self): s1 = 'set label "" at 0.0,0.0 point pt 7 ps 0.5\n' s2 = 'set label "" at 6.0,0.0 point pt 7 ps 0.5\n' s3 = 'set label "" at 0.0,12.0 point pt 7 ps 0.5\n' s4 = 'set arrow from 0.0,0.0 to 6.0,0.0 nohead\n' s5 = 'set arrow from 0.0,0.0 to 0.0,12.0 nohead\n' s6 = 'set arrow from 6.0,0.0 to 0.0,12.0 nohead\n' self.assertEqual(self.t1.gnu(True), s1 + s2 + s3 + s4 + s5 + s6) 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)) def test_in_circumcircle(self): # Srodek okregu opisanego na t1 jest w Point(3, 6). self.assertTrue(self.t1.in_circumcircle(Point(1, 1))) self.assertTrue(self.t1.in_circumcircle(Point(6, 11))) self.assertFalse(self.t1.in_circumcircle(Point(7, 0))) def test_circumcenter(self): self.assertEqual(self.t1.circumcenter(), Point(3, 6)) self.assertEqual(self.t2.circumcenter(), Point(6, Fraction(9, 4))) self.assertEqual(self.t3.circumcenter(), Point(1, Fraction(3, 4))) def test_iterpoints(self): L = list(self.t1.iterpoints()) self.assertEqual(L[0], self.t1.pt1) self.assertEqual(L[1], self.t1.pt2) self.assertEqual(L[2], self.t1.pt3) def test_itersegments(self): L = list(self.t1.itersegments()) self.assertTrue(Segment(0, 0, 6, 0) in L) self.assertTrue(Segment(0, 0, 0, 12) in L) self.assertTrue(Segment(0, 12, 6, 0) in L) def test_itersegments_oriented(self): L = list(self.t1.itersegments_oriented()) self.assertTrue(Segment(6, 0, 0, 0) in L) self.assertTrue(Segment(0, 0, 0, 12) in L) self.assertTrue(Segment(0, 12, 6, 0) in L) def tearDown(self): pass
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)
import random import Gnuplot # Python 2 only from planegeometry.structures.points import Point from planegeometry.structures.segments import Segment from planegeometry.structures.rectangles import Rectangle from planegeometry.structures.triangles import Triangle from planegeometry.structures.circles import Circle gnu = Gnuplot.Gnuplot(persist=1) visible = True rectangle = Rectangle(0.1, 0.3, 0.9, 0.7) gnu(rectangle.gnu(visible)) triangle = Triangle(0.2, 0.2, 0.8, 0.4, 0.6, 0.8) gnu(triangle.gnu(visible)) circle = Circle(0.4, 0.6, 0.2) gnu(circle.gnu(visible)) # Wyswietlenie grafu. gnu('set terminal pdf enhanced') gnu('set output "random_figures.pdf"') gnu('set grid') gnu('unset key') gnu('set size square') #gnu('unset border') #gnu('unset tics') gnu('set xlabel "x"') gnu('set ylabel "y"')
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")
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)