graphics/circle.js

  1. 'use strict';
  2. var Thing = require('./thing.js');
  3. var Color = require('./color.js');
  4. var graphicsUtils = require('./graphics-utils.js');
  5. /**
  6. * @class Circle
  7. * @augments Thing
  8. * @param {number} radius - Desired radius
  9. */
  10. function Circle(radius) {
  11. if (arguments.length !== 1) {
  12. throw new Error('You should pass exactly 1 argument to <span ' +
  13. 'class="code">new Circle(radius)</span>');
  14. }
  15. if (typeof radius !== 'number' || !isFinite(radius)) {
  16. throw new TypeError('You must pass a finite number to <span class=' +
  17. '"code">new Circle(radius)</span>. Did you forget the ' +
  18. 'parentheses in <span class="code">getWidth()</span> or <span ' +
  19. 'class="code">getHeight()</span>? Or did you perform a ' +
  20. 'calculation on a variable that is not a number?');
  21. }
  22. Thing.call(this);
  23. this.radius = Math.max(0, radius);
  24. this.color = Color.black;
  25. this.lineWidth = 3;
  26. this.type = 'Circle';
  27. }
  28. Circle.prototype = new Thing();
  29. Circle.prototype.constructor = Circle;
  30. /**
  31. * Draws the circle in the canvas.
  32. * @param {CodeHSGraphics} __graphics__ - Instance of the Graphics module.
  33. */
  34. Circle.prototype.draw = function(__graphics__) {
  35. var context = __graphics__.getContext();
  36. context.beginPath();
  37. if (this.hasBorder) {
  38. context.strokeStyle = this.stroke.toString();
  39. context.lineWidth = this.lineWidth;
  40. }
  41. context.fillStyle = this.color.toString();
  42. context.arc(this.x,this.y,this.radius,0,Math.PI * 2,true);
  43. context.closePath();
  44. if (this.hasBorder) {
  45. context.stroke();
  46. }
  47. context.fill();
  48. };
  49. /**
  50. * Gets the radius of the circle
  51. * @returns {number} Radius of the circle.
  52. */
  53. Circle.prototype.getRadius = function() {
  54. return this.radius;
  55. };
  56. /**
  57. * Gets the height (diamter) of the circle.
  58. * @returns {number} Height (diameter) of the circle.
  59. */
  60. Circle.prototype.getHeight = function() {
  61. return this.radius * 2;
  62. };
  63. /**
  64. * Gets the width (diamter) of the circle.
  65. * @returns {number} Width (diameter) of the circle.
  66. */
  67. Circle.prototype.getWidth = function() {
  68. return this.radius * 2;
  69. };
  70. /**
  71. * Sets the radius of the circle.
  72. * @param {number} radius - Desired resulting radius of the circle.
  73. */
  74. Circle.prototype.setRadius = function(radius) {
  75. if (arguments.length !== 1) {
  76. throw new Error('You should pass exactly 1 argument to <span ' +
  77. 'class="code">setRadius(radius)</span>');
  78. }
  79. if (typeof radius !== 'number' || !isFinite(radius)) {
  80. throw new TypeError('You must pass a finite number to <span class=' +
  81. '"code">setRadius(radius)</span>. Did you forget the ' +
  82. 'parentheses in <span class="code">getWidth()</span> or <span ' +
  83. 'class="code">getHeight()</span>? Or did you perform a ' +
  84. 'calculation on a variable that is not a number?');
  85. }
  86. this.radius = Math.max(0, radius);
  87. };
  88. /**
  89. * Checks if the passed point is contained in the circle.
  90. * @param {number} x - The x coordinate of the point being tested.
  91. * @param {number} y - The y coordinate of the point being tested.
  92. * @returns {boolean} Whether the passed point is contained in the circle.
  93. */
  94. Circle.prototype.containsPoint = function(x, y) {
  95. var circleEdge = this.radius;
  96. if (this.hasBorder) {
  97. circleEdge += this.lineWidth;
  98. }
  99. var dist = graphicsUtils.getDistance(this.x, this.y, x, y);
  100. return dist < circleEdge;
  101. };
  102. module.exports = Circle;