forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.ts
165 lines (141 loc) · 4.26 KB
/
math.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
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
namespace microcode {
export class Vec2 {
public dirty: boolean
//public readonly: boolean;
//% blockCombine block="x" callInDebugger
public get x() {
return this.x_
}
public set x(v) {
//if (this.readonly) throw "hey";
this.x_ = v
this.dirty = true
}
//% blockCombine block="y" callInDebugger
public get y() {
return this.y_
}
public set y(v) {
//if (this.readonly) throw "hey";
this.y_ = v
this.dirty = true
}
constructor(public x_ = 0, public y_ = 0) {
// perf: ensure x_, y_ are integers
//control.assert((this.x_ | 0) == this.x_, 123)
//control.assert((this.y_ | 0) == this.y_, 123)
}
public clone(): Vec2 {
return new Vec2(this.x, this.y)
}
public copyFrom(v: Vec2): this {
this.x = v.x
this.y = v.y
return this
}
public set(x: number, y: number): this {
this.x = x
this.y = y
return this
}
public magSq(): number {
return this.x * this.x + this.y * this.y
}
public floor(): this {
this.x = Math.floor(this.x)
this.y = Math.floor(this.y)
return this
}
public add(v: Vec2): this {
this.x = this.x + v.x
this.y = this.y + v.y
return this
}
public static DistSq(a: Vec2, b: Vec2): number {
const x = b.x - a.x
const y = b.y - a.y
return x * x + y * y
}
public static ZeroToRef(ref: Vec2): Vec2 {
return ref.set(0, 0)
}
/*
public static RotateToRef(v: Vec2, angle: number, ref: Vec2): Vec2 {
const s = trig.sin(angle);
const c = trig.cos(angle);
const xp = Fx.sub(Fx.mul(v.x, c), Fx.mul(v.y, s));
const yp = Fx.add(Fx.mul(v.x, s), Fx.mul(v.y, c));
ref.x = xp;
ref.y = yp;
return ref;
}
*/
public static TranslateToRef(v: Vec2, p: Vec2, ref: Vec2): Vec2 {
ref.x = v.x + p.x
ref.y = v.y + p.y
return ref
}
public static ScaleToRef(v: Vec2, scale: number, ref: Vec2): Vec2 {
ref.x = v.x * scale
ref.y = v.y * scale
return ref
}
public static FloorToRef(v: Vec2, ref: Vec2): Vec2 {
ref.x = Math.floor(v.x)
ref.y = Math.floor(v.y)
return ref
}
public static MaxToRef(a: Vec2, b: Vec2, ref: Vec2): Vec2 {
ref.x = Math.max(a.x, b.x)
ref.y = Math.max(a.y, b.y)
return ref
}
public static MinToRef(a: Vec2, b: Vec2, ref: Vec2): Vec2 {
ref.x = Math.min(a.x, b.x)
ref.y = Math.min(a.y, b.y)
return ref
}
public static SubToRef(a: Vec2, b: Vec2, ref: Vec2): Vec2 {
ref.x = a.x - b.x
ref.y = a.y - b.y
return ref
}
public static AddToRef(a: Vec2, b: Vec2, ref: Vec2): Vec2 {
ref.x = a.x + b.x
ref.y = a.y + b.y
return ref
}
public static MulToRef(a: Vec2, b: Vec2, ref: Vec2): Vec2 {
ref.x = a.x * b.x
ref.y = a.y * b.y
return ref
}
public static LerpToRefFix(
a: Vec2,
b: Vec2,
t: number,
ref: Vec2
): Vec2 {
ref.x = lerpFix(a.x, b.x, t)
ref.y = lerpFix(a.y, b.y, t)
return ref
}
public static RandomRangeToRef(
xmin: number,
xmax: number,
ymin: number,
ymax: number,
ref: Vec2
): Vec2 {
ref.x = Math.randomRange(xmin, xmax)
ref.y = Math.randomRange(ymin, ymax)
return ref
}
public toString(): string {
return `Vec2(x:${this.x},y:${this.y})`
}
}
export function lerpFix(a: number, b: number, t: number): number {
return a + (((b - a) * t) >> 8)
}
}