Source: core/math/shapes/Circle.js

core/math/shapes/Circle.js

  1. import Rectangle from './Rectangle';
  2. import { SHAPES } from '../../const';
  3. /**
  4. * The Circle object can be used to specify a hit area for displayObjects
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class Circle
  10. {
  11. /**
  12. * @param {number} [x=0] - The X coordinate of the center of this circle
  13. * @param {number} [y=0] - The Y coordinate of the center of this circle
  14. * @param {number} [radius=0] - The radius of the circle
  15. */
  16. constructor(x = 0, y = 0, radius = 0)
  17. {
  18. /**
  19. * @member {number}
  20. * @default 0
  21. */
  22. this.x = x;
  23. /**
  24. * @member {number}
  25. * @default 0
  26. */
  27. this.y = y;
  28. /**
  29. * @member {number}
  30. * @default 0
  31. */
  32. this.radius = radius;
  33. /**
  34. * The type of the object, mainly used to avoid `instanceof` checks
  35. *
  36. * @member {number}
  37. * @readOnly
  38. * @default PIXI.SHAPES.CIRC
  39. * @see PIXI.SHAPES
  40. */
  41. this.type = SHAPES.CIRC;
  42. }
  43. /**
  44. * Creates a clone of this Circle instance
  45. *
  46. * @return {PIXI.Circle} a copy of the Circle
  47. */
  48. clone()
  49. {
  50. return new Circle(this.x, this.y, this.radius);
  51. }
  52. /**
  53. * Checks whether the x and y coordinates given are contained within this circle
  54. *
  55. * @param {number} x - The X coordinate of the point to test
  56. * @param {number} y - The Y coordinate of the point to test
  57. * @return {boolean} Whether the x/y coordinates are within this Circle
  58. */
  59. contains(x, y)
  60. {
  61. if (this.radius <= 0)
  62. {
  63. return false;
  64. }
  65. const r2 = this.radius * this.radius;
  66. let dx = (this.x - x);
  67. let dy = (this.y - y);
  68. dx *= dx;
  69. dy *= dy;
  70. return (dx + dy <= r2);
  71. }
  72. /**
  73. * Returns the framing rectangle of the circle as a Rectangle object
  74. *
  75. * @return {PIXI.Rectangle} the framing rectangle
  76. */
  77. getBounds()
  78. {
  79. return new Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2);
  80. }
  81. }