Advertisement
Guest User

Shapes

a guest
Nov 4th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Module Shapes
  2. var Shapes = (function () {
  3.    
  4.     // Shape - base class, all other figures are inherited from base class Shape
  5.     var Shape = (function () {
  6.         function Shape(x, y, color) {
  7.             this._x = x;
  8.             this._y = y;
  9.             this._color = color;
  10.         }
  11.        
  12.         Shape.prototype = {
  13.             serialize: function () {
  14.                 var serializedShape = {
  15.                     x: this._x,
  16.                     y: this._y,
  17.                     color: this.color
  18.                 };
  19.                 return serializedShape;
  20.             },
  21.             canvas: function () {
  22.                 var canvas = {
  23.                     element: document.getElementById("shapesContainer").getContext("2d")
  24.                 };
  25.                 return canvas;
  26.             }
  27.         }
  28.        
  29.         Shape.prototype.toString = function () {
  30.             return this.serialize();
  31.         }
  32.        
  33.         return Shape;
  34.     }());
  35.    
  36.     // Circle
  37.     var Circle = (function () {
  38.        
  39.         function validateRadius() {
  40.             if (this._radius < 0) {
  41.                 return false;
  42.             }
  43.             return true;
  44.         }
  45.  
  46.         function Circle(x, y, color, radius) {
  47.             Shape.call(this, x, y, color);
  48.             if (!validateRadius.call(this)) {
  49.                 throw new Error('Invalid radius, must be positive value!');
  50.             }
  51.             this._radius = radius;
  52.         }
  53.        
  54.         Circle.prototype = new Shape();
  55.        
  56.         Circle.prototype.serialize = function () {
  57.             var serializedCircle = Shape.prototype.serialize.call(this);
  58.             serializedCircle.radius = this._radius;
  59.             return serializedCircle;
  60.         };
  61.        
  62.         Circle.prototype.draw = function () {
  63.             this.canvas().element.beginPath();
  64.             this.canvas().element.arc(this.serialize().x, this.serialize().y, this.serialize().radius, 0, 2 * Math.PI, false);
  65.             this.canvas().element.fillStyle = this.serialize().color;
  66.             this.canvas().element.fill();
  67.             this.canvas().element.lineWidth = 5;
  68.             this.canvas().element.strokeStyle = this.serialize().color;
  69.             this.canvas().element.stroke();
  70.         };
  71.        
  72.         Circle.prototype.toString = function () {
  73.             return "Circle: " + JSON.stringify(this.serialize());
  74.         };
  75.        
  76.         return Circle;
  77.  
  78.     }());
  79.    
  80.     // Point
  81.     var Point = (function () {
  82.         function Point(x, y, color) {
  83.             Shape.call(this, x, y, color);
  84.         }
  85.        
  86.         Point.prototype = new Shape();
  87.        
  88.         Point.prototype.serialize = function () {
  89.             var serializedPoint = Shape.prototype.serialize.call(this);
  90.             return serializedPoint;
  91.         };
  92.        
  93.         Point.prototype.draw = function () {
  94.             this.canvas().element.beginPath();
  95.             this.canvas().element.fillStyle = this.serialize().color;
  96.             this.canvas().element.fillRect(this.serialize().x, this.serialize().y, 3, 3);
  97.         };
  98.        
  99.         Point.prototype.toString = function () {
  100.             return "Point: " + JSON.stringify(this.serialize());
  101.         };
  102.        
  103.         return Point;
  104.  
  105.     }());
  106.    
  107.     // Rectangle
  108.     var Rectangle = (function () {
  109.        
  110.         var BORDERS = {
  111.             LEFT: 0,
  112.             RIGHT: 1000,
  113.             TOP: 0,
  114.             BOTTOM: 1000
  115.         };
  116.        
  117.         function validatePosition() {
  118.             if (this._x < BORDERS.LEFT || BORDERS.RIGHT < this._x ||
  119.             this._y < BORDERS.TOP || BORDERS.BOTTOM < this._y) {
  120.                 return false;
  121.             }
  122.             return true;
  123.         }
  124.        
  125.         function Rectangle(x, y, color, width, height) {
  126.             Shape.call(this, x, y, color);
  127.             if (!validatePosition.call(this)) {
  128.                 throw new Error('Invalid Rect position');
  129.             }
  130.             this._width = width;
  131.             this._height = height;
  132.         }
  133.        
  134.        
  135.         Rectangle.prototype = new Shape();
  136.        
  137.         Rectangle.prototype.serialize = function () {
  138.             var serializedRect = Shape.prototype.serialize.call(this);
  139.             serializedRect.width = this._width;
  140.             serializedRect.height = this._height;
  141.             return serializedRect;
  142.         };
  143.        
  144.         Rectangle.prototype.draw = function () {
  145.             this.canvas().element.beginPath();
  146.             this.canvas().element.fillStyle = this.serialize().color;
  147.             this.canvas().element.fillRect(this.serialize().x,
  148.                 this.serialize().y, this.serialize().width, this.serialize().height);
  149.         };
  150.        
  151.         Rectangle.prototype.toString = function () {
  152.             return "Rectangle: " + JSON.stringify(this.serialize());
  153.         };
  154.        
  155.         return Rectangle;
  156.     }());
  157.    
  158.    
  159.     // Triangle
  160.     var Triangle = (function () {
  161.        
  162.         var SIDES = {
  163.             a: Math.sqrt((this._x2 - this._x1) * (this._x2 - this._x1) + (this._y2 - this._y1) * (this._y2 - this._y1)),
  164.             b: Math.sqrt((this._x3 - this._x2) * (this._x3 - this._x2) + (this._y3 - this._y2) * (this._y3 - this._y2)),
  165.             c: Math.sqrt((this._x3 - this._x1) * (this._x3 - this._x1) + (this._y3 - this._y1) * (this._y3 - this._y1)),
  166.         };
  167.        
  168.         function validateTriangle() {
  169.             if (SIDES.a + SIDES.b > SIDES.c &&
  170.                 SIDES.b + SIDES.c > SIDES.a &&
  171.                 SIDES.a + SIDES.c > SIDES.b) {
  172.                 return true;
  173.             }
  174.             return false;
  175.         }
  176.        
  177.        
  178.         function Triangle(x, y, color, x2, y2, x3, y3) {
  179.             Shape.call(this, x, y, color);
  180.             if (!validateTriangle.call(this)) {
  181.                 throw new Error('Invalid Triangle');
  182.             }
  183.             this._x2 = x2;
  184.             this._y2 = y2;
  185.             this._x3 = x3;
  186.             this._y3 = y3;
  187.         }
  188.        
  189.         Triangle.prototype = new Shape();
  190.        
  191.         Triangle.prototype.serialize = function () {
  192.             var serializedTriangle = Shape.prototype.serialize.call(this);
  193.             serializedTriangle.x2 = this._x2;
  194.             serializedTriangle.y2 = this._y2;
  195.             serializedTriangle.x3 = this._x3;
  196.             serializedTriangle.y3 = this._y3;
  197.             return serializedTriangle;
  198.         };
  199.        
  200.         Triangle.prototype.draw = function () {
  201.             this.canvas().element.beginPath();
  202.             this.canvas().element.fillStyle = this.serialize().color;
  203.             this.canvas().element.moveTo(this.serialize().x, this.serialize().y);
  204.             this.canvas().element.lineTo(this.serialize().x2 + this.serialize().x, this.serialize().y2 + this.serialize().y);
  205.             this.canvas().element.lineTo(this.serialize().x3 + this.serialize().x, this.serialize().y3 + this.serialize().y);
  206.             this.canvas().element.fill();
  207.         };
  208.        
  209.         Triangle.prototype.toString = function () {
  210.             return "Triangle: " + JSON.stringify(this.serialize());
  211.         };
  212.        
  213.         return Triangle;
  214.     }());
  215.    
  216.     // Segment
  217.     var Segment = (function () {
  218.         function Segment(x, y, color, x2, y2) {
  219.             Shape.call(this, x, y, color);
  220.             this._x2 = x2;
  221.             this._y2 = y2;
  222.         }
  223.        
  224.         Segment.prototype = new Shape();
  225.        
  226.         Segment.prototype.serialize = function () {
  227.             var serializedSegment = Shape.prototype.serialize.call(this);
  228.             serializedSegment.x2 = this._x2;
  229.             serializedSegment.y2 = this._y2;
  230.             return serializedSegment;
  231.         };
  232.        
  233.         Segment.prototype.draw = function () {
  234.             this.canvas().element.beginPath();
  235.             this.canvas().element.moveTo(this.serialize().x, this.serialize().y);
  236.             this.canvas().element.lineTo(this.serialize().x2 + this.serialize().x, this.serialize().y2 + this.serialize().y);
  237.             this.canvas().element.strokeStyle = this.serialize().color;
  238.             this.canvas().element.stroke();
  239.         };
  240.        
  241.         Segment.prototype.toString = function () {
  242.             return "Segment: " + JSON.stringify(this.serialize());
  243.         };
  244.        
  245.         return Segment;
  246.     }());
  247.    
  248.     return {
  249.         Shape: Shape,
  250.         Circle: Circle,
  251.         Rectangle: Rectangle,
  252.         Point: Point,
  253.         Triangle: Triangle,
  254.         Segment: Segment,
  255.     };
  256.  
  257. }());
  258.  
  259. var rectangle = new Shapes.Rectangle(2, 1000, "red", 200, 300);
  260. console.log(rectangle.toString());
  261.  
  262. var point = new Shapes.Point(1, 2, "blue");
  263. console.log(point.toString());
  264.  
  265. var triangle = new Shapes.Triangle(1, 2, "red", 2, 0, 3, 3);
  266. console.log(triangle.toString());
  267.  
  268. var segment = new Shapes.Segment(1, 2, "red", 22, 45);
  269. console.log(segment.toString());
  270.  
  271. var circle = new Shapes.Circle(1, 2, "blue", 10);
  272. console.log(circle.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement