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-06-19 21:33:25 +04:00
|
|
|
* 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.
|
2012-01-25 23:14:02 +04:00
|
|
|
* @class
|
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.
|
2013-06-19 21:33:25 +04:00
|
|
|
* @property {Number} [x] The vector component 'x'.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @property {Number} [y] The vector component 'y'.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
$.Point = function( x, y ) {
|
|
|
|
this.x = typeof ( x ) == "number" ? x : 0;
|
|
|
|
this.y = typeof ( y ) == "number" ? y : 0;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
$.Point.prototype = {
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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-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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
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
|
|
|
/**
|
|
|
|
* 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-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 ));
|