dim4o

Geometry-PseudoClassical-Inheritance

Mar 20th, 2015
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shapes = (function () {
  2.     Function.prototype.inherits = function(parent) {
  3.         this.prototype = Object.create(parent.prototype);
  4.         this.prototype.constructor = this;
  5.     };
  6.     // Validations
  7.     function isNumber(num) {
  8.         return !isNaN(parseFloat(num)) && isFinite(num);
  9.     }
  10.     function isValidCoordinate(coordinate) {
  11.         if (!isNumber(coordinate)) {
  12.             throw new Error("Invalid point coordinate!");
  13.         }
  14.     }
  15.     function isValidDimension(dim) {
  16.  
  17.         if (!isNumber(dim) || dim < 0) {
  18.             throw new Error("Invalid dimension!");
  19.         }
  20.     }
  21.  
  22.     function isValidColor(color) {
  23.         var regEx = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
  24.         if (!regEx.test(color)) {
  25.             throw  new Error("Invalid color hexadecimal format!");
  26.         }
  27.     }
  28.  
  29.     // Pseudo classes
  30.     var Point = (function () {
  31.         function Point(x, y) {
  32.             this.setX(x);
  33.             this.setY(y);
  34.         }
  35.  
  36.         Point.prototype.getX = function () {
  37.             return this._x;
  38.         };
  39.         Point.prototype.setX = function (x) {
  40.             isValidCoordinate(x);
  41.             this._x = x;
  42.         };
  43.  
  44.         Point.prototype.getY = function () {
  45.             return this._y;
  46.         };
  47.         Point.prototype.setY = function (y) {
  48.             isValidCoordinate(y);
  49.             this._y = y;
  50.         };
  51.  
  52.         Point.prototype.toString = function () {
  53.             return "(" + this.getX() + ", " + this.getY() + ")";
  54.         };
  55.         return Point;
  56.     }());
  57.  
  58.     // Shape - abstract class
  59.     var Shape = (function () {
  60.         // constructor
  61.         function Shape(point, color) {
  62.             // not necessary
  63.             if ( this.constructor === Shape) {
  64.                 throw Error("Can not instantiate!")
  65.             }
  66.             Point.call(this, point._x, point._y);
  67.             this.setPoint(point);
  68.             this.setColor(color);
  69.         }
  70.  
  71.         Shape.inherits(Point);
  72.  
  73.         Shape.prototype.getPoint = function () {
  74.             return this._point;
  75.         };
  76.         Shape.prototype.setPoint = function (point) {
  77.             this._point = point;
  78.         };
  79.  
  80.         Shape.prototype.getColor = function () {
  81.             return this._color;
  82.         };
  83.         Shape.prototype.setColor = function () {
  84.  
  85.         };
  86.         Shape.prototype.getColor = function () {
  87.             return this._color;
  88.         };
  89.         // validates color format
  90.         Shape.prototype.setColor = function (color) {
  91.             isValidColor(color);
  92.             this._color = color;
  93.         };
  94.  
  95.         Shape.prototype.toString = function () {
  96.             return "Color = "+ this.getColor().toUpperCase() + ", First point: "
  97.                 + this.getPoint();
  98.         };
  99.  
  100.         return Shape;
  101.     }());
  102.  
  103.     var Circle = (function () {
  104.         function Circle(point, radius, color) {
  105.             Shape.call(this, point, color);
  106.             this.setRadius(radius);
  107.         }
  108.  
  109.         Circle.inherits(Shape);
  110.  
  111.         Circle.prototype.getRadius = function () {
  112.             return this._radius;
  113.         };
  114.         Circle.prototype.setRadius = function (radius) {
  115.             isValidDimension(radius);
  116.             this._radius = radius;
  117.         };
  118.  
  119.         Circle.prototype.toString = function () {
  120.             return Shape.prototype.toString.call(this).replace("First", "Center")
  121.                 + ", radius: " + this.getRadius();
  122.         };
  123.         return Circle;
  124.     }());
  125.  
  126.     var Line = (function () {
  127.         function Line(firstPoint, endPoint, color) {
  128.             Shape.call(this, firstPoint, color);
  129.             this.setEndPoint(endPoint);
  130.         }
  131.  
  132.         Line.inherits(Shape);
  133.  
  134.         Line.prototype.getEndPoint = function () {
  135.             return this._endPoint;
  136.         };
  137.         Line.prototype.setEndPoint = function (endPoint) {
  138.             this._endPoint = endPoint;
  139.         };
  140.  
  141.         Line.prototype.toString = function () {
  142.             return Shape.prototype.toString.call(this)
  143.                 + ", Second point: " + this.getEndPoint();
  144.         };
  145.  
  146.         return Line;
  147.     }());
  148.  
  149.     var Segment = (function () {
  150.         function Segment(firstPoint, endPoint, color) {
  151.             Line.call(this, firstPoint, endPoint, color);
  152.         }
  153.  
  154.         Segment.inherits(Line);
  155.  
  156.         return Segment;
  157.     }());
  158.  
  159.     var Rectangle = (function () {
  160.         function Rectangle(startPoint, width, height, color) {
  161.             Shape.call(this, startPoint, color);
  162.             this.setWidth(width);
  163.             this.setHeight(height);
  164.         }
  165.  
  166.         Rectangle.inherits(Shape);
  167.  
  168.         Rectangle.prototype.getWidth = function () {
  169.             return this._witdh;
  170.         };
  171.         Rectangle.prototype.setWidth = function (width) {
  172.             isValidDimension(width);
  173.             this._witdh = width;
  174.         };
  175.  
  176.         Rectangle.prototype.getHeight= function () {
  177.             return this._height;
  178.         };
  179.         Rectangle.prototype.setHeight = function (height) {
  180.             isValidDimension(height);
  181.             this._height = height;
  182.         };
  183.  
  184.         Rectangle.prototype.toString = function (){
  185.             return Shape.prototype.toString.call(this)
  186.                 + ", width: " + this.getWidth()
  187.                 + ", height: " + this.getHeight();
  188.         };
  189.         return Rectangle;
  190.     }());
  191.  
  192.     var Triangle  = (function () {
  193.         function Triangle(startPoint, secondPoint, thirdPoint, color) {
  194.             Line.call(this, startPoint, secondPoint, color);
  195.             this.setThirdPoint(thirdPoint);
  196.         }
  197.         Triangle.inherits(Line);
  198.  
  199.         Triangle.prototype.getThirdPoint = function () {
  200.             return this._thirdPoint;
  201.         };
  202.         Triangle.prototype.setThirdPoint = function (thirdPoint) {
  203.             this._thirdPoint = thirdPoint;
  204.         };
  205.  
  206.         Triangle.prototype.toString = function (){
  207.             return Line.prototype.toString.call(this)
  208.                 + ", Third point: " + this.getThirdPoint();
  209.         };
  210.         return Triangle;
  211.     }());
  212.  
  213.     return {
  214.         Point: Point,
  215.         Line: Line,
  216.         Segment: Segment,
  217.         Rectangle: Rectangle,
  218.         Triangle: Triangle,
  219.         Circle: Circle
  220.     }
  221. }());
  222.  
  223.  
  224.  
  225. var point1 = new shapes.Point("3",5);
  226. var point2 = new shapes.Point(6,"7");
  227. var point3 = new shapes.Point(12, 8);
  228.  
  229. console.log("Point: " + point1.toString());
  230.  
  231. var rec = new shapes.Rectangle(point1, "20", 30, "#123ffd");
  232.  
  233. console.log("Rectangle: " + rec.toString());
  234.  
  235. var line = new shapes.Line(point1, point2, "#abc");
  236. console.log("Line: " + line.toString());
  237.  
  238. var tri = new shapes.Triangle(point1, point2, point3, "#Ab4586");
  239. console.log("Triangle: " + tri.toString());
  240.  
  241. var seg = new shapes.Segment(point1, point2, "#ffF321");
  242. console.log("Segment: " + seg.toString());
  243.  
  244. var cir = new shapes.Circle(point1, 5, "#aaa");
  245. console.log("Circle: " + cir.toString());
Advertisement
Add Comment
Please, Sign In to add comment