Source: math/Circle.js

  1. import {Rectangle} from './Rectangle';
  2. /**
  3. * 圆形对象
  4. *
  5. * @class
  6. * @memberof JC
  7. * @param {number} x x轴的坐标
  8. * @param {number} y y轴的坐标
  9. * @param {number} radius 圆的半径
  10. */
  11. function Circle(x, y, radius) {
  12. /**
  13. * @member {number}
  14. * @default 0
  15. */
  16. this.x = x || 0;
  17. /**
  18. * @member {number}
  19. * @default 0
  20. */
  21. this.y = y || 0;
  22. /**
  23. * @member {number}
  24. * @default 0
  25. */
  26. this.radius = radius || 0;
  27. }
  28. /**
  29. * 克隆一个该圆对象
  30. *
  31. * @return {PIXI.Circle} 克隆出来的圆对象
  32. */
  33. Circle.prototype.clone = function() {
  34. return new Circle(this.x, this.y, this.radius);
  35. };
  36. /**
  37. * 检测坐标点是否在园内
  38. *
  39. * @param {number} x 坐标点的x轴坐标
  40. * @param {number} y 坐标点的y轴坐标
  41. * @return {boolean} 坐标点是否在园内
  42. */
  43. Circle.prototype.contains = function(x, y) {
  44. if (this.radius <= 0) {
  45. return false;
  46. }
  47. let dx = (this.x - x);
  48. let dy = (this.y - y);
  49. let r2 = this.radius * this.radius;
  50. dx *= dx;
  51. dy *= dy;
  52. return (dx + dy <= r2);
  53. };
  54. /**
  55. * 返回对象所占的矩形区域
  56. *
  57. * @return {PIXI.Rectangle} 矩形对象
  58. */
  59. Circle.prototype.getBounds = function() {
  60. return new Rectangle(
  61. this.x - this.radius,
  62. this.y - this.radius,
  63. this.radius * 2,
  64. this.radius * 2
  65. );
  66. };
  67. export {Circle};