/* * -------------------------------------------------- * Class: Point2DClass.as * -------------------------------------------------- * Version: 1.0 * -------------------------------------------------- * Latest update: August 1, 2003 * -------------------------------------------------- * Adapted for Flash MX by Richard Wright - June 2003 * [wisolutions2002@shaw.ca] * -------------------------------------------------- * Copyright (c) 2000-2002, Kevin Lindsey * [http://www.kevlindev.com/] * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ----------------------------------------------------------- * Functions: * - class * 1. Point2D(x,y) * - prototypes * Point2D.prototype. * 2. init(x,y) * 3. adds(that) * 4. addEquals(that) * 5. scalarAdd(scalar) * 6. scalarAddEquals(scalar) * 7. subtract(that) * 8. subtractEquals(that) * 9. scalarSubtract(scalar) * 10. scalarSubtractEquals(scalar) * 11. multiply(scalar) * 12. multiplyEquals(scalar) * 13. divide(scalar) * 14. divideEquals(scalar) * - comparison methods * 15. eqs(that) * 16. lessThan(that) * 17. lessThanEq(that) * 18. greaterThan(that) * 19. greaterThanEq(that) * - utility methods * 20. lerp(that,t) * 21. distanceFrom(that) * 22. min(that) * 23. max(that) * 24. toString() * - get/set methods * 25. setXY(x,y) * 26. setFromPoint(that) * 27. swap(that) * ------------------------------------------------------ * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ----------------------------------------------------- */ /***** * * Point2D * *****/ /***** * * 1. constructor * *****/ function Point2D(x, y) { if ( arguments.length > 0 ) { this.init(x, y); } } /***** * * 2. init * *****/ Point2D.prototype.init = function(x, y) { this.x = x; this.y = y; }; /***** * * 3. adds * *****/ Point2D.prototype.adds = function(that) { return new Point2D(this.x+that.x, this.y+that.y); }; /***** * * 4. addEquals * *****/ Point2D.prototype.addEquals = function(that) { this.x += that.x; this.y += that.y; return this; }; /***** * * 5. scalarAdd * *****/ Point2D.prototype.scalarAdd = function(scalar) { return new Point2D(this.x+scalar, this.y+scalar); }; /***** * * 6. scalarAddEquals * *****/ Point2D.prototype.scalarAddEquals = function(scalar) { this.x += scalar; this.y += scalar; return this; }; /***** * * 7. subtract * *****/ Point2D.prototype.subtract = function(that) { return new Point2D(this.x-that.x, this.y-that.y); }; /***** * * 8. subtractEquals * *****/ Point2D.prototype.subtractEquals = function(that) { this.x -= that.x; this.y -= that.y; return this; }; /***** * * 9. scalarSubtract * *****/ Point2D.prototype.scalarSubtract = function(scalar) { return new Point2D(this.x-scalar, this.y-scalar); }; /***** * * 10. scalarSubtractEquals * *****/ Point2D.prototype.scalarSubtractEquals = function(scalar) { this.x -= scalar; this.y -= scalar; return this; }; /***** * * 11. multiply * *****/ Point2D.prototype.multiply = function(scalar) { return new Point2D(this.x*scalar, this.y*scalar); }; /***** * * 12. multiplyEquals * *****/ Point2D.prototype.multiplyEquals = function(scalar) { this.x *= scalar; this.y *= scalar; return this; }; /***** * * 13. divide * *****/ Point2D.prototype.divide = function(scalar) { return new Point2D(this.x/scalar, this.y/scalar); }; /***** * * 14. divideEquals * *****/ Point2D.prototype.divideEquals = function(scalar) { this.x /= scalar; this.y /= scalar; return this; }; /***** * * comparison methods * *****/ /***** * * 15. eqs - equal * *****/ Point2D.prototype.eqs = function(that) { return ( this.x == that.x && this.y == that.y ); }; /***** * * 16. lessThan - less than * *****/ Point2D.prototype.lessThan = function(that) { return ( this.x < that.x && this.y < that.y ); }; /***** * * 17. lessThanEq - less than or equal * *****/ Point2D.prototype.lessThanEq = function(that) { return ( this.x <= that.x && this.y <= that.y ); }; /***** * * 18. greaterThan - greater than * *****/ Point2D.prototype.greaterThan = function(that) { return ( this.x > that.x && this.y > that.y ); }; /***** * * 19. greaterThanEq - greater than or equal * *****/ Point2D.prototype.greaterThanEq = function(that) { return ( this.x >= that.x && this.y >= that.y ); }; /***** * * utility methods * *****/ /***** * * 20. lerp * *****/ Point2D.prototype.lerp = function(that, t) { return new Point2D( this.x + (that.x - this.x) * t, this.y + (that.y - this.y) * t ); }; /***** * * 21. distanceFrom * *****/ Point2D.prototype.distanceFrom = function(that) { var dx = this.x - that.x; var dy = this.y - that.y; return Math.sqrt(dx*dx + dy*dy); }; /***** * * 22. min * *****/ Point2D.prototype.min = function(that) { return new Point2D( Math.min( this.x, that.x ), Math.min( this.y, that.y ) ); }; /***** * * 23. max * *****/ Point2D.prototype.max = function(that) { return new Point2D( Math.max( this.x, that.x ), Math.max( this.y, that.y ) ); }; /***** * * 24. toString * *****/ Point2D.prototype.toString = function() { return this.x + "," + this.y; }; /***** * * get/set methods * *****/ /***** * * 25. setXY * *****/ Point2D.prototype.setXY = function(x, y) { this.x = x; this.y = y; }; /***** * * 26. setFromPoint * *****/ Point2D.prototype.setFromPoint = function(that) { this.x = that.x; this.y = that.y; }; /***** * * 27. swap * *****/ Point2D.prototype.swap = function(that) { var x = this.x; var y = this.y; this.x = that.x; this.y = that.y; that.x = x; that.y = y; };