/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ /** *
cc.Vec2 extensions based on Chipmunk's cpVect file.
* These extensions work both with cc.Vec2
The "ccp" prefix means: "CoCos2d Point"
*/ /** * smallest such that 1.0+FLT_EPSILON != 1.0 * @constant * @type Number */ cc.POINT_EPSILON = parseFloat('1.192092896e-07F'); /** * Returns opposite of point. * @method pNeg * @param {Vec2} point * @return {Vec2} */ cc.pNeg = function (point) { return cc.p(-point.x, -point.y); }; /** * Calculates sum of two points. * @method pAdd * @param {Vec2} v1 * @param {Vec2} v2 * @return {Vec2} * @examples {@link utils/api/engine/docs/cocos2d/core/support/pAdd.js} */ cc.pAdd = function (v1, v2) { return cc.p(v1.x + v2.x, v1.y + v2.y); }; /** * Calculates difference of two points. * @method pSub * @param {Vec2} v1 * @param {Vec2} v2 * @return {Vec2} */ cc.pSub = function (v1, v2) { return cc.p(v1.x - v2.x, v1.y - v2.y); }; /** * Returns point multiplied by given factor. * @method pMult * @param {Vec2} point * @param {Number} floatVar * @return {Vec2} */ cc.pMult = function (point, floatVar) { return cc.p(point.x * floatVar, point.y * floatVar); }; /** * Calculates midpoint between two points. * @method pMidpoint * @param {Vec2} v1 * @param {Vec2} v2 * @return {pMult} */ cc.pMidpoint = function (v1, v2) { return cc.pMult(cc.pAdd(v1, v2), 0.5); }; /** * Calculates dot product of two points. * @method pDot * @param {Vec2} v1 * @param {Vec2} v2 * @return {Number} */ cc.pDot = function (v1, v2) { return v1.x * v2.x + v1.y * v2.y; }; /** * Calculates cross product of two points. * @method pCross * @param {Vec2} v1 * @param {Vec2} v2 * @return {Number} */ cc.pCross = function (v1, v2) { return v1.x * v2.y - v1.y * v2.x; }; /** * Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) greater than 0 * @method pPerp * @param {Vec2} point * @return {Vec2} */ cc.pPerp = function (point) { return cc.p(-point.y, point.x); }; /** * Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) smaller than 0 * @method pRPerp * @param {Vec2} point * @return Vec2} */ cc.pRPerp = function (point) { return cc.p(point.y, -point.x); }; /** * Calculates the projection of v1 over v2. * @method pProject * @param {Vec2} v1 * @param {Vec2} v2 * @return {pMult} */ cc.pProject = function (v1, v2) { return cc.pMult(v2, cc.pDot(v1, v2) / cc.pDot(v2, v2)); }; /** * Rotates two points. * @method pRotate * @param {Vec2} v1 * @param {Vec2} v2 * @return {Vec2} */ cc.pRotate = function (v1, v2) { return cc.p(v1.x * v2.x - v1.y * v2.y, v1.x * v2.y + v1.y * v2.x); }; /** * Unrotates two points. * @method pUnrotate * @param {Vec2} v1 * @param {Vec2} v2 * @return {Vec2} */ cc.pUnrotate = function (v1, v2) { return cc.p(v1.x * v2.x + v1.y * v2.y, v1.y * v2.x - v1.x * v2.y); }; /** * Calculates the square length of a cc.Vec2 (not calling sqrt() ) * @method pLengthSQ * @param {Vec2} v * @return {Number} */ cc.pLengthSQ = function (v) { return cc.pDot(v, v); }; /** * Calculates the square distance between two points (not calling sqrt() ) * @method pDistanceSQ * @param {Vec2} point1 * @param {Vec2} point2 * @return {Number} */ cc.pDistanceSQ = function(point1, point2){ return cc.pLengthSQ(cc.pSub(point1,point2)); }; /** * Calculates distance between point an origin * @method pLength * @param {Vec2} v * @return {Number} */ cc.pLength = function (v) { return Math.sqrt(cc.pLengthSQ(v)); }; /** * Calculates the distance between two points * @method pDistance * @param {Vec2} v1 * @param {Vec2} v2 * @return {Number} */ cc.pDistance = function (v1, v2) { return cc.pLength(cc.pSub(v1, v2)); }; /** * Returns point multiplied to a length of 1. * @method pNormalize * @param {Vec2} v * @return {Vec2} */ cc.pNormalize = function (v) { var n = cc.pLength(v); return n === 0 ? cc.p(v) : cc.pMult(v, 1.0 / n); }; /** * Converts radians to a normalized vector. * @method pForAngle * @param {Number} a * @return {Vec2} */ cc.pForAngle = function (a) { return cc.p(Math.cos(a), Math.sin(a)); }; /** * Converts a vector to radians. * @method pToAngle * @param {Vec2} v * @return {Number} */ cc.pToAngle = function (v) { return Math.atan2(v.y, v.x); }; /** * Clamp a value between from and to. * @method clampf * @param {Number} value * @param {Number} min_inclusive * @param {Number} max_inclusive * @return {Number} */ cc.clampf = function (value, min_inclusive, max_inclusive) { if (min_inclusive > max_inclusive) { var temp = min_inclusive; min_inclusive = max_inclusive; max_inclusive = temp; } return value < min_inclusive ? min_inclusive : value < max_inclusive ? value : max_inclusive; }; /** * Clamp a value between 0 and 1. * @method clamp01 * @param {Number} value * @return {Number} */ cc.clamp01 = function (value) { return value < 0 ? 0 : value < 1 ? value : 1; }; /** * Clamp a point between from and to. * @method pClamp * @param {Point} p * @param {Number} min_inclusive * @param {Number} max_inclusive * @return {Vec2} */ cc.pClamp = function (p, min_inclusive, max_inclusive) { return cc.p(cc.clampf(p.x, min_inclusive.x, max_inclusive.x), cc.clampf(p.y, min_inclusive.y, max_inclusive.y)); }; /** * Quickly convert cc.Size to a cc.Vec2. * @method pFromSize * @param {Size} s * @return {Vec2} */ cc.pFromSize = function (s) { return cc.p(s.width, s.height); }; /** * Run a math operation function on each point component