/* * OpenSeadragon - Rect * * Copyright (C) 2009 CodePlex Foundation * Copyright (C) 2010-2013 OpenSeadragon contributors * * 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. */ (function( $ ){ /** * A Rectangle really represents a 2x2 matrix where each row represents a * 2 dimensional vector component, the first is (x,y) and the second is * (width, height). The latter component implies the equation of a simple * plane. * * @class * @param {Number} x The vector component 'x'. * @param {Number} y The vector component 'y'. * @param {Number} width The vector component 'height'. * @param {Number} height The vector component 'width'. * @property {Number} x The vector component 'x'. * @property {Number} y The vector component 'y'. * @property {Number} width The vector component 'width'. * @property {Number} height The vector component 'height'. */ $.Rect = function( x, y, width, height ) { this.x = typeof ( x ) == "number" ? x : 0; this.y = typeof ( y ) == "number" ? y : 0; this.width = typeof ( width ) == "number" ? width : 0; this.height = typeof ( height ) == "number" ? height : 0; }; $.Rect.prototype = { /** * The aspect ratio is simply the ratio of width to height. * @function * @returns {Number} The ratio of width to height. */ getAspectRatio: function() { return this.width / this.height; }, /** * Provides the coordinates of the upper-left corner of the rectangle as a * point. * @function * @returns {OpenSeadragon.Point} The coordinate of the upper-left corner of * the rectangle. */ getTopLeft: function() { return new $.Point( this.x, this.y ); }, /** * Provides the coordinates of the bottom-right corner of the rectangle as a * point. * @function * @returns {OpenSeadragon.Point} The coordinate of the bottom-right corner of * the rectangle. */ getBottomRight: function() { return new $.Point( this.x + this.width, this.y + this.height ); }, /** * Provides the coordinates of the top-right corner of the rectangle as a * point. * @function * @returns {OpenSeadragon.Point} The coordinate of the top-right corner of * the rectangle. */ getTopRight: function() { return new $.Point( this.x + this.width, this.y ); }, /** * Provides the coordinates of the bottom-left corner of the rectangle as a * point. * @function * @returns {OpenSeadragon.Point} The coordinate of the bottom-left corner of * the rectangle. */ getBottomLeft: function() { return new $.Point( this.x, this.y + this.height ); }, /** * Computes the center of the rectangle. * @function * @returns {OpenSeadragon.Point} The center of the rectangle as represented * as represented by a 2-dimensional vector (x,y) */ getCenter: function() { return new $.Point( this.x + this.width / 2.0, this.y + this.height / 2.0 ); }, /** * Returns the width and height component as a vector OpenSeadragon.Point * @function * @returns {OpenSeadragon.Point} The 2 dimensional vector representing the * the width and height of the rectangle. */ getSize: function() { return new $.Point( this.width, this.height ); }, /** * Determines if two Rectangles have equivalent components. * @function * @param {OpenSeadragon.Rect} rectangle The Rectangle to compare to. * @return {Boolean} 'true' if all components are equal, otherwise 'false'. */ equals: function( other ) { return ( other instanceof $.Rect ) && ( this.x === other.x ) && ( this.y === other.y ) && ( this.width === other.width ) && ( this.height === other.height ); }, /** * Rotates a rectangle around a point. Currently only 90, 180, and 270 * degrees are supported. * @function * @param {Number} degrees The angle in degrees to rotate. * @param {OpenSeadragon.Point} pivot The point about which to rotate. * Defaults to the center of the rectangle. * @return {OpenSeadragon.Rect} */ rotate: function( degrees, pivot ) { // TODO support arbitrary rotation var width = this.width, height = this.height, newTopLeft; pivot = pivot || this.getCenter(); switch ( degrees ) { case 90: newTopLeft = this.getBottomLeft(); width = this.height; height = this.width; break; case 180: newTopLeft = this.getBottomRight(); break; case 270: newTopLeft = this.getTopRight(); width = this.height; height = this.width; break; default: newTopLeft = this.getTopLeft(); break; } newTopLeft = newTopLeft.rotate(degrees, pivot); return new $.Rect(newTopLeft.x, newTopLeft.y, width, height); }, /** * Provides a string representation of the rectangle which is useful for * debugging. * @function * @returns {String} A string representation of the rectangle. */ toString: function() { return "[" + Math.round(this.x*100) + "," + Math.round(this.y*100) + "," + Math.round(this.width*100) + "x" + Math.round(this.height*100) + "]"; } }; }( OpenSeadragon ));