JAVASCRIPT   11

Point2D.js

Guest on 18th May 2021 04:48:22 AM

  1. ///
  2. //  Point2D v 1.0
  3. //  Russell Lowke, Febrauary 22nd
  4. //
  5. //  Copyright (c)  Lowke Media
  6. //  Permission is hereby granted, free of charge, to any person obtaining a
  7. //  copy of this software and associated documentation files (the "Software"),
  8. //  to deal in the Software without restriction, including without limitation
  9. //  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. //  and/or sell copies of the Software, and to permit persons to whom the
  11. //  Software is furnished to do so, subject to the following conditions:
  12. //
  13. //  The above copyright notice and this permission notice shall be included in
  14. //  all copies or substantial portions of the Software.
  15. //
  16. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. //  IN THE SOFTWARE.
  23. //
  24. //
  25.  
  26. (function(window) {
  27.  
  28.       function Point2D(x,                             // (number) x location
  29.                                y)                           // (number) y location
  30.       {
  31.           this.x = x;
  32.           this.y = y;
  33.          
  34.           ///
  35.           //  Test if another point has the same x and y as this one.
  36.           //
  37.           this.equal = function(other) {        // (Point2D) other pt being tested
  38.             if (this.x === other.x &&
  39.                   this.y === other.y) {
  40.                   return true;
  41.             }
  42.            
  43.             return false;
  44.           }
  45.          
  46.           ///
  47.           //  Adds two Point2Ds together or, if val is a Number, adds that
  48.           //  number to both x and y in this Point2D.
  49.           //
  50.           this.add = function(val) {                  // (Point2D or number) to be added
  51.             if (val instanceof Point2D) {
  52.                   this.x += val.x;
  53.                   this.y += val.y;
  54.             } else if (typeof(val) === "number") {
  55.                   this.x += val;
  56.                   this.y += val;
  57.             } else {
  58.                         throw new Error("add() method of Point2D doesn't understand val parameter of " + val);
  59.               }
  60.               return this;
  61.           }
  62.          
  63.           ///
  64.           //  Subtracts a Point2D from this Point2D or, if val is a Number,
  65.           //  subtracts that Number from both x and y in this Point2D.
  66.           this.subtract = function(val) {       // (Point2D or number) to be subtracted
  67.             if (val instanceof Point2D) {
  68.                   this.x -= val.x;
  69.                   this.y -= val.y;
  70.             } else if (typeof(val) === "number") {
  71.                   this.x -= val;
  72.                   this.y -= val;
  73.             } else {
  74.                         throw new Error("subtract() method of Point2D doesn't understand val parameter of " + val);
  75.               }
  76.               return this;
  77.           }
  78.          
  79.           ///
  80.           //  Multiplies two Point2Ds together or, if val is a Number,
  81.           //  multiplies that Number to both x and y in this Point2D.
  82.           //
  83.           this.multiply = function(val) {       // (Point2D or number) to be multiplied
  84.             if (val instanceof Point2D) {
  85.                   this.x *= val.x;
  86.                   this.y *= val.y;
  87.             } else if (typeof(val) === "number") {
  88.                   this.x *= val;
  89.                   this.y *= val;
  90.             } else {
  91.                         throw new Error("multiply() method of Point2D doesn't understand val parameter of " + val);
  92.               }
  93.               return this;
  94.           }
  95.          
  96.           ///
  97.           //  Divides this Point2D by another or, if val is a Number,
  98.           //  divides both x and y by that Number.
  99.           //
  100.           this.divide = function(val) {         // (Point2D or number) to be divided
  101.             if (val instanceof Point2D) {
  102.                   this.x /= val.x;
  103.                   this.y /= val.y;
  104.             } else if (typeof(val) === "number") {
  105.                   this.x /= val;
  106.                   this.y /= val;
  107.             } else {
  108.                         throw new Error("divide() method of Point2D doesn't understand val parameter of " + val);
  109.               }
  110.               return this;
  111.           }
  112.          
  113.           ///
  114.           //  Rounds both x and y values.
  115.           //
  116.           this.round = function() {
  117.               this.x = Math.round(this.x);
  118.               this.y = Math.round(this.y);
  119.           }
  120.          
  121.           ///
  122.           //  Calculates the distance between this point and another
  123.           //
  124.           this.distance = function(val) {       // (Point2D) pt distance is being checked between
  125.             if (val instanceof Point2D) {
  126.                   return Math.sqrt(Math.pow(val.x - this.x, 2) + Math.pow(val.y - this.y, 2));
  127.             } else {
  128.                   throw new Error("distance() method of Point2D doesn't understand val parameter of " + val);
  129.             }
  130.           }
  131.          
  132.           ///
  133.           // Rotates this point around (0, 0) by an angle
  134.           //
  135.           this.rotate = function(angle) {       // (number) angle in degrees by which to rotate point
  136.               var theta = Point2D.degreesToRadians(angle);
  137.               var x = Math.cos(theta)*this.x + -Math.sin(theta)*this.y;
  138.               var y = Math.sin(theta)*this.x +  Math.cos(theta)*this.y;
  139.              
  140.               this.x = x;
  141.               this.y = y;
  142.              
  143.               return this;
  144.           }
  145.          
  146.           ///
  147.           //  Ensures neither x nor y are ever greater than limiting value
  148.           //
  149.           this.limit = function(val) {          // (number) limiting value
  150.               this.x %= val;
  151.               this.y %= val;
  152.           }
  153.  
  154.             ///
  155.           //  Clones this Point2D, returning a safe copy that can be manipulated
  156.           //
  157.           this.clone = function() {
  158.               return new Point2D(this.x, this.y);
  159.           }
  160.          
  161.           ///
  162.           // gives the length to (0, 0)
  163.           //
  164.           this.getLength = function() {
  165.             return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2))
  166.           }
  167.  
  168.           this.toString = function() {
  169.                   return "[Point2D (x:" + this.x + ", y:" + this.y + ")]";
  170.             }
  171.            
  172.       };
  173.      
  174.       ///
  175.       // Convert degrees to radians.
  176.       // radians = degrees*PI/180
  177.       //
  178.       Point2D.degreesToRadians = function(degrees) {
  179.             // precalculate PI/180 = 0.01745329251994
  180.             return degrees*0.01745329251994;
  181.       }
  182.      
  183.       window.Point2D = Point2D;
  184. }(window));

Raw Paste


Login or Register to edit or fork this paste. It's free.