Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(TS): types and tweaks for intersection file #8603

Merged
merged 8 commits into from
Jan 18, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- 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;
Comment on lines +76 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am guessing all of this can be reduced by using dotProduct crossProduct
but it is a headache

Don't you prefect subtracting points and working with vectors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that as a fun thing when the rest is done, and only if i save some bytes, otherwise i see it as non useful

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