diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index fa37f15a7..27d454a34 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -24,6 +24,7 @@ ### Fixed - Input: a child cropped out of view by a container with `clipping`, or scrolled out of a `UIPanel`, no longer gets pointer events where it is not drawn +- Geometry: `Line#contains()` checks the segment endpoints as well as collinearity; a point on the line beyond either endpoint was reported as contained (thanks @mmporong) - UI: a `UITextButton` moved after it was built draws its background where it is, not where it was first put - UI: a blocking `UIPanel` stays opaque to hover for as long as the pointer is on it, not only on the frame the pointer enters - UI: a draggable `UIBaseElement` pressed with no hover first, as a touch tap is, no longer throws, and drags by touch; in the world, rather than floating, it no longer jumps by the camera position when dragged diff --git a/packages/melonjs/src/geometries/line.ts b/packages/melonjs/src/geometries/line.ts index 781cf7077..d69342996 100644 --- a/packages/melonjs/src/geometries/line.ts +++ b/packages/melonjs/src/geometries/line.ts @@ -48,7 +48,12 @@ export class Line extends Polygon { //(Cy - Ay) * (Bx - Ax) = (By - Ay) * (Cx - Ax) return ( - (_y - start.y) * (end.x - start.x) === (end.y - start.y) * (_x - start.x) + (_y - start.y) * (end.x - start.x) === + (end.y - start.y) * (_x - start.x) && + _x >= Math.min(start.x, end.x) && + _x <= Math.max(start.x, end.x) && + _y >= Math.min(start.y, end.y) && + _y <= Math.max(start.y, end.y) ); } diff --git a/packages/melonjs/tests/line.spec.ts b/packages/melonjs/tests/line.spec.ts index 1996de187..32c22a1ff 100644 --- a/packages/melonjs/tests/line.spec.ts +++ b/packages/melonjs/tests/line.spec.ts @@ -60,6 +60,11 @@ describe("Shape : Line", () => { expect(line.contains(29, 61)).toEqual(false); }); + it("does not contain collinear points beyond its endpoints", () => { + expect(line.contains(56, 120)).toEqual(false); + expect(line.contains(-28, -60)).toEqual(false); + }); + it("Line Bounding Rect width is 28", () => { expect(bounds.width).toEqual(28); });