From 4bcb1cfd053c05062be43ade199726d3b538dc6c Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Sun, 15 Nov 2026 10:42:00 -0700 Subject: [PATCH 1/3] Normalize line coefficients for point containment Updated the contains method to normalize the line coefficients before calculating the dot product, improving accuracy in point containment tests. --- spatialmath/geom2d.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spatialmath/geom2d.py b/spatialmath/geom2d.py index ee42c08f..1f3df350 100755 --- a/spatialmath/geom2d.py +++ b/spatialmath/geom2d.py @@ -146,6 +146,10 @@ def contains(self, p: ArrayLike2, tol: float = 20) -> bool: """ Test if point is in line + For a Euclidean point (or a homogeneous point with unit weight), + ``tol`` bounds the perpendicular distance in units of machine epsilon, + independently of a nonzero scale on the line coefficients. + :param p1: point to test :type p1: array_like(2) or array_like(3) :param tol: tolerance in units of eps, defaults to 20 @@ -156,7 +160,8 @@ def contains(self, p: ArrayLike2, tol: float = 20) -> bool: p = smb.getvector(p) if len(p) == 2: p = np.r_[p, 1] - return abs(np.dot(self.line, p)) < tol * _eps + normal = np.hypot(self.line[0], self.line[1]) + return abs(np.dot(self.line / normal, p)) < tol * _eps # variant that gives lambda From ab4dabf3cd9397ec91656daf9c801ffdeddef458 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Sun, 15 Nov 2026 10:42:33 -0700 Subject: [PATCH 2/3] Test scale-independent line containment and polygon queries Add tests for Line2 and Polygon2 scale invariance and geometric tolerance. --- tests/tests/test_line2_contains_scale.py | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/tests/test_line2_contains_scale.py diff --git a/tests/tests/test_line2_contains_scale.py b/tests/tests/test_line2_contains_scale.py new file mode 100644 index 00000000..a07a7082 --- /dev/null +++ b/tests/tests/test_line2_contains_scale.py @@ -0,0 +1,35 @@ +import numpy as np +import pytest + +from spatialmath.geom2d import Line2, Polygon2 + + +@pytest.mark.parametrize("scale", [1, -1, 1e-20, -1e-20, 1e-8, -1e8, 1e20]) +@pytest.mark.parametrize( + "coefficients,on,off", + [([1, 0, -1], [1, 2], [2, 2]), ([3, 4, -10], [2, 1], [2, 2])], +) +def test_contains_coefficient_scale(scale, coefficients, on, off): + line = Line2(scale * np.array(coefficients)) + assert line.contains(on) + assert not line.contains(off) + assert line.contains(np.r_[on, 1]) + assert not line.contains(np.r_[off, 1]) + + +@pytest.mark.parametrize("scale", [1, -1, 1e-8, -1e8]) +def test_contains_geometric_tolerance(scale): + eps = np.finfo(float).eps + line = Line2(scale * np.array([0, 1, 0])) + assert line.contains([0, 10 * eps]) + assert not line.contains([0, 30 * eps]) + assert line.contains([0, 30 * eps], tol=40) + + +@pytest.mark.parametrize("scale", [1, 1e-20, -1e-20, 1e8]) +def test_polygon_line_nonintersection_is_scale_invariant(scale): + polygon = Polygon2([(-1, -1), (1, -1), (1, 1), (-1, 1)]) + outside = Line2(scale * np.array([0, 1, -2])) + crossing = Line2(scale * np.array([0, 1, 0])) + assert not polygon.intersects(outside) + assert polygon.intersects(crossing) From a714a48e5f087036950b14fd6a766567a3e12fc8 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Sun, 15 Nov 2026 10:43:08 -0700 Subject: [PATCH 3/3] Place containment regression tests in the tests directory --- tests/{tests => }/test_line2_contains_scale.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{tests => }/test_line2_contains_scale.py (100%) diff --git a/tests/tests/test_line2_contains_scale.py b/tests/test_line2_contains_scale.py similarity index 100% rename from tests/tests/test_line2_contains_scale.py rename to tests/test_line2_contains_scale.py