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
|
2023-12-15 03:14:05 +03:00
|
|
|
* Copyright (C) 2010-2024 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#
|
|
|
|
*/
|
2020-06-26 02:01:14 +03: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#
|
|
|
|
*/
|
2020-06-26 02:01:14 +03:00
|
|
|
this.y = typeof ( y ) === "number" ? y : 0;
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2016-01-25 00:09:18 +03:00
|
|
|
/** @lends OpenSeadragon.Point.prototype */
|
|
|
|
$.Point.prototype = {
|
2014-08-19 03:04:49 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Point} a duplicate of this Point
|
|
|
|
*/
|
|
|
|
clone: function() {
|
|
|
|
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
|
|
|
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
|
|
|
/**
|
2018-09-01 11:59:20 +03:00
|
|
|
* Subtract another Point to this point and return a new Point.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @function
|
2018-09-01 11:59:20 +03:00
|
|
|
* @param {OpenSeadragon.Point} point The point to subtract vector components.
|
|
|
|
* @returns {OpenSeadragon.Point} A new point representing the subtraction 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
|
|
|
},
|
|
|
|
|
2016-09-02 17:31:36 +03:00
|
|
|
/**
|
|
|
|
* Compute the squared distance between this point and another point.
|
|
|
|
* Useful for optimizing things like comparing distances.
|
|
|
|
* @function
|
2016-09-23 06:01:32 +03:00
|
|
|
* @param {OpenSeadragon.Point} point The point to compute the squared distance with.
|
|
|
|
* @returns {Number} The squared distance between the 2 points
|
2016-09-02 17:31:36 +03:00
|
|
|
*/
|
|
|
|
squaredDistanceTo: function( point ) {
|
|
|
|
return Math.pow( this.x - point.x, 2 ) +
|
|
|
|
Math.pow( this.y - point.y, 2 );
|
|
|
|
},
|
|
|
|
|
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.
|
2015-12-12 03:07:23 +03:00
|
|
|
* @param {OpenSeadragon.Point} [pivot=(0,0)] Point around which to rotate.
|
|
|
|
* Defaults to the origin.
|
2013-08-14 01:39:22 +04:00
|
|
|
* @returns {OpenSeadragon.Point}. A new point representing the point rotated around the specified pivot
|
|
|
|
*/
|
2015-12-12 03:07:23 +03:00
|
|
|
rotate: function (degrees, pivot) {
|
|
|
|
pivot = pivot || new $.Point(0, 0);
|
2015-12-13 19:49:58 +03:00
|
|
|
var cos;
|
|
|
|
var sin;
|
|
|
|
// Avoid float computations when possible
|
|
|
|
if (degrees % 90 === 0) {
|
2016-08-17 16:43:08 +03:00
|
|
|
var d = $.positiveModulo(degrees, 360);
|
2015-12-13 19:49:58 +03:00
|
|
|
switch (d) {
|
|
|
|
case 0:
|
|
|
|
cos = 1;
|
|
|
|
sin = 0;
|
|
|
|
break;
|
|
|
|
case 90:
|
|
|
|
cos = 0;
|
|
|
|
sin = 1;
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
cos = -1;
|
|
|
|
sin = 0;
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
cos = 0;
|
|
|
|
sin = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var angle = degrees * Math.PI / 180.0;
|
|
|
|
cos = Math.cos(angle);
|
|
|
|
sin = Math.sin(angle);
|
|
|
|
}
|
2015-12-12 03:07:23 +03:00
|
|
|
var x = cos * (this.x - pivot.x) - sin * (this.y - pivot.y) + pivot.x;
|
|
|
|
var y = sin * (this.x - pivot.x) + cos * (this.y - pivot.y) + pivot.y;
|
|
|
|
return new $.Point(x, y);
|
2013-08-14 01:39:22 +04:00
|
|
|
},
|
|
|
|
|
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() {
|
2014-11-18 03:24:40 +03:00
|
|
|
return "(" + (Math.round(this.x * 100) / 100) + "," + (Math.round(this.y * 100) / 100) + ")";
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}( OpenSeadragon ));
|