Create class for Point, Size and Rectangle - Node.js Data Structure

Node.js examples for Data Structure:Rectangle

Description

Create class for Point, Size and Rectangle

Demo Code


var Point = function(x, y) {
    this.x = x;/*from ww  w.  ja v  a 2  s .c om*/
    this.y = y;
};

Point.prototype = {
    x: 0,
    y: 0,
    getX: function() {
        return this.x;
    },
    getY: function() {
        return this.y;
    },
    setX: function(x) {
        this.x = x;
    },
    setY: function(y) {
        this.y = y;
    }
}

var Size = function(w, h) {
    this.width = w;
    this.height = h;
};

Size.prototype = {
    width: 0,
    height: 0,
    getWidth: function() {
        return this.width;
    },
    getHeight: function() {
        return this.height;
    }
}

var Rectangle = function(x, y, width, height) {
    this.location = new Point(x, y);
    this.size = new Size(width, height);
    
    this.center = new Point(x + width/2, y+height/2);
};
Rectangle.prototype = {
    location: new Point(0, 0),
    size: new Size(0, 0),
    center: new Point(0,0),
    getX: function() {
        return this.location.getX();
    },
    getY: function() {
        return this.location.getY();
    },
    getWidth: function() {
        return this.size.getWidth();
    },
    getHeight: function() {
        return this.size.getHeight();
    },
    getLocation: function() {
        return this.location;
    },
    getCenter: function() {
        return this.center;
    },
    getSize: function() {
        return this.size;
    },
    clone: function() {
        return new Rectangle(this.location, this.size);
    }
}

Related Tutorials