-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
198 lines (168 loc) · 4.38 KB
/
vector.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// https://labs.minutelabs.io/toolkit/js/vector.js
///////////////////////////////////////////
// MinuteLabs.io Vector Library
// author: Jasper Palfree ([email protected])
// For educational use.
// Copyright 2020 Jasper Palfree
// License: GPLv3
///////////////////////////////////////////
function _drawArrow(ctx, x, y, length, angle, color) {
ctx.strokeStyle = color
ctx.fillStyle = color
ctx.lineWidth = 2
ctx.translate(x, y)
ctx.rotate(angle)
ctx.beginPath()
ctx.moveTo(0, 0)
ctx.lineTo(length, 0)
ctx.stroke()
ctx.lineTo(length - 6, 6)
ctx.lineTo(length - 6, -6)
ctx.lineTo(length, 0)
ctx.fill()
ctx.rotate(-angle)
ctx.translate(-x, -y)
}
class Vector {
constructor(x = 0, y = 0) {
this.set(x, y)
}
clone() {
return new Vector(this.x, this.y)
}
set(x, y) {
this.x = x
this.y = y
return this
}
add(other) {
this.x += other.x
this.y += other.y
return this
}
plus(other) {
return this.clone().add(other)
}
subtract(other) {
this.x -= other.x
this.y -= other.y
return this
}
minus(other) {
return this.clone().subtract(other)
}
multiply(number) {
this.x *= number
this.y *= number
return this
}
times(number) {
return this.clone().multiply(number)
}
divide(number) {
this.x /= number
this.y /= number
return this
}
dividedBy(number) {
return this.clone().divide(number)
}
normSq() {
return this.x * this.x + this.y * this.y
}
norm() {
return Math.sqrt(this.normSq())
}
setNorm(n) {
let norm = this.norm()
if (norm === 0) {
norm = 1
this.x = 1
this.y = 0
}
n /= norm
this.x *= n
this.y *= n
return this
}
normalize() {
return this.setNorm(1)
}
angle() {
return Math.atan2(this.y, this.x)
}
setAngle(angle) {
let n = this.norm()
if (n === 0) {
n = 1
this.x = 1
this.y = 0
}
this.x = n * Math.cos(angle)
this.y = n * Math.sin(angle)
return this
}
rotateBy(angle) {
return this.setAngle(this.angle() + angle)
}
dot(vector) {
return this.x * vector.x + this.y * vector.y
}
proj(vector) {
let other = vector.clone().normalize()
return other.multiply(this.dot(other))
}
projScalar(vector) {
return this.dot(vector) / vector.norm()
}
clampedProj(vector) {
let n = vector.norm()
let other = vector.clone().normalize()
return other.multiply(Math.min(n, Math.max(0, this.dot(other))))
}
clamp(min, max) {
this.x = Math.max(min.x, Math.min(max.x, this.x))
this.y = Math.max(min.y, Math.min(max.y, this.y))
return this
}
// perform a reflection with specified normal vector to the mirror
reflect(normal) {
let n = normal.normSq()
return this.subtract(normal.times(2 * this.dot(normal) / n))
}
// return a new vector that is the reflection along normal
reflection(normal) {
return this.copy().reflect(normal)
}
randomize(n = 1) {
return this.setNorm(n).setAngle(2 * Math.PI * Math.random())
}
// Draws this vector to a canvas context
debugDraw(ctx, offset = null, scale = 1, withComponents = false, color = 'white') {
let angle = this.angle()
let n = scale * this.norm()
let ox = offset ? offset.x : 0
let oy = offset ? offset.y : 0
if (withComponents) {
// _drawArrow(ctx, ox, oy + this.y * scale, scale * this.x, 0, 'red')
// _drawArrow(ctx, ox, oy, scale * this.y, Math.PI / 2, 'yellow')
ctx.strokeStyle = 'tomato'
ctx.translate(ox, oy)
ctx.beginPath()
let y = scale * this.y
ctx.moveTo(0, y)
ctx.lineTo(scale * this.x, y)
ctx.stroke()
ctx.strokeStyle = 'gold'
ctx.beginPath()
ctx.moveTo(0, 0)
ctx.lineTo(0, y)
ctx.stroke()
ctx.translate(-ox, -oy)
}
_drawArrow(ctx, ox, oy, n, angle, color)
}
}
function V(x, y) {
return new Vector(x, y)
}