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