-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.ts
31 lines (28 loc) · 871 Bytes
/
vector2.ts
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
export type IVector2 = {
readonly getX: () => number
readonly getY: () => number
readonly setX: (_: number) => void
readonly setY: (_: number) => void
readonly length: () => number
readonly normalize: () => IVector2
readonly toString: () => string
}
export const Vector2 = (x: number, y: number): IVector2 => {
let fields = { x: x, y: y }
return {
getX: () => fields.x,
getY: () => fields.y,
setX: x => fields.x = x,
setY: y => fields.y = y,
length: () => Math.sqrt(fields.x * fields.x + fields.y * fields.y),
normalize: function (this: IVector2) { return Vector2(fields.x / this.length(), fields.y / this.length()) },
toString: () => `(${fields.x}, ${fields.y})`
}
}
let v1 = Vector2(5, 3)
const s1 = v1.toString()
v1.setX(2)
const s2 = v1.toString()
let v2 = v1.normalize()
const s3 = v2.toString()
console.log("done")