Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/g-curve-t-at-length-negative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@joint/core": patch
---

g.Curve - fix `tAtLength()` and `pointAtLength()` to measure a negative length from the end of the curve
5 changes: 5 additions & 0 deletions .changeset/g-path-point-at-length-copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@joint/core": patch
---

g.Path - fix `pointAtLength()` to return a new point for a length beyond the path
5 changes: 5 additions & 0 deletions .changeset/g-polyline-contains-point-plain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@joint/core": patch
---

g.Polyline - fix `containsPoint()` to accept a plain `{ x, y }` object
5 changes: 5 additions & 0 deletions .changeset/g-polyline-contains-point-vertex.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/joint-core/src/g/curve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
16 changes: 7 additions & 9 deletions packages/joint-core/src/g/path.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -887,7 +885,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];
Expand Down
47 changes: 47 additions & 0 deletions packages/joint-core/src/g/polyline.containsPoint.mjs
Original file line number Diff line number Diff line change
@@ -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;
};
53 changes: 2 additions & 51 deletions packages/joint-core/src/g/polyline.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -159,57 +160,7 @@ Polyline.prototype = {
// Closes open polylines (always imagines a final closing segment).
containsPoint: function(p) {

var points = this.points;
var 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)
}

// move to check the next polyline segment
startIndex = endIndex;
}

// returns `true` for odd numbers of intersections (even-odd algorithm)
return ((numIntersections % 2) === 1);
return containsPoint(this.points, p.x, p.y);
},

close: function() {
Expand Down
12 changes: 12 additions & 0 deletions packages/joint-core/test/geometry/curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions packages/joint-core/test/geometry/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
62 changes: 62 additions & 0 deletions packages/joint-core/test/geometry/polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading