Browse Source

✨ Add isIn function to Shapes

- Checks if a point is inside of the shape, assuming the center of the shape is the origin.
Pedro Schneider 3 years ago
parent
commit
e94d687a55
2 changed files with 30 additions and 0 deletions
  1. 15 0
      pandora/components/Ellipse.js
  2. 15 0
      pandora/components/Rect.js

+ 15 - 0
pandora/components/Ellipse.js

@@ -42,4 +42,19 @@ class Ellipse
         this.rx = rx;
         this.ry = ry;
     }
+
+    /**
+     * Calculates if a point (x, y) lies iside the ellipse, assuming the
+     * ellipse is on the origin of the Cartesian plane.
+     * 
+     * @param {number} x    x-cooridnate of the point. 
+     * @param {number} y    y-coordinate of the point.
+     * 
+     * @returns {boolean}   true if the point lies within the bounds of the
+     *                      ellipse, false if not. 
+     */
+    isIn(x, y)
+    {
+        return (x * x) * (this.ry * this.ry) + (y * y) * (this.rx * this.rx) <= (this.rx * this.rx) * (this.ry * this.ry);
+    }
 }

+ 15 - 0
pandora/components/Rect.js

@@ -42,4 +42,19 @@ class Rect
         this.w = w;
         this.h = h;
     }
+
+    /**
+     * Calculates if a point (x, y) lies iside the rect, assuming the
+     * rect is on the origin of the Cartesian plane.
+     * 
+     * @param {number} x    x-cooridnate of the point. 
+     * @param {number} y    y-coordinate of the point.
+     * 
+     * @returns {boolean}   true if the point lies within the bounds of the
+     *                      rect, false if not. 
+     */
+    isIn(x, y)
+    {
+        return abs(x) <= this.w / 2 && abs(y) <= this.h / 2;
+    }
 }