| 1 | class vector |
| 2 | { |
| 3 | x = 0; |
| 4 | y = 0; |
| 5 | |
| 6 | constructor (x, y) |
| 7 | { |
| 8 | this.x = x; |
| 9 | |
| 10 | this.y = y; |
| 11 | } |
| 12 | |
| 13 | get length () |
| 14 | { |
| 15 | return Math.sqrt((this.x * this.x) + (this.y * this.y)); |
| 16 | } |
| 17 | |
| 18 | get lengthSq () |
| 19 | { |
| 20 | return (this.x * this.x) + (this.y * this.y); |
| 21 | } |
| 22 | |
| 23 | getLength () |
| 24 | { |
| 25 | return Math.sqrt((this.x * this.x) + (this.y * this.y)); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | toAngle () |
| 30 | { |
| 31 | let angle = (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90; |
| 32 | if (angle < 0) |
| 33 | {angle += 360;} |
| 34 | return angle; |
| 35 | } |
| 36 | |
| 37 | static fromAngle (angle) |
| 38 | { |
| 39 | angle = 360 - ((angle + 180) % 360); |
| 40 | angle = (angle / 180) * Math.PI; |
| 41 | return new vector(Math.sin(angle), Math.cos(angle)); |
| 42 | } |
| 43 | |
| 44 | normalize () |
| 45 | { |
| 46 | var newVector = this.getCopy(); |
| 47 | var length = newVector.length; |
| 48 | |
| 49 | if (length != 0) |
| 50 | { |
| 51 | newVector.x /= length; |
| 52 | newVector.y /= length; |
| 53 | } |
| 54 | |
| 55 | return newVector; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | lerpTo (otherVector, t) |
| 60 | { |
| 61 | var delta = otherVector.subtract(this); |
| 62 | |
| 63 | return this.add(delta.stretch(t)); |
| 64 | } |
| 65 | |
| 66 | lerpToInto (otherVector, t, out) |
| 67 | { |
| 68 | out.x = this.x + (otherVector.x - this.x) * t; |
| 69 | out.y = this.y + (otherVector.y - this.y) * t; |
| 70 | return out; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | distanceToSq (otherVector) |
| 75 | { |
| 76 | var dX = this.x - otherVector.x; |
| 77 | var dY = this.y - otherVector.y; |
| 78 | return (dX * dX) + (dY * dY); |
| 79 | } |
| 80 | |
| 81 | distanceTo (otherVector) |
| 82 | { |
| 83 | return Math.sqrt(this.distanceToSq(otherVector)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | add (otherVector) |
| 88 | { |
| 89 | var newVector = this.getCopy(); |
| 90 | |
| 91 | newVector.x += otherVector.x; |
| 92 | newVector.y += otherVector.y; |
| 93 | |
| 94 | return newVector; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | subtract (otherVector) |
| 99 | { |
| 100 | var newVector = this.getCopy(); |
| 101 | |
| 102 | newVector.x -= otherVector.x; |
| 103 | newVector.y -= otherVector.y; |
| 104 | |
| 105 | return newVector; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | multiply (otherVector) |
| 110 | { |
| 111 | var newVector = this.getCopy(); |
| 112 | |
| 113 | newVector.x *= otherVector.x; |
| 114 | newVector.y *= otherVector.y; |
| 115 | |
| 116 | return newVector; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | divide (otherVector) |
| 121 | { |
| 122 | var newVector = this.getCopy(); |
| 123 | |
| 124 | newVector.x /= otherVector.x; |
| 125 | newVector.y /= otherVector.y; |
| 126 | |
| 127 | return newVector; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | stretch (length) |
| 132 | { |
| 133 | var newVector = this.getCopy(); |
| 134 | |
| 135 | newVector.x *= length; |
| 136 | newVector.y *= length; |
| 137 | |
| 138 | return newVector; |
| 139 | } |
| 140 | |
| 141 | rotate (degrees) |
| 142 | { |
| 143 | var ca = Math.cos(degrees * (Math.PI / 180)); |
| 144 | var sa = Math.sin(degrees * (Math.PI / 180)); |
| 145 | return new vector(ca * this.x - sa * this.y, sa * this.x + ca * this.y); |
| 146 | } |
| 147 | |
| 148 | deltaAngle (otherVector) |
| 149 | { |
| 150 | var a = this.toAngle() * (Math.PI / 180); |
| 151 | var b = otherVector.toAngle() * (Math.PI / 180); |
| 152 | |
| 153 | return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI); |
| 154 | } |
| 155 | |
| 156 | cross (otherVector) |
| 157 | { |
| 158 | return this.x * otherVector.y - this.y * otherVector.x; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | getCopy () |
| 163 | { |
| 164 | return new vector(this.x, this.y); |
| 165 | } |
| 166 | }; |