summaryrefslogtreecommitdiffstats
path: root/src/math/line.ts
blob: db99035abba3ccbfd581bb86df00876982880963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { mat2, vec2 } from 'gl-matrix';
import { Collidable } from './collision';

const rot90 = mat2.fromValues(
	0, 1,
	-1, 0,
);

export function normal(out: vec2, a: vec2): vec2 {
	return vec2.transformMat2(out, a, rot90);
}

export function crossz(a: vec2, b: vec2): number {
	return a[0] * b[1] - a[1] * b[0];
}

export class Line {
	constructor(
		public readonly p: vec2,
		public readonly v: vec2,
	) {}

	public getNormal(out: vec2): vec2 {
		return normal(out, this.v);
	}

	public projectPointDistance(p2: vec2): number {
		const v2 = vec2.sub(vec2.create(), p2, this.p);
		return vec2.dot(this.v, v2);
	}

	public projectPoint(out: vec2, p2: vec2): vec2 {
		const d = this.projectPointDistance(p2);
		return vec2.scaleAndAdd(out, this.p, this.v, d);
	}

	public distancePoint(p2: vec2): number {
		const v2 = vec2.sub(vec2.create(), p2, this.p);
		return crossz(this.v, v2);
	}

	public intersectLine(out: vec2, l2: Line): vec2 {
		const vp = vec2.sub(vec2.create(), l2.p, this.p);
		const d = crossz(vp, this.v);
		const d2 = d / crossz(this.v, l2.v);
		return vec2.scaleAndAdd(out, l2.p, l2.v, d2);
	}
}

export class Movement {
	public readonly v: vec2;

	constructor(
		public readonly src: vec2,
		public readonly dest: vec2,
	) {
		this.v = vec2.sub(vec2.create(), dest, src);
	}

	public intersectLine(out: vec2, l: Line): vec2 {
		const vp = vec2.sub(vec2.create(), l.p, this.src);
		const d = crossz(vp, this.v);
		const d2 = d / crossz(this.v, l.v);
		return vec2.scaleAndAdd(out, l.p, l.v, d2);
	}

	public passes(p: vec2): boolean {
		const vp = vec2.sub(vec2.create(), p, this.src);
		const d = vec2.dot(this.v, vp);
		return d >= 0 && d <= vec2.sqrLen(this.v);
	}

	public toLineSegment(): LineSegment {
		return LineSegment.fromPoints(this.src, this.dest);
	}

	public translate(t: vec2): Movement {
		const src = vec2.add(vec2.create(), this.src, t);
		const dest = vec2.add(vec2.create(), this.dest, t);
		return new Movement(src, dest);
	}
}

export class LineSegment extends Line implements Collidable {
	public static fromPoints(p1: vec2, p2: vec2): LineSegment {
		const d = vec2.dist(p1, p2);
		const v = vec2.sub(vec2.create(), p2, p1);
		vec2.scale(v, v, 1 / d);

		return new LineSegment(p1, v, d);
	}

	constructor(
		p: vec2,
		v: vec2,
		public readonly l: number,
	) {
		super(p, v);
	}

	public getP2(out: vec2): vec2 {
		return vec2.scaleAndAdd(out, this.p, this.v, this.l);
	}

	public containsPoint(p2: vec2): boolean {
		const d = this.projectPointDistance(p2);
		return (d >= 0 && d <= this.l);
	}

	public collide(out: vec2, move: Movement, r: number): boolean {
		if (this.distancePoint(move.src) < 0)
			return false;

		if (crossz(move.v, this.v) < 0)
			return false;

		const t = this.getNormal(vec2.create());
		vec2.scale(t, t, -r);

		const refMove = move.translate(t);

		if (!this.collideRef(out, refMove))
			return false;

		vec2.sub(out, out, t);
		return true;
	}

	private collideRef(out: vec2, move: Movement): boolean {
		if (this.distancePoint(move.dest) >= 0)
			return false;

		const x = move.intersectLine(vec2.create(), this);
		if (!this.containsPoint(x))
			return false;

		this.projectPoint(out, move.dest);

		return true;
	}
}