      1 (function() {
      2
      3   /* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
      4
      5   var fabric = this.fabric || (this.fabric = { });
      6
      7   if (fabric.Point) {
      8     fabric.warn('fabric.Point is already defined');
      9     return;
     10   }
     11
     12   fabric.Point = Point;
     13
     14   /**
     15    * @name Point
     16    * @memberOf fabric
     17    * @constructor
     18    * @param {Number} x
     19    * @param {Number} y
     20    * @return {fabric.Point} thisArg
     21    */
     22   function Point(x, y) {
     23     if (arguments.length > 0) {
     24       this.init(x, y);
     25     }
     26   }
     27
     28   Point.prototype = /** @scope fabric.Point.prototype */ {
     29
     30     constructor: Point,
     31
     32     /**
     33      * @method init
     34      * @param {Number} x
     35      * @param {Number} y
     36      */
     37     init: function (x, y) {
     38       this.x = x;
     39       this.y = y;
     40     },
     41
     42     /**
     43      * @method add
     44      * @param {fabric.Point} that
     45      * @return {fabric.Point} new Point instance with added values
     46      */
     47     add: function (that) {
     48       return new Point(this.x + that.x, this.y + that.y);
     49     },
     50
     51     /**
     52      * @method addEquals
     53      * @param {fabric.Point} that
     54      * @return {fabric.Point} thisArg
     55      */
     56     addEquals: function (that) {
     57       this.x += that.x;
     58       this.y += that.y;
     59       return this;
     60     },
     61
     62     /**
     63      * @method scalarAdd
     64      * @param {Number} scalar
     65      * @return {fabric.Point} new Point with added value
     66      */
     67     scalarAdd: function (scalar) {
     68       return new Point(this.x + scalar, this.y + scalar);
     69     },
     70
     71     /**
     72      * @method scalarAddEquals
     73      * @param {Number} scalar
     74      * @param {fabric.Point} thisArg
     75      */
     76     scalarAddEquals: function (scalar) {
     77       this.x += scalar;
     78       this.y += scalar;
     79       return this;
     80     },
     81
     82     /**
     83      * @method subtract
     84      * @param {fabric.Point} that
     85      * @return {fabric.Point} new Point object with subtracted values
     86      */
     87     subtract: function (that) {
     88       return new Point(this.x - that.x, this.y - that.y);
     89     },
     90
     91     /**
     92      * @method subtractEquals
     93      * @param {fabric.Point} that
     94      * @return {fabric.Point} thisArg
     95      */
     96     subtractEquals: function (that) {
     97       this.x -= that.x;
     98       this.y -= that.y;
     99       return this;
    100     },
    101
    102     scalarSubtract: function (scalar) {
    103       return new Point(this.x - scalar, this.y - scalar);
    104     },
    105
    106     scalarSubtractEquals: function (scalar) {
    107       this.x -= scalar;
    108       this.y -= scalar;
    109       return this;
    110     },
    111
    112     multiply: function (scalar) {
    113       return new Point(this.x * scalar, this.y * scalar);
    114     },
    115
    116     multiplyEquals: function (scalar) {
    117       this.x *= scalar;
    118       this.y *= scalar;
    119       return this;
    120     },
    121
    122     divide: function (scalar) {
    123       return new Point(this.x / scalar, this.y / scalar);
    124     },
    125
    126     divideEquals: function (scalar) {
    127       this.x /= scalar;
    128       this.y /= scalar;
    129       return this;
    130     },
    131
    132     eq: function (that) {
    133       return (this.x == that.x && this.y == that.y);
    134     },
    135
    136     lt: function (that) {
    137       return (this.x < that.x && this.y < that.y);
    138     },
    139
    140     lte: function (that) {
    141       return (this.x <= that.x && this.y <= that.y);
    142     },
    143
    144     gt: function (that) {
    145       return (this.x > that.x && this.y > that.y);
    146     },
    147
    148     gte: function (that) {
    149       return (this.x >= that.x && this.y >= that.y);
    150     },
    151
    152     lerp: function (that, t) {
    153       return new Point(this.x + (that.x - this.x) * t, this.y + (that.y - this.y) * t);
    154     },
    155
    156     distanceFrom: function (that) {
    157       var dx = this.x - that.x,
    158           dy = this.y - that.y;
    159       return Math.sqrt(dx * dx + dy * dy);
    160     },
    161
    162     min: function (that) {
    163       return new Point(Math.min(this.x, that.x), Math.min(this.y, that.y));
    164     },
    165
    166     max: function (that) {
    167       return new Point(Math.max(this.x, that.x), Math.max(this.y, that.y));
    168     },
    169
    170     toString: function () {
    171       return this.x + "," + this.y;
    172     },
    173
    174     setXY: function (x, y) {
    175       this.x = x;
    176       this.y = y;
    177     },
    178
    179     setFromPoint: function (that) {
    180       this.x = that.x;
    181       this.y = that.y;
    182     },
    183
    184     swap: function (that) {
    185       var x = this.x,
    186           y = this.y;
    187       this.x = that.x;
    188       this.y = that.y;
    189       that.x = x;
    190       that.y = y;
    191     }
    192   };
    193
    194 })();