Diagram.py: fix and simplify drawing polygon fill and stroke
authorAntonio Ospite <ao2@ao2.it>
Thu, 18 Jun 2015 09:59:47 +0000 (11:59 +0200)
committerAntonio Ospite <ao2@ao2.it>
Thu, 18 Jun 2015 09:59:47 +0000 (11:59 +0200)
Diagram.py
RadialSymmetryDiagram.py

index 3a03949..28cb064 100755 (executable)
@@ -97,30 +97,29 @@ class Diagram(object):
         else:
             return None
 
         else:
             return None
 
-    def draw_polygon(self, verts, stroke_color=[0, 0, 0], fill_color=None):
+    def _draw_polygon(self, verts):
         cr = self.cr
 
         cr = self.cr
 
-        if fill_color:
-            v = verts[0]
-            cr.move_to(v[0], v[1])
-            for v in verts[1:]:
-                cr.line_to(v[0], v[1])
-                cr.close_path()
+        v = verts[0]
+        cr.move_to(v[0], v[1])
+        for v in verts[1:]:
+            cr.line_to(v[0], v[1])
+        cr.close_path()
+
+    def draw_polygon(self, verts, fill_color=None, stroke_color=[0, 0, 0]):
+        cr = self.cr
 
 
+        if fill_color:
+            self._draw_polygon(verts)
             r, g, b, a = self.color_to_rgba(fill_color)
             cr.set_source_rgba(r, g, b, a)
             cr.fill()
 
             r, g, b, a = self.color_to_rgba(fill_color)
             cr.set_source_rgba(r, g, b, a)
             cr.fill()
 
-        n = len(verts)
-        for i in range(0, n):
-            v1 = verts[i]
-            v2 = verts[(i + 1) % n]
-            cr.move_to(v1[0], v1[1])
-            cr.line_to(v2[0], v2[1])
-
-        r, g, b, a = self.color_to_rgba(stroke_color)
-        cr.set_source_rgba(r, g, b, a)
-        cr.stroke()
+        if stroke_color:
+            self._draw_polygon(verts)
+            r, g, b, a = self.color_to_rgba(stroke_color)
+            cr.set_source_rgba(r, g, b, a)
+            cr.stroke()
 
     def draw_star(self, cx, cy, verts, stroke_color=[0, 0, 0]):
         cr = self.cr
 
     def draw_star(self, cx, cy, verts, stroke_color=[0, 0, 0]):
         cr = self.cr
index 333606a..e21694b 100755 (executable)
@@ -66,7 +66,7 @@ class RadialSymmetryDiagram(Diagram.Diagram):
                                          base_polygon_orientation)
 
         if model.show_base_polygon:
                                          base_polygon_orientation)
 
         if model.show_base_polygon:
-            self.draw_polygon(verts, [0, 0, 0])
+            self.draw_polygon(verts, None, [0, 0, 0])
 
         for i, v in enumerate(verts[:]):
             radial_orientation_angle = (i + 1) * central_angle
 
         for i, v in enumerate(verts[:]):
             radial_orientation_angle = (i + 1) * central_angle
@@ -96,7 +96,7 @@ class RadialSymmetryDiagram(Diagram.Diagram):
     def draw_element(self, x, y, model, theta, color=[0, 0, 0]):
         if model.show_elements:
             tverts = self.get_regular_polygon(x, y, model.element_sides, model.element_radius, theta)
     def draw_element(self, x, y, model, theta, color=[0, 0, 0]):
         if model.show_elements:
             tverts = self.get_regular_polygon(x, y, model.element_sides, model.element_radius, theta)
-            self.draw_polygon(tverts, color)
+            self.draw_polygon(tverts, None, color)
 
         if model.show_stars:
             apothem = model.element_radius * cos(pi / model.element_sides)
 
         if model.show_stars:
             apothem = model.element_radius * cos(pi / model.element_sides)