PageRenderTime 28ms CodeModel.GetById 12ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/js/model/Point.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 222 lines | 107 code | 9 blank | 106 comment | 9 complexity | 9bb6157d46d45c3a1b613f172c784beb MD5 | raw file
  1/**
  2 * @author  Hyperandroid  ||  http://hyperandroid.com/
  3 *
  4 * Hold a 2D point information.
  5 * Think about the possibility of turning CAAT.Point into {x:,y:}.
  6 *
  7 * (This is stolen from Hyperandroid's CAAT)
  8 **/
  9(function () {
 10
 11    RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
 12
 13    /**
 14     *
 15     * A point defined by two coordinates.
 16     *
 17     * @param xpos {number}
 18     * @param ypos {number}
 19     *
 20     * @constructor
 21     */
 22    RealtimeMultiplayerGame.model.Point = function (xpos, ypos) {
 23        this.x = xpos || 0;
 24        this.y = ypos || 0;
 25        return this;
 26    };
 27
 28    RealtimeMultiplayerGame.model.Point.prototype = {
 29        x: 0,
 30        y: 0,
 31
 32        /**
 33         * Sets this point coordinates.
 34         * @param x {number}
 35         * @param y {number}
 36         *
 37         * @return this
 38         */
 39        set: function (x, y) {
 40            this.x = x;
 41            this.y = y;
 42            return this;
 43        },
 44        /**
 45         * Create a new RealtimeMultiplayerGame.model.Point equal to this one.
 46         * @return {RealtimeMultiplayerGame.model.Point}
 47         */
 48        clone: function () {
 49            var p = new RealtimeMultiplayerGame.model.Point();
 50            p.set(this.x, this.y);
 51            return p;
 52        },
 53        /**
 54         * Translate this point to another position. The final point will be (point.x+x, point.y+y)
 55         * @param x {number}
 56         * @param y {number}
 57         *
 58         * @return this
 59         */
 60        translate: function (x, y) {
 61            this.x += x;
 62            this.y += y;
 63
 64            return this;
 65        },
 66        /**
 67         * Translate this point to another point.
 68         * @param aPoint {RealtimeMultiplayerGame.model.Point}
 69         * @return this
 70         */
 71        translatePoint: function (aPoint) {
 72            this.x += aPoint.x;
 73            this.y += aPoint.y;
 74            return this;
 75        },
 76        /**
 77         * Substract a point from this one.
 78         * @param aPoint {RealtimeMultiplayerGame.model.Point}
 79         * @return this
 80         */
 81        subtract: function (aPoint) {
 82            this.x -= aPoint.x;
 83            this.y -= aPoint.y;
 84            return this;
 85        },
 86
 87        /**
 88         * Substract a point from this one
 89         * Returns a new point with the difference
 90         * @param aPoint {RealtimeMultiplayerGame.model.Point}
 91         * @return {RealtimeMultiplayerGame.model.Point}
 92         */
 93        subtractClone: function (aPoint) {
 94            return new RealtimeMultiplayerGame.model.Point(this.x - aPoint.x, this.y - aPoint.y)
 95        },
 96
 97        /**
 98         * Multiply this point by a scalar.
 99         * @param factor {number}
100         * @return this
101         */
102        multiply: function (factor) {
103            this.x *= factor;
104            this.y *= factor;
105            return this;
106        },
107        /**
108         * Rotate this point by an angle. The rotation is held by (0,0) coordinate as center.
109         * @param angle {number}
110         * @return this
111         */
112        rotate: function (angle) {
113            var x = this.x, y = this.y;
114            this.x = x * Math.cos(angle) - Math.sin(angle) * y;
115            this.y = x * Math.sin(angle) + Math.cos(angle) * y;
116            return this;
117        },
118        /**
119         *
120         * @param angle {number}
121         * @return this
122         */
123        setAngle: function (angle) {
124            var len = this.getLength();
125            this.x = Math.cos(angle) * len;
126            this.y = Math.sin(angle) * len;
127            return this;
128        },
129        /**
130         *
131         * @param length {number}
132         * @return this
133         */
134        setLength: function (length) {
135            var len = this.getLength();
136            if (len)this.multiply(length / len);
137            else this.x = this.y = length;
138            return this;
139        },
140        /**
141         * Normalize this point, that is, both set coordinates proportionally to values raning 0..1
142         * @return this
143         */
144        normalize: function () {
145            var len = this.getLength();
146            this.x /= len;
147            this.y /= len;
148            return this;
149        },
150        /**
151         * Return the angle from -Pi to Pi of this point.
152         * @return {number}
153         */
154        getAngle: function () {
155            return Math.atan2(this.y, this.x);
156        },
157        /**
158         * Set this point coordinates proportinally to a maximum value.
159         * @param max {number}
160         * @return this
161         */
162        limit: function (max) {
163            var aLenthSquared = this.getLengthSquared();
164            if (aLenthSquared + 0.01 > max * max) {
165                var aLength = Math.sqrt(aLenthSquared);
166                this.x = (this.x / aLength) * max;
167                this.y = (this.y / aLength) * max;
168            }
169            return this;
170        },
171        /**
172         * Get this point's lenght.
173         * @return {number}
174         */
175        getLength: function () {
176            var length = Math.sqrt(this.x * this.x + this.y * this.y);
177            if (length < 0.005 && length > -0.005) return 0.000001;
178            return length;
179
180        },
181        /**
182         * Get this point's squared length.
183         * @return {number}
184         */
185        getLengthSquared: function () {
186            var lengthSquared = this.x * this.x + this.y * this.y;
187            if (lengthSquared < 0.005 && lengthSquared > -0.005) return 0;
188            return lengthSquared;
189        },
190        /**
191         * Get the distance between two points.
192         * @param point {RealtimeMultiplayerGame.model.Point}
193         * @return {number}
194         */
195        getDistance: function (point) {
196            var deltaX = this.x - point.x;
197            var deltaY = this.y - point.y;
198            return Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
199        },
200        /**
201         * Get the squared distance between two points.
202         * @param point {RealtimeMultiplayerGame.model.Point}
203         * @return {number}
204         */
205        getDistanceSquared: function (point) {
206            var deltaX = this.x - point.x;
207            var deltaY = this.y - point.y;
208            return (deltaX * deltaX) + (deltaY * deltaY);
209        },
210        /**
211         * Get a string representation.
212         * @return {string}
213         */
214        toString: function () {
215            return "(RealtimeMultiplayerGame.model.Point)" +
216                " x:'" + String(Math.round(Math.floor(this.x * 10)) / 10) +
217                " y:" + String(Math.round(Math.floor(this.y * 10)) / 10);
218        }
219    };
220
221    RealtimeMultiplayerGame.model.Point.prototype.ZERO = new RealtimeMultiplayerGame.model.Point(0, 0);
222})();