| 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 this.length(); |
| 16 | } |
| 17 | |
| 18 | length() |
| 19 | { |
| 20 | return Math.sqrt((this.x * this.x) + (this.y * this.y)); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | toAngle() |
| 25 | { |
| 26 | return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | normalize() |
| 31 | { |
| 32 | var newVector = this.getCopy(); |
| 33 | |
| 34 | if(newVector.length() != 0) |
| 35 | { |
| 36 | var length = newVector.length(); |
| 37 | newVector.x /= length; |
| 38 | newVector.y /= length; |
| 39 | } |
| 40 | |
| 41 | return newVector; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | lerpTo(otherVector, t) |
| 46 | { |
| 47 | var delta = otherVector.subtract(this); |
| 48 | |
| 49 | return this.add(delta.stretch(t)); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | distanceTo(otherVector) |
| 54 | { |
| 55 | var dX = this.x - otherVector.x; |
| 56 | var dY = this.y - otherVector.y; |
| 57 | |
| 58 | return new vector(dX, dY).length(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | add(otherVector) |
| 63 | { |
| 64 | var newVector = this.getCopy(); |
| 65 | |
| 66 | newVector.x += otherVector.x; |
| 67 | newVector.y += otherVector.y; |
| 68 | |
| 69 | return newVector; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | subtract(otherVector) |
| 74 | { |
| 75 | var newVector = this.getCopy(); |
| 76 | |
| 77 | newVector.x -= otherVector.x; |
| 78 | newVector.y -= otherVector.y; |
| 79 | |
| 80 | return newVector; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | multiply(otherVector) |
| 85 | { |
| 86 | var newVector = this.getCopy(); |
| 87 | |
| 88 | newVector.x *= otherVector.x; |
| 89 | newVector.y *= otherVector.y; |
| 90 | |
| 91 | return newVector; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | divide(otherVector) |
| 96 | { |
| 97 | var newVector = this.getCopy(); |
| 98 | |
| 99 | newVector.x /= otherVector.x; |
| 100 | newVector.y /= otherVector.y; |
| 101 | |
| 102 | return newVector; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | stretch(length) |
| 107 | { |
| 108 | var newVector = this.getCopy(); |
| 109 | |
| 110 | newVector.x *= length; |
| 111 | newVector.y *= length; |
| 112 | |
| 113 | return newVector; |
| 114 | } |
| 115 | |
| 116 | rotate(degrees) |
| 117 | { |
| 118 | var ca = Math.cos(degrees * (Math.PI / 180)); |
| 119 | var sa = Math.sin(degrees * (Math.PI / 180)); |
| 120 | return new vector(ca*this.x - sa*this.y, sa*this.x + ca*this.y); |
| 121 | } |
| 122 | |
| 123 | deltaAngle(otherVector) |
| 124 | { |
| 125 | var a = this.toAngle() * (Math.PI / 180); |
| 126 | var b = otherVector.toAngle() * (Math.PI / 180); |
| 127 | |
| 128 | return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI) |
| 129 | } |
| 130 | |
| 131 | cross(otherVector) |
| 132 | { |
| 133 | return this.x * otherVector.y - this.y * otherVector.x; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | getCopy() |
| 138 | { |
| 139 | return new vector(this.x, this.y); |
| 140 | } |
| 141 | }; |