added test for collinearity, fixed bowtie test

Tue, 23 Jan 2018 15:30:48 +0200

author
Santeri Piippo
date
Tue, 23 Jan 2018 15:30:48 +0200
changeset 30
0d9ca37901ed
parent 29
db6ca177c6c4
child 31
02e7e1d73ebb

added test for collinearity, fixed bowtie test

geometry.py file | annotate | diff | comparison | revisions
reference/quadrilaterals.ldr file | annotate | diff | comparison | revisions
tests/quadrilaterals.py file | annotate | diff | comparison | revisions
--- a/geometry.py	Tue Jan 23 14:09:52 2018 +0200
+++ b/geometry.py	Tue Jan 23 15:30:48 2018 +0200
@@ -442,14 +442,22 @@
         a, b = line_1.vertices
         c, d = line_2.vertices
         try:
-            t = (intersection.x - a.x) / (b.x - a.x)
+            t1 = (intersection.x - a.x) / (b.x - a.x)
         except ZeroDivisionError:
-            t = inf
+            t1 = inf
+        try:
+            t2 = (intersection.x - c.x) / (d.x - c.x)
+        except ZeroDivisionError:
+            t2 = inf
         try:
-            u = (intersection.y - a.y) / (b.y - a.y)
+            u1 = (intersection.y - a.y) / (b.y - a.y)
         except ZeroDivisionError:
-            u = inf
-        if 0 < t < 1 or 0 < u < 1:
+            u1 = inf
+        try:
+            u2 = (intersection.y - c.y) / (d.y - c.y)
+        except ZeroDivisionError:
+            u2 = inf
+        if (0 < t1 < 1 and 0 < t2 < 1) or (0 < u1 < 1 and 0 < u2 < 1):
             return intersection
         else:
             return None
--- a/reference/quadrilaterals.ldr	Tue Jan 23 14:09:52 2018 +0200
+++ b/reference/quadrilaterals.ldr	Tue Jan 23 15:30:48 2018 +0200
@@ -17,3 +17,7 @@
 4 16 -5 0 40 -15 0 35 -5 0 30 5 0 35
 0 // collinear
 4 16 20 0 40 30 0 45 30 0 50 30 0 55
+0 // crossed and collinear
+4 16 -50 0 10 -40 0 15 -40 0 10 -40 0 20
+0 // concave and skew
+4 16 -30 0 -20 -35 5 -5 -30 0 -10 -25 0 -5
--- a/tests/quadrilaterals.py	Tue Jan 23 14:09:52 2018 +0200
+++ b/tests/quadrilaterals.py	Tue Jan 23 15:30:48 2018 +0200
@@ -51,11 +51,19 @@
                 yield error(quadrilateral, 'self-intersecting')
                 break
 
+def collinear_test(model):
+    for element in model.body:
+        if hasattr(element, 'geometry') and len(element.geometry.vertices) >= 3:
+            for a, b, c in pairs(element.geometry.vertices, count = 3):
+                if cross_product(b - a, c - a).length() < 1e-5:
+                    yield error(element, 'collinearity-error')
+
 manifest = {
     'tests': {
         'skew': skew_test,
         'concave': concave_test,
         'bowtie': bowtie_test,
+        'collinearity': collinear_test,
     },
     'messages': {
         'skew-error': lambda skew_angle:
@@ -68,5 +76,6 @@
             ),
         'concave-error': 'concave quadrilateral',
         'self-intersecting': 'bowtie quadrilateral',
+        'collinearity-error': 'collinear polygon',
     },
 }

mercurial