summaryrefslogtreecommitdiffstats
path: root/src/math/line.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/line.ts')
-rw-r--r--src/math/line.ts45
1 files changed, 24 insertions, 21 deletions
diff --git a/src/math/line.ts b/src/math/line.ts
index 98e126b..902c9b6 100644
--- a/src/math/line.ts
+++ b/src/math/line.ts
@@ -37,9 +37,23 @@ export class Line {
const d2 = d / crossz(this.v, l2.v);
return vec2.scaleAndAdd(out, l2.p, l2.v, d2);
}
+}
+
+export class Movement {
+ public readonly v: vec2;
- public equals(l2: Line): boolean {
- return vec2.equals(this.p, l2.p) && vec2.equals(this.v, l2.v);
+ constructor(
+ public readonly p1: vec2,
+ public readonly p2: vec2,
+ ) {
+ this.v = vec2.sub(vec2.create(), p2, p1);
+ }
+
+ public intersectLine(out: vec2, l: Line): vec2 {
+ const vp = vec2.sub(vec2.create(), l.p, this.p1);
+ const d = crossz(vp, this.v);
+ const d2 = d / crossz(this.v, l.v);
+ return vec2.scaleAndAdd(out, l.p, l.v, d2);
}
}
@@ -69,30 +83,19 @@ export class LineSegment extends Line {
return (d >= 0 && d <= this.l);
}
- public collidesLine(l2: Line): boolean {
- const x = this.intersectLine(vec2.create(), l2);
- return this.collidesPoint(x);
- }
-
- public collidesLineSegment(l2: LineSegment): boolean {
- const x = this.intersectLine(vec2.create(), l2);
- if (!this.collidesPoint(x))
- return false;
- if (!l2.collidesPoint(x))
+ public collidesMove(out: vec2, move: Movement): boolean {
+ if (this.distancePoint(move.p1) < 0)
return false;
- return true;
- }
+ if (this.distancePoint(move.p2) >= 0)
+ return false;
- public collidesMove(move: LineSegment): boolean {
- if (!this.collidesLineSegment(move))
+ const x = move.intersectLine(vec2.create(), this);
+ if (!this.collidesPoint(x))
return false;
- const p2 = move.getP2(vec2.create());
- return this.distancePoint(p2) < 0;
- }
+ this.projectPoint(out, move.p2);
- public equals(l2: LineSegment): boolean {
- return super.equals(l2) && this.l === l2.l;
+ return true;
}
}