Ellipse.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /************************************************************************
  2. * Ellipse.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * This {@code Ellipse} class provides an interface to store an ellipse as a component to
  23. * any GameObject.
  24. *
  25. * @author Pedro Schneider
  26. *
  27. * @class
  28. */
  29. class Ellipse
  30. {
  31. /**
  32. * Initializes an Ellipse with the given parameters.
  33. *
  34. * @param {number} rx radius on the X axis.
  35. * @param {number} ry radius on the Y axis. Default is the same as the X axis.
  36. *
  37. * @constructor
  38. */
  39. constructor(rx, ry = rx)
  40. {
  41. this.rx = rx;
  42. this.ry = ry;
  43. }
  44. /**
  45. * Calculates if a point (x, y) lies iside the ellipse, assuming the
  46. * ellipse is on the origin of the Cartesian plane.
  47. *
  48. * @param {number} x x-cooridnate of the point.
  49. * @param {number} y y-coordinate of the point.
  50. *
  51. * @returns {boolean} true if the point lies within the bounds of the
  52. * ellipse, false if not.
  53. */
  54. isIn(x, y)
  55. {
  56. return (x * x) * (this.ry * this.ry) + (y * y) * (this.rx * this.rx) <= (this.rx * this.rx) * (this.ry * this.ry);
  57. }
  58. }