fixed the collinear test for polygons with identical vertices

Mon, 24 Jun 2019 10:16:36 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 24 Jun 2019 10:16:36 +0300
changeset 90
6ae063fd27dc
parent 89
d2b277cb948e
child 91
c71a7d863c2e

fixed the collinear test for polygons with identical vertices

geometry.py file | annotate | diff | comparison | revisions
tests/quadrilaterals.py file | annotate | diff | comparison | revisions
--- a/geometry.py	Mon Jun 24 10:13:13 2019 +0300
+++ b/geometry.py	Mon Jun 24 10:16:36 2019 +0300
@@ -197,12 +197,13 @@
             yield LineSegment(v1, v2)
     @property
     def angles(self):
-        from math import acos
+        from math import acos, isclose
         for v1, v2, v3 in pairs(self.vertices, count = 3):
             vec1 = (position_vector(v3) - position_vector(v2)).normalized()
             vec2 = (position_vector(v1) - position_vector(v2)).normalized()
-            cosine = dot_product(vec1, vec2) / vec1.length() / vec2.length()
-            yield acos(cosine)
+            if not isclose(vec1.length(), 0) and not isclose(vec2.length(), 0):
+                cosine = dot_product(vec1, vec2) / vec1.length() / vec2.length()
+                yield acos(cosine)
     @property
     def smallest_angle(self):
         return min(
--- a/tests/quadrilaterals.py	Mon Jun 24 10:13:13 2019 +0300
+++ b/tests/quadrilaterals.py	Mon Jun 24 10:16:36 2019 +0300
@@ -84,6 +84,7 @@
             for a, b, c in pairs(element.geometry.vertices, count = 3):
                 if cross_product(b - a, c - a).length() < 1e-5:
                     yield report_problem('collinear', bad_object = element)
+                    break
 
 @problem_type('hairline-polygon',
     severity = 'warning',

mercurial