From 731f2ed157b4520b89243692567c5e29b29f0937 Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Sat, 14 Nov 2026 21:58:00 +0100 Subject: [PATCH 1/2] fix(g): polyline containsPoint through a vertex, curve negative length, path pointAtLength copy --- .changeset/g-curve-t-at-length-negative.md | 5 ++ .changeset/g-path-point-at-length-copy.md | 5 ++ .changeset/g-polyline-contains-point-plain.md | 5 ++ .../g-polyline-contains-point-vertex.md | 5 ++ packages/joint-core/src/g/curve.mjs | 2 +- packages/joint-core/src/g/path.mjs | 2 +- packages/joint-core/src/g/polyline.mjs | 81 +++++++++---------- packages/joint-core/test/geometry/curve.js | 12 +++ packages/joint-core/test/geometry/path.js | 16 ++++ packages/joint-core/test/geometry/polyline.js | 62 ++++++++++++++ 10 files changed, 149 insertions(+), 46 deletions(-) create mode 100644 .changeset/g-curve-t-at-length-negative.md create mode 100644 .changeset/g-path-point-at-length-copy.md create mode 100644 .changeset/g-polyline-contains-point-plain.md create mode 100644 .changeset/g-polyline-contains-point-vertex.md diff --git a/.changeset/g-curve-t-at-length-negative.md b/.changeset/g-curve-t-at-length-negative.md new file mode 100644 index 0000000000..6ccf97f872 --- /dev/null +++ b/.changeset/g-curve-t-at-length-negative.md @@ -0,0 +1,5 @@ +--- +"@joint/core": patch +--- + +g.Curve - fix `tAtLength()` and `pointAtLength()` to measure a negative length from the end of the curve diff --git a/.changeset/g-path-point-at-length-copy.md b/.changeset/g-path-point-at-length-copy.md new file mode 100644 index 0000000000..2c30b296de --- /dev/null +++ b/.changeset/g-path-point-at-length-copy.md @@ -0,0 +1,5 @@ +--- +"@joint/core": patch +--- + +g.Path - fix `pointAtLength()` to return a new point for a length beyond the path diff --git a/.changeset/g-polyline-contains-point-plain.md b/.changeset/g-polyline-contains-point-plain.md new file mode 100644 index 0000000000..5f4003821c --- /dev/null +++ b/.changeset/g-polyline-contains-point-plain.md @@ -0,0 +1,5 @@ +--- +"@joint/core": patch +--- + +g.Polyline - fix `containsPoint()` to accept a plain `{ x, y }` object diff --git a/.changeset/g-polyline-contains-point-vertex.md b/.changeset/g-polyline-contains-point-vertex.md new file mode 100644 index 0000000000..40400be37c --- /dev/null +++ b/.changeset/g-polyline-contains-point-vertex.md @@ -0,0 +1,5 @@ +--- +"@joint/core": patch +--- + +g.Polyline - fix `containsPoint()` to return `true` for a point inside when the horizontal ray from the point goes through a vertex diff --git a/packages/joint-core/src/g/curve.mjs b/packages/joint-core/src/g/curve.mjs index 35c33686d3..e2257df238 100644 --- a/packages/joint-core/src/g/curve.mjs +++ b/packages/joint-core/src/g/curve.mjs @@ -951,7 +951,7 @@ Curve.prototype = { for (var i = 0; i < n; i++) { var index = (fromStart ? i : (n - 1 - i)); - var currentSubdivision = subdivisions[i]; + var currentSubdivision = subdivisions[index]; var d = currentSubdivision.endpointDistance(); // length of current subdivision if (length <= (l + d)) { diff --git a/packages/joint-core/src/g/path.mjs b/packages/joint-core/src/g/path.mjs index af8d8262ad..2beeca6892 100644 --- a/packages/joint-core/src/g/path.mjs +++ b/packages/joint-core/src/g/path.mjs @@ -887,7 +887,7 @@ Path.prototype = { } // if length requested is higher than the length of the path, return last visible segment endpoint - if (lastVisibleSegment) return (fromStart ? lastVisibleSegment.end : lastVisibleSegment.start); + if (lastVisibleSegment) return (fromStart ? lastVisibleSegment.end : lastVisibleSegment.start).clone(); // if no visible segment, return last segment end point (no matter if fromStart or no) var lastSegment = segments[numSegments - 1]; diff --git a/packages/joint-core/src/g/polyline.mjs b/packages/joint-core/src/g/polyline.mjs index dd5479b1ac..38b42f5e83 100644 --- a/packages/joint-core/src/g/polyline.mjs +++ b/packages/joint-core/src/g/polyline.mjs @@ -159,57 +159,50 @@ Polyline.prototype = { // Closes open polylines (always imagines a final closing segment). containsPoint: function(p) { - var points = this.points; - var numPoints = points.length; + const points = this.points; + const numPoints = points.length; if (numPoints === 0) return false; // shortcut (this polyline has no points) - var x = p.x; - var y = p.y; - - // initialize a final closing segment by creating one from last-first points on polyline - var startIndex = numPoints - 1; // start of current polyline segment - var endIndex = 0; // end of current polyline segment - var numIntersections = 0; - var segment = new Line(); - var ray = new Line(); - var rayEnd = new Point(); - for (; endIndex < numPoints; endIndex++) { - var start = points[startIndex]; - var end = points[endIndex]; - if (p.equals(start)) return true; // shortcut (`p` is a point on polyline) - // current polyline segment - segment.start = start; - segment.end = end; - if (segment.containsPoint(p)) return true; // shortcut (`p` lies on a polyline segment) - - // do we have an intersection? - if (((y <= start.y) && (y > end.y)) || ((y > start.y) && (y <= end.y))) { - // this conditional branch IS NOT entered when `segment` is collinear/coincident with `ray` - // (when `y === start.y === end.y`) - // this conditional branch IS entered when `segment` touches `ray` at only one point - // (e.g. when `y === start.y !== end.y`) - // since this branch is entered again for the following segment, the two touches cancel out - - var xDifference = (((start.x - x) > (end.x - x)) ? (start.x - x) : (end.x - x)); - if (xDifference >= 0) { - // segment lies at least partially to the right of `p` - rayEnd.x = x + xDifference; - rayEnd.y = y; // right - ray.start = p; - ray.end = rayEnd; - if (segment.intersect(ray)) { - // an intersection was detected to the right of `p` - numIntersections++; - } - } // else: `segment` lies completely to the left of `p` (i.e. no intersection to the right) + const x = p.x; + const y = p.y; + + let isInside = false; + // the first segment is the closing one (from the last point to the first point) + let start = points[numPoints - 1]; + for (let i = 0; i < numPoints; i++) { + const end = points[i]; + const startX = start.x; + const startY = start.y; + const endX = end.x; + const endY = end.y; + + // the side of the segment the point `p` is on (`0` = `p` is collinear with the segment) + const side = ((endX - startX) * (y - startY)) - ((endY - startY) * (x - startX)); + if (side === 0) { + const isBetweenX = (startX <= endX) ? (x >= startX && x <= endX) : (x >= endX && x <= startX); + const isBetweenY = (startY <= endY) ? (y >= startY && y <= endY) : (y >= endY && y <= startY); + if (isBetweenX && isBetweenY) return true; // shortcut (`p` lies on a polyline segment) + } + + // Does the horizontal ray to the right of `p` cross the segment? + // A segment collinear with the ray (`y === startY === endY`) is never counted. + // A segment is counted with only one of its ends, so a ray through a vertex + // counts one crossing when the polyline crosses the ray there, and none or two + // when it only touches it. + if (startY >= y) { + // the segment goes up, the crossing is to the right of `p` if `p` is on its left + if (endY < y && side < 0) isInside = !isInside; + } else { + // the segment goes down + if (endY >= y && side > 0) isInside = !isInside; } // move to check the next polyline segment - startIndex = endIndex; + start = end; } - // returns `true` for odd numbers of intersections (even-odd algorithm) - return ((numIntersections % 2) === 1); + // `true` for an odd number of crossings (even-odd algorithm) + return isInside; }, close: function() { diff --git a/packages/joint-core/test/geometry/curve.js b/packages/joint-core/test/geometry/curve.js index f7d14ccc7c..f32e446352 100644 --- a/packages/joint-core/test/geometry/curve.js +++ b/packages/joint-core/test/geometry/curve.js @@ -1118,6 +1118,18 @@ QUnit.module('curve', function() { assert.equal(curve.pointAtLength(-10000).toString(), '0@0'); }); + QUnit.test('a negative length is measured from the end of the curve', function(assert) { + + // not symmetric: the subdivisions do not have the same length + const curve = new g.Curve('0 0', '0 100', '10 100', '200 0'); + const length = curve.length(); + [10, 30, 100, 150].forEach(function(distance) { + const fromEnd = curve.pointAtLength(-distance); + const fromStart = curve.pointAtLength(length - distance); + assert.ok(fromEnd.distance(fromStart) < 1, 'distance from the end: ' + distance); + }); + }); + QUnit.test('compare to browser implementation', function(assert) { var p1; diff --git a/packages/joint-core/test/geometry/path.js b/packages/joint-core/test/geometry/path.js index 3d3d9b4731..a9a7b63e2b 100644 --- a/packages/joint-core/test/geometry/path.js +++ b/packages/joint-core/test/geometry/path.js @@ -3277,6 +3277,22 @@ QUnit.module('path', function(hooks) { svg.remove(); }); + + QUnit.test('returns a new point for a length beyond the path', function(assert) { + + const path = new g.Path('M 0 0 L 10 0 L 10 10'); + + const end = path.pointAtLength(1000); + assert.equal(end.toString(), '10@10'); + assert.notEqual(end, path.segments[2].end); + end.x = 100; + assert.equal(path.serialize(), 'M 0 0 L 10 0 L 10 10'); + + const start = path.pointAtLength(-1000); + assert.equal(start.toString(), '0@0'); + start.x = 100; + assert.equal(path.serialize(), 'M 0 0 L 10 0 L 10 10'); + }); }); QUnit.module('pointAtT()', function() { diff --git a/packages/joint-core/test/geometry/polyline.js b/packages/joint-core/test/geometry/polyline.js index 66f574e7d3..3ea4e15964 100644 --- a/packages/joint-core/test/geometry/polyline.js +++ b/packages/joint-core/test/geometry/polyline.js @@ -586,6 +586,68 @@ QUnit.module('polyline', function() { point = new g.Point(15, 15); assert.equal(polyline.containsPoint(point), false, 'point in self-intersection = outside'); }); + + QUnit.test('returns true for a point inside when the horizontal ray goes through a vertex', function(assert) { + + const numPoints = 16; + const radius = 50; + const points = []; + for (let i = 0; i < numPoints; i++) { + const angle = 2 * Math.PI * i / numPoints; + points.push(new g.Point(radius * Math.cos(angle), radius * Math.sin(angle))); + } + const polyline = new g.Polyline(points); + + [-45, -17.5, -15, -10, -7.5, 0, 10, 45].forEach(function(x) { + assert.equal(polyline.containsPoint(new g.Point(x, 0)), true, 'inside: x = ' + x); + }); + [-60, -50.5, 50.5, 60].forEach(function(x) { + assert.equal(polyline.containsPoint(new g.Point(x, 0)), false, 'outside: x = ' + x); + }); + }); + + QUnit.test('counts a vertex on the ray once when the polyline crosses the ray, not when it touches it', function(assert) { + + // a diamond: the ray from the center goes through the right vertex + const diamond = new g.Polyline([ + new g.Point(0, 10), + new g.Point(10, 0), + new g.Point(20, 10), + new g.Point(10, 20) + ]); + assert.equal(diamond.containsPoint(new g.Point(10, 10)), true, 'ray crosses the polyline at a vertex'); + assert.equal(diamond.containsPoint(new g.Point(-5, 10)), false, 'ray through two vertices'); + assert.equal(diamond.containsPoint(new g.Point(5, 0)), false, 'ray touches the top vertex'); + assert.equal(diamond.containsPoint(new g.Point(5, 20)), false, 'ray touches the bottom vertex'); + + // a shape with a horizontal side on the ray + const step = new g.Polyline([ + new g.Point(0, 0), + new g.Point(30, 0), + new g.Point(30, 10), + new g.Point(20, 10), + new g.Point(20, 20), + new g.Point(0, 20) + ]); + assert.equal(step.containsPoint(new g.Point(10, 10)), true, 'ray along a side, point inside'); + assert.equal(step.containsPoint(new g.Point(-5, 10)), false, 'ray along a side, point outside'); + assert.equal(step.containsPoint(new g.Point(25, 10)), true, 'point on the side'); + assert.equal(step.containsPoint(new g.Point(35, 10)), false, 'point collinear with the side, outside'); + }); + + QUnit.test('accepts a plain point', function(assert) { + + const polyline = new g.Polyline([ + new g.Point(0, 0), + new g.Point(20, 0), + new g.Point(20, 20), + new g.Point(0, 20) + ]); + assert.equal(polyline.containsPoint({ x: 10, y: 10 }), true, 'inside'); + assert.equal(polyline.containsPoint({ x: 0, y: 0 }), true, 'on a vertex'); + assert.equal(polyline.containsPoint({ x: 20, y: 10 }), true, 'on a side'); + assert.equal(polyline.containsPoint({ x: 30, y: 10 }), false, 'outside'); + }); }); QUnit.module('convexHull()', function(assert) { From a3319c84faeaf4b5b18f9f24903437532d9fbfc8 Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Sun, 15 Nov 2026 13:15:50 +0100 Subject: [PATCH 2/2] refactor(g): move the point in polyline test to a function that takes the points, use it in Path.containsPoint() --- packages/joint-core/src/g/path.mjs | 14 +++--- .../src/g/polyline.containsPoint.mjs | 47 +++++++++++++++++++ packages/joint-core/src/g/polyline.mjs | 46 +----------------- 3 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 packages/joint-core/src/g/polyline.containsPoint.mjs diff --git a/packages/joint-core/src/g/path.mjs b/packages/joint-core/src/g/path.mjs index 2beeca6892..7b4d0cdff4 100644 --- a/packages/joint-core/src/g/path.mjs +++ b/packages/joint-core/src/g/path.mjs @@ -8,6 +8,7 @@ import { Curve } from './curve.mjs'; import { types } from './types.mjs'; import { extend } from './extend.mjs'; import { intersection } from './line.intersection.mjs'; +import { containsPoint } from './polyline.containsPoint.mjs'; import { toPlainMatrix, transformPoint } from './matrix.helpers.mjs'; import { KAPPA } from './geometry.helpers.mjs'; @@ -404,19 +405,16 @@ Path.prototype = { // Precision may be adjusted by passing an `opt` object. containsPoint: function(p, opt) { - // the points of the subpaths are only read here, so they are not cloned into polylines of their own const subpaths = this.toPoints(opt); if (!subpaths) return false; // shortcut (this path has no polylines) - // how many component polylines does `p` lie within? + const x = p.x; + const y = p.y; + + // how many subpaths does `p` lie within? let numIntersections = 0; - const polyline = new Polyline(); for (let i = 0, n = subpaths.length; i < n; i++) { - polyline.points = subpaths[i]; - if (polyline.containsPoint(p)) { - // `p` lies within this polyline - numIntersections++; - } + if (containsPoint(subpaths[i], x, y)) numIntersections++; } // returns `true` for odd numbers of intersections (even-odd algorithm) diff --git a/packages/joint-core/src/g/polyline.containsPoint.mjs b/packages/joint-core/src/g/polyline.containsPoint.mjs new file mode 100644 index 0000000000..4e5ebd0518 --- /dev/null +++ b/packages/joint-core/src/g/polyline.containsPoint.mjs @@ -0,0 +1,47 @@ +// Returns `true` if the area surrounded by the points contains the point `x`, `y`. +// Implements the even-odd SVG algorithm (self-intersections are "outside"). +// (Uses a horizontal ray to the right of the point to look for intersections.) +// Closes an open shape (always imagines a final closing segment). +export const containsPoint = function(points, x, y) { + + const numPoints = points.length; + if (numPoints === 0) return false; // shortcut (there are no points) + + let isInside = false; + // the first segment is the closing one (from the last point to the first point) + let start = points[numPoints - 1]; + for (let i = 0; i < numPoints; i++) { + const end = points[i]; + const startX = start.x; + const startY = start.y; + const endX = end.x; + const endY = end.y; + + // the side of the segment the point is on (`0` = the point is collinear with the segment) + const side = ((endX - startX) * (y - startY)) - ((endY - startY) * (x - startX)); + if (side === 0) { + const isBetweenX = (startX <= endX) ? (x >= startX && x <= endX) : (x >= endX && x <= startX); + const isBetweenY = (startY <= endY) ? (y >= startY && y <= endY) : (y >= endY && y <= startY); + if (isBetweenX && isBetweenY) return true; // shortcut (the point lies on a segment) + } + + // Does the horizontal ray to the right of the point cross the segment? + // A segment collinear with the ray (`y === startY === endY`) is never counted. + // A segment is counted with only one of its ends, so a ray through a vertex + // counts one crossing when the shape crosses the ray there, and none or two + // when it only touches it. + if (startY >= y) { + // the segment goes up, the crossing is to the right of the point if the point is on its left + if (endY < y && side < 0) isInside = !isInside; + } else { + // the segment goes down + if (endY >= y && side > 0) isInside = !isInside; + } + + // move to check the next segment + start = end; + } + + // `true` for an odd number of crossings (even-odd algorithm) + return isInside; +}; diff --git a/packages/joint-core/src/g/polyline.mjs b/packages/joint-core/src/g/polyline.mjs index 38b42f5e83..be7415f092 100644 --- a/packages/joint-core/src/g/polyline.mjs +++ b/packages/joint-core/src/g/polyline.mjs @@ -3,6 +3,7 @@ import { Point } from './point.mjs'; import { Line } from './line.mjs'; import { intersection } from './line.intersection.mjs'; import { ensurePoint } from './point.ensure.mjs'; +import { containsPoint } from './polyline.containsPoint.mjs'; import { types } from './types.mjs'; import { toPlainMatrix, transformPoint } from './matrix.helpers.mjs'; import { clonePoints, parsePoints, convexHull } from './points.mjs'; @@ -159,50 +160,7 @@ Polyline.prototype = { // Closes open polylines (always imagines a final closing segment). containsPoint: function(p) { - const points = this.points; - const numPoints = points.length; - if (numPoints === 0) return false; // shortcut (this polyline has no points) - - const x = p.x; - const y = p.y; - - let isInside = false; - // the first segment is the closing one (from the last point to the first point) - let start = points[numPoints - 1]; - for (let i = 0; i < numPoints; i++) { - const end = points[i]; - const startX = start.x; - const startY = start.y; - const endX = end.x; - const endY = end.y; - - // the side of the segment the point `p` is on (`0` = `p` is collinear with the segment) - const side = ((endX - startX) * (y - startY)) - ((endY - startY) * (x - startX)); - if (side === 0) { - const isBetweenX = (startX <= endX) ? (x >= startX && x <= endX) : (x >= endX && x <= startX); - const isBetweenY = (startY <= endY) ? (y >= startY && y <= endY) : (y >= endY && y <= startY); - if (isBetweenX && isBetweenY) return true; // shortcut (`p` lies on a polyline segment) - } - - // Does the horizontal ray to the right of `p` cross the segment? - // A segment collinear with the ray (`y === startY === endY`) is never counted. - // A segment is counted with only one of its ends, so a ray through a vertex - // counts one crossing when the polyline crosses the ray there, and none or two - // when it only touches it. - if (startY >= y) { - // the segment goes up, the crossing is to the right of `p` if `p` is on its left - if (endY < y && side < 0) isInside = !isInside; - } else { - // the segment goes down - if (endY >= y && side > 0) isInside = !isInside; - } - - // move to check the next polyline segment - start = end; - } - - // `true` for an odd number of crossings (even-odd algorithm) - return isInside; + return containsPoint(this.points, p.x, p.y); }, close: function() {