1 /**
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008, Luke Benstead.
  4  All rights reserved.
  5 
  6  Redistribution and use in source and binary forms, with or without modification,
  7  are permitted provided that the following conditions are met:
  8 
  9  Redistributions of source code must retain the above copyright notice,
 10  this list of conditions and the following disclaimer.
 11  Redistributions in binary form must reproduce the above copyright notice,
 12  this list of conditions and the following disclaimer in the documentation
 13  and/or other materials provided with the distribution.
 14 
 15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 18  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 19  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 20  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 21  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 22  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 25  */
 26 
 27 cc.kmVec2 = function (x, y) {
 28     this.x = x || 0;
 29     this.y = y || 0;
 30 };
 31 
 32 cc.kmVec2Fill = function (pOut, x, y) {
 33     pOut.x = x;
 34     pOut.y = y;
 35     return pOut;
 36 };
 37 
 38 cc.kmVec2Length = function (pIn) {
 39     return Math.sqrt(cc.kmSQR(pIn.x) + cc.kmSQR(pIn.y));
 40 };
 41 
 42 cc.kmVec2LengthSq = function (pIn) {
 43     return cc.kmSQR(pIn.x) + cc.kmSQR(pIn.y);
 44 };
 45 
 46 cc.kmVec2Normalize = function (pOut, pIn) {
 47     var l = 1.0 / cc.kmVec2Length(pIn);
 48 
 49     var v = new cc.kmVec2();
 50     v.x = pIn.x * l;
 51     v.y = pIn.y * l;
 52 
 53     pOut.x = v.x;
 54     pOut.y = v.y;
 55 
 56     return pOut;
 57 };
 58 
 59 cc.kmVec2Add = function (pOut, pV1, pV2) {
 60     pOut.x = pV1.x + pV2.x;
 61     pOut.y = pV1.y + pV2.y;
 62 
 63     return pOut
 64 };
 65 
 66 cc.kmVec2Dot = function (pV1, pV2) {
 67     return pV1.x * pV2.x + pV1.y * pV2.y;
 68 };
 69 
 70 cc.kmVec2Subtract = function (pOut, pV1, pV2) {
 71     pOut.x = pV1.x - pV2.x;
 72     pOut.y = pV1.y - pV2.y;
 73 
 74     return pOut;
 75 };
 76 
 77 cc.kmVec2Transform = function (pOut, pV, pM) {
 78     var v= new cc.kmVec2();
 79 
 80     v.x = pV.x * pM.mat[0] + pV.y * pM.mat[3] + pM.mat[6];
 81     v.y = pV.x * pM.mat[1] + pV.y * pM.mat[4] + pM.mat[7];
 82 
 83     pOut.x = v.x;
 84     pOut.y = v.y;
 85 
 86     return pOut;
 87 };
 88 
 89 cc.kmVec2TransformCoord = function (pOut, pV, pM) {
 90     return null;
 91 };
 92 
 93 cc.kmVec2Scale = function (pOut, pIn, s) {
 94     pOut.x = pIn.x * s;
 95     pOut.y = pIn.y * s;
 96 
 97     return pOut;
 98 };
 99 
100 cc.kmVec2AreEqual = function (p1, p2) {
101     return (
102         (p1.x < p2.x + cc.kmEpsilon && p1.x > p2.x - cc.kmEpsilon) &&
103             (p1.y < p2.y + cc.kmEpsilon && p1.y > p2.y - cc.kmEpsilon)
104         );
105 };
106