文档丢了,暂时不补了 求投影 vh=n⋅v⋅n
后面有再学习的时候再补上文档吧
vec2的运算
[fxfyfz]
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
| class Vec2 { public x: number public y: number constructor(arr: Array<number>) { ;[this.x, this.y] = arr } set(x: number, y: number): Vec2 { this.x = x this.y = y return this; } getSize(): number { return Math.sqrt(this.x * this.x + this.y * this.y) } normalize(): Vec2 { const size = this.getSize() this.x = this.x / size this.y = this.y / size return this } multiply(n: number): Vec2 multiply(n: Vec2): number multiply(n: Vec2 | number): Vec2| number { if (typeof n === 'number') { this.x *= n this.y *= n } else { return this.x * n.x + this.y * n.y } return this }
add(v: Vec2): Vec2 { this.x += v.x this.y += v.y return this }
getDistanceFrom(b: Vec2): number { return b.add(this.multiply(-1)).getSize() }
getAngle(b: Vec2): number { return Math.acos(this.multiply(b) / (this.getSize() + b.getSize())) }
crossMultiply(b: Vec2):Vec2 {
return this; }
clone() { return new Vec2([this.x, this.y]) } horizontalProjectTo(n: Vec2): Vec2 { n = n.clone().normalize() return n.multiply(this.multiply(n)) } verticalProjectTo(n: Vec2): Vec2 { n.normalize() return this.add(this.horizontalProjectTo(n).multiply(-1)) } log() { console.log(`Vec2([${this.x}, ${this.y}])`) }
} let v = new Vec2([3, 3]) let n = new Vec2([1, 0])
v.horizontalProjectTo(n).log() v.verticalProjectTo(n).log()
|