regularpolygon.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. define(function(require, exports, module) {
  2. var Point = require('./point');
  3. return require('../core/class').createClass('RegularPolygon', {
  4. base: require('./path'),
  5. constructor: function(side, radius, x, y) {
  6. this.callBase();
  7. this.radius = radius || 0;
  8. this.side = Math.max(side || 3, 3);
  9. if (arguments.length > 2) {
  10. if (arguments.length == 3) {
  11. y = x.y;
  12. x = x.x;
  13. }
  14. }
  15. this.center = new Point(x, y);
  16. this.draw();
  17. },
  18. getSide: function() {
  19. return this.side;
  20. },
  21. setSide: function(side) {
  22. this.side = side;
  23. return this.draw();
  24. },
  25. getRadius: function() {
  26. return this.radius;
  27. },
  28. setRadius: function(radius) {
  29. this.radius = radius;
  30. return this.draw();
  31. },
  32. draw: function() {
  33. var radius = this.radius,
  34. side = this.side,
  35. step = Math.PI * 2 / side,
  36. drawer = this.getDrawer(),
  37. i;
  38. drawer.clear();
  39. drawer.moveTo(Point.fromPolar(radius, Math.PI / 2, 'rad').offset(this.center));
  40. for (i = 0; i <= side; i++) {
  41. drawer.lineTo(Point.fromPolar(radius, step * i + Math.PI / 2, 'rad').offset(this.center));
  42. }
  43. drawer.close();
  44. return this;
  45. }
  46. });
  47. });