2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - Point
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
(function( $ ){
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2013-11-25 20:48:44 +04:00
|
|
|
* @class Point
|
|
|
|
* @classdesc A Point is really used as a 2-dimensional vector, equally useful for
|
2012-02-01 00:59:09 +04:00
|
|
|
* representing a point on a plane, or the height and width of a plane
|
|
|
|
* not requiring any other frame of reference.
|
2013-11-25 20:48:44 +04:00
|
|
|
*
|
2013-11-16 10:19:53 +04:00
|
|
|
* @memberof OpenSeadragon
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {Number} [x] The vector component 'x'. Defaults to the origin at 0.
|
|
|
|
* @param {Number} [y] The vector component 'y'. Defaults to the origin at 0.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
$.Point = function( x, y ) {
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'x'.
|
|
|
|
* @member {Number} x
|
|
|
|
* @memberof OpenSeadragon.Point#
|
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
this.x = typeof ( x ) == "number" ? x : 0;
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* The vector component 'y'.
|
|
|
|
* @member {Number} y
|
|
|
|
* @memberof OpenSeadragon.Point#
|
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
this.y = typeof ( y ) == "number" ? y : 0;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2013-11-16 10:19:53 +04:00
|
|
|
$.Point.prototype = /** @lends OpenSeadragon.Point.prototype */{
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* Add another Point to this point and return a new Point.
|
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Point} point The point to add vector components.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the sum of the
|
|
|
|
* vector components
|
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
plus: function( point ) {
|
|
|
|
return new $.Point(
|
2013-06-19 21:33:25 +04:00
|
|
|
this.x + point.x,
|
2011-12-30 02:14:42 +04:00
|
|
|
this.y + point.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Substract another Point to this point and return a new Point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {OpenSeadragon.Point} point The point to substract vector components.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the substraction of the
|
2012-02-01 00:59:09 +04:00
|
|
|
* vector components
|
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
minus: function( point ) {
|
|
|
|
return new $.Point(
|
2013-06-19 21:33:25 +04:00
|
|
|
this.x - point.x,
|
2011-12-30 02:14:42 +04:00
|
|
|
this.y - point.y
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Multiply this point by a factor and return a new Point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {Number} factor The factor to multiply vector components.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the multiplication
|
|
|
|
* of the vector components by the factor
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
times: function( factor ) {
|
|
|
|
return new $.Point(
|
2013-06-19 21:33:25 +04:00
|
|
|
this.x * factor,
|
2011-12-30 02:14:42 +04:00
|
|
|
this.y * factor
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Divide this point by a factor and return a new Point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {Number} factor The factor to divide vector components.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the division of the
|
|
|
|
* vector components by the factor
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
divide: function( factor ) {
|
|
|
|
return new $.Point(
|
2013-06-19 21:33:25 +04:00
|
|
|
this.x / factor,
|
2011-12-30 02:14:42 +04:00
|
|
|
this.y / factor
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Compute the opposite of this point and return a new Point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the opposite of the
|
2012-02-01 00:59:09 +04:00
|
|
|
* vector components
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
negate: function() {
|
2011-12-30 02:14:42 +04:00
|
|
|
return new $.Point( -this.x, -this.y );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Compute the distance between this point and another point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {OpenSeadragon.Point} point The point to compute the distance with.
|
|
|
|
* @returns {Number} The distance between the 2 points
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
distanceTo: function( point ) {
|
|
|
|
return Math.sqrt(
|
|
|
|
Math.pow( this.x - point.x, 2 ) +
|
|
|
|
Math.pow( this.y - point.y, 2 )
|
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Apply a function to each coordinate of this point and return a new point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {function} func The function to apply to each coordinate.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point with the coordinates computed
|
|
|
|
* by the specified function
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
apply: function( func ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
return new $.Point( func( this.x ), func( this.y ) );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Check if this point is equal to another one.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @param {OpenSeadragon.Point} point The point to compare this point with.
|
|
|
|
* @returns {Boolean} true if they are equal, false otherwise.
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-30 02:14:42 +04:00
|
|
|
equals: function( point ) {
|
2013-06-19 21:33:25 +04:00
|
|
|
return (
|
|
|
|
point instanceof $.Point
|
|
|
|
) && (
|
|
|
|
this.x === point.x
|
|
|
|
) && (
|
|
|
|
this.y === point.y
|
2012-01-24 07:48:45 +04:00
|
|
|
);
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
|
2013-08-14 01:39:22 +04:00
|
|
|
/**
|
|
|
|
* Rotates the point around the specified pivot
|
|
|
|
* From http://stackoverflow.com/questions/4465931/rotate-rectangle-around-a-point
|
|
|
|
* @function
|
|
|
|
* @param {Number} degress to rotate around the pivot.
|
|
|
|
* @param {OpenSeadragon.Point} pivot Point about which to rotate.
|
|
|
|
* @returns {OpenSeadragon.Point}. A new point representing the point rotated around the specified pivot
|
|
|
|
*/
|
|
|
|
rotate: function ( degrees, pivot ) {
|
|
|
|
var angle = degrees * Math.PI / 180.0,
|
2013-08-16 02:15:20 +04:00
|
|
|
x = Math.cos( angle ) * ( this.x - pivot.x ) - Math.sin( angle ) * ( this.y - pivot.y ) + pivot.x,
|
|
|
|
y = Math.sin( angle ) * ( this.x - pivot.x ) + Math.cos( angle ) * ( this.y - pivot.y ) + pivot.y;
|
2013-08-14 01:39:22 +04:00
|
|
|
return new $.Point( x, y );
|
|
|
|
},
|
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
2014-02-05 01:59:45 +04:00
|
|
|
* Convert this point to a string in the format (x,y) where x and y are
|
|
|
|
* rounded to the nearest integer.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2014-02-05 01:59:45 +04:00
|
|
|
* @returns {String} A string representation of this point.
|
2012-02-01 00:59:09 +04:00
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
toString: function() {
|
2013-01-24 08:00:11 +04:00
|
|
|
return "(" + Math.round(this.x) + "," + Math.round(this.y) + ")";
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}( OpenSeadragon ));
|