Skip to content

Commit

Permalink
chore(TS): types and tweaks for intersection file (#8603)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored and ShaMan123 committed Mar 16, 2024
1 parent 4390d5d commit a22dc8e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- fix(): regression of canvas migration with pointer and sendPointToPlane [#8563](https://github.com/fabricjs/fabric.js/pull/8563)
\- chore(TS): Add declare in front of properties that are type definitions. [#8574](https://github.com/fabricjs/fabric.js/pull/8574)
- refactor(Animation): modernize IText cursor animation based on animation API changes (and fix minor regression) plus leftovers from #8547 [#8583](https://github.com/fabricjs/fabric.js/pull/8583)
- chore(TS): Intersection class, finalize TS [#8603](https://github.com/fabricjs/fabric.js/pull/8603)
- chore(TS): Update Pattern to remove ts-nocheck and added types where missing [#8605](https://github.com/fabricjs/fabric.js/pull/8605)
- chore(TS): Followup for interactivy and controls migration to TS [#8404](https://github.com/fabricjs/fabric.js/pull/8404)
- refactor(IText): Fixes Draggable Text for retina and viewport transform #8534
Expand Down
83 changes: 59 additions & 24 deletions src/intersection.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ts-nocheck
import { Point } from './point.class';

/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
Expand Down Expand Up @@ -32,11 +31,11 @@ export class Intersection {
}

/**
*
* Used to verify if a point is alredy in the collection
* @param {Point} point
* @returns
* @returns {boolean}
*/
contains(point) {
private includes(point: Point): boolean {
return this.points.some((p) => p.eq(point));
}

Expand All @@ -46,10 +45,10 @@ export class Intersection {
* @return {Intersection} thisArg
* @chainable
*/
private append(...points) {
private append(...points: Point[]): Intersection {
this.points = this.points.concat(
points.filter((point) => {
return !this.contains(point);
return !this.includes(point);
})
);
return this;
Expand All @@ -66,24 +65,35 @@ export class Intersection {
* @param {boolean} [bInfinite=true] check segment intersection by passing `false`
* @return {Intersection}
*/
static intersectLineLine(a1, a2, b1, b2, aInfinite = true, bInfinite = true) {
let result;
const uaT = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x),
ubT = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x),
uB = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
static intersectLineLine(
a1: Point,
a2: Point,
b1: Point,
b2: Point,
aInfinite = true,
bInfinite = true
): Intersection {
const b2xb1x = b2.x - b1.x,
a1yb1y = a1.y - b1.y,
b2yb1y = b2.y - b1.y,
a1xb1x = a1.x - b1.x,
a2ya1y = a2.y - a1.y,
a2xa1x = a2.x - a1.x,
uaT = b2xb1x * a1yb1y - b2yb1y * a1xb1x,
ubT = a2xa1x * a1yb1y - a2ya1y * a1xb1x,
uB = b2yb1y * a2xa1x - b2xb1x * a2ya1y;
if (uB !== 0) {
const ua = uaT / uB,
ub = ubT / uB;
if (
(aInfinite || (0 <= ua && ua <= 1)) &&
(bInfinite || (0 <= ub && ub <= 1))
) {
result = new Intersection('Intersection');
result.append(
new Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y))
return new Intersection('Intersection').append(
new Point(a1.x + ua * a2xa1x, a1.y + ua * a2ya1y)
);
} else {
result = new Intersection();
return new Intersection();
}
} else {
if (uaT === 0 || ubT === 0) {
Expand All @@ -94,12 +104,11 @@ export class Intersection {
isContainedInInterval(a2, b1, b2) ||
isContainedInInterval(b1, a1, a2) ||
isContainedInInterval(b2, a1, a2);
result = new Intersection(segmentsCoincide ? 'Coincident' : undefined);
return new Intersection(segmentsCoincide ? 'Coincident' : undefined);
} else {
result = new Intersection('Parallel');
return new Intersection('Parallel');
}
}
return result;
}

/**
Expand All @@ -112,7 +121,12 @@ export class Intersection {
* @param {Point} l2 other point on line
* @return {Intersection}
*/
static intersectSegmentLine(s1, s2, l1, l2) {
static intersectSegmentLine(
s1: Point,
s2: Point,
l1: Point,
l2: Point
): Intersection {
return Intersection.intersectLineLine(s1, s2, l1, l2, false, true);
}

Expand All @@ -126,7 +140,12 @@ export class Intersection {
* @param {Point} b2 other boundary point of segment
* @return {Intersection}
*/
static intersectSegmentSegment(a1, a2, b1, b2) {
static intersectSegmentSegment(
a1: Point,
a2: Point,
b1: Point,
b2: Point
): Intersection {
return Intersection.intersectLineLine(a1, a2, b1, b2, false, false);
}

Expand All @@ -143,7 +162,12 @@ export class Intersection {
* @param {boolean} [infinite=true] check segment intersection by passing `false`
* @return {Intersection}
*/
static intersectLinePolygon(a1, a2, points, infinite = true) {
static intersectLinePolygon(
a1: Point,
a2: Point,
points: Point[],
infinite = true
): Intersection {
const result = new Intersection();
const length = points.length;

Expand Down Expand Up @@ -173,7 +197,11 @@ export class Intersection {
* @param {Point[]} points polygon points
* @return {Intersection}
*/
static intersectSegmentPolygon(a1, a2, points) {
static intersectSegmentPolygon(
a1: Point,
a2: Point,
points: Point[]
): Intersection {
return Intersection.intersectLinePolygon(a1, a2, points, false);
}

Expand All @@ -187,7 +215,10 @@ export class Intersection {
* @param {Point[]} points2
* @return {Intersection}
*/
static intersectPolygonPolygon(points1, points2) {
static intersectPolygonPolygon(
points1: Point[],
points2: Point[]
): Intersection {
const result = new Intersection(),
length = points1.length;
const coincidences = [];
Expand Down Expand Up @@ -222,7 +253,11 @@ export class Intersection {
* @param {Point} r2 bottom right point of rect
* @return {Intersection}
*/
static intersectPolygonRectangle(points, r1, r2) {
static intersectPolygonRectangle(
points: Point[],
r1: Point,
r2: Point
): Intersection {
const min = r1.min(r2),
max = r1.max(r2),
topRight = new Point(max.x, min.y),
Expand Down

0 comments on commit a22dc8e

Please sign in to comment.