|
| 1 | +export class Vec2 { |
| 2 | + static readonly zero = new Vec2(0, 0); |
| 3 | + |
| 4 | + readonly x: number; |
| 5 | + readonly y: number; |
| 6 | + |
| 7 | + constructor(x: number, y: number) { |
| 8 | + this.x = x; |
| 9 | + this.y = y; |
| 10 | + } |
| 11 | + |
| 12 | + toString(): string { |
| 13 | + return `<${this.x}, ${this.y}>`; |
| 14 | + } |
| 15 | + add(v: Vec2): Vec2 { |
| 16 | + return new Vec2(this.x + v.x, this.y + v.y); |
| 17 | + } |
| 18 | + minus(v: Vec2): Vec2 { |
| 19 | + return new Vec2(this.x - v.x, this.y - v.y); |
| 20 | + } |
| 21 | + divide(s: number) { |
| 22 | + return new Vec2(this.x / s, this.y / s); |
| 23 | + } |
| 24 | + scale(s: number) { |
| 25 | + return new Vec2(this.x * s, this.y * s); |
| 26 | + } |
| 27 | + dot(v: Vec2) { |
| 28 | + return this.x * v.x + this.y * v.y; |
| 29 | + } |
| 30 | + lengthSquared(): number { |
| 31 | + return this.x * this.x + this.y * this.y; |
| 32 | + } |
| 33 | + length(): number { |
| 34 | + return Math.sqrt(this.lengthSquared()); |
| 35 | + } |
| 36 | + normalize(): Vec2 { |
| 37 | + return this.divide(this.length()); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export class Vec3 { |
| 42 | + static readonly zero = new Vec3(0, 0, 0); |
| 43 | + |
| 44 | + readonly x: number; |
| 45 | + readonly y: number; |
| 46 | + readonly z: number; |
| 47 | + |
| 48 | + constructor(x: number, y: number, z: number) { |
| 49 | + this.x = x; |
| 50 | + this.y = y; |
| 51 | + this.z = z; |
| 52 | + } |
| 53 | + |
| 54 | + toString(): string { |
| 55 | + return `<${this.x}, ${this.y}, ${this.z}>`; |
| 56 | + } |
| 57 | + add(v: Vec3): Vec3 { |
| 58 | + return new Vec3(this.x + v.x, this.y + v.y, this.z + v.z); |
| 59 | + } |
| 60 | + minus(v: Vec3): Vec3 { |
| 61 | + return new Vec3(this.x - v.x, this.y - v.y, this.z - v.z); |
| 62 | + } |
| 63 | + divide(s: number): Vec3 { |
| 64 | + return new Vec3(this.x / s, this.y / s, this.z / s); |
| 65 | + } |
| 66 | + scale(s: number): Vec3 { |
| 67 | + return new Vec3(this.x * s, this.y * s, this.z * s); |
| 68 | + } |
| 69 | + multiply(v: Vec3): Vec3 { |
| 70 | + return new Vec3(this.x * v.x, this.y * v.y, this.z * v.z); |
| 71 | + } |
| 72 | + dot(v: Vec3): number { |
| 73 | + return this.x * v.x + this.y * v.y + this.z * v.z; |
| 74 | + } |
| 75 | + cross(v: Vec3): Vec3 { |
| 76 | + return new Vec3( |
| 77 | + this.y * v.z - this.z * v.y, |
| 78 | + this.z * v.x - this.x * v.z, |
| 79 | + this.x * v.y - this.y * v.x, |
| 80 | + ); |
| 81 | + } |
| 82 | + lengthSquared(): number { |
| 83 | + return this.x * this.x + this.y * this.y + this.z * this.z; |
| 84 | + } |
| 85 | + length(): number { |
| 86 | + return Math.sqrt(this.lengthSquared()); |
| 87 | + } |
| 88 | + normalize(): Vec3 { |
| 89 | + return this.divide(this.length()); |
| 90 | + } |
| 91 | + |
| 92 | + clamp(min: number, max: number): Vec3 { |
| 93 | + return new Vec3( |
| 94 | + Math.min(Math.max(this.x, min), max), |
| 95 | + Math.min(Math.max(this.y, min), max), |
| 96 | + Math.min(Math.max(this.z, min), max), |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export class Vec4 { |
| 102 | + static readonly zezo = new Vec4(0, 0, 0, 0); |
| 103 | + |
| 104 | + readonly x: number; |
| 105 | + readonly y: number; |
| 106 | + readonly z: number; |
| 107 | + readonly w: number; |
| 108 | + |
| 109 | + constructor(x: number, y: number, z: number, w: number) { |
| 110 | + this.x = x; |
| 111 | + this.y = y; |
| 112 | + this.z = z; |
| 113 | + this.w = w; |
| 114 | + } |
| 115 | + |
| 116 | + toString(): string { |
| 117 | + return `<${this.x}, ${this.y}, ${this.z}, ${this.w}>`; |
| 118 | + } |
| 119 | + add(v: Vec4): Vec4 { |
| 120 | + return new Vec4(this.x + v.x, this.y + v.y, this.z + v.z, this.w + v.w); |
| 121 | + } |
| 122 | + minus(v: Vec4): Vec4 { |
| 123 | + return new Vec4(this.x - v.x, this.y - v.y, this.z - v.z, this.w - v.w); |
| 124 | + } |
| 125 | + multiply(v: Vec4): Vec4 { |
| 126 | + return new Vec4(this.x * v.x, this.y * v.y, this.z * v.z, this.w * v.w); |
| 127 | + } |
| 128 | + divide(s: number): Vec4 { |
| 129 | + return new Vec4(this.x / s, this.y / s, this.z / s, this.w / s); |
| 130 | + } |
| 131 | + dot(v: Vec4): number { |
| 132 | + return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; |
| 133 | + } |
| 134 | + scale(s: number) { |
| 135 | + return new Vec4(this.x * s, this.y * s, this.z * s, this.w * s); |
| 136 | + } |
| 137 | + lengthSquared(): number { |
| 138 | + return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; |
| 139 | + } |
| 140 | + length(): number { |
| 141 | + return Math.sqrt(this.lengthSquared()); |
| 142 | + } |
| 143 | + normalize(): Vec4 { |
| 144 | + return this.divide(this.length()); |
| 145 | + } |
| 146 | +} |
0 commit comments