From b9a435e1c3ec06eaae01722a1e7def44994ac33b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Perdig=C3=A3o?= Date: Thu, 15 May 2025 17:40:29 -0300 Subject: [PATCH] Fix polyline tool calculation when dealing with 180 degrees angles. --- src/bonsai/bonsai/tool/cad.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/bonsai/bonsai/tool/cad.py b/src/bonsai/bonsai/tool/cad.py index 62c2b854ab..49d77f5691 100644 --- a/src/bonsai/bonsai/tool/cad.py +++ b/src/bonsai/bonsai/tool/cad.py @@ -102,6 +102,10 @@ class Cad: d1 = v1 - v2 d2 = v3 - v2 + # Rounding avoids problems when dealing with 180 degrees + d1 = Vector((round(c, 6) for c in d1)) + d2 = Vector((round(c, 6) for c in d2)) + d1.normalize() d2.normalize() @@ -123,12 +127,24 @@ class Cad: if new_angle is not None: rot_mat = Matrix.Rotation(new_angle, 3, axis) rot_vector = (d1 @ rot_mat) if parameter else (rot_mat @ d1) + + # 180 degrees special cases + if abs(round(a, 4)) == round(math.pi, 4) and ( + rot_vector.x == 0.0 and rot_vector.y == 0.0 or + rot_vector.x == 0.0 and rot_vector.z == 0.0 or + rot_vector.y == 0.0 and rot_vector.z == 0.0 + ): + rot_vector *= -1 return rot_vector else: sign = -1 if parameter else 1 if degrees: a = math.degrees(a) + + # 180 degrees special cases + if abs(round(a, 2)) == abs(180.00): + return -180.0 return a * sign else: return a