2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCGEMETRY_H__
|
|
|
|
#define __CCGEMETRY_H__
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-06-08 13:55:28 +08:00
|
|
|
#include "CCObject.h"
|
2013-05-04 15:18:22 +08:00
|
|
|
#include <math.h>
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup data_structures
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-05-04 15:18:22 +08:00
|
|
|
// for CCPoint assignement operator and copy constructor
|
2013-05-03 11:24:52 +08:00
|
|
|
class CC_DLL CCSize;
|
|
|
|
|
2013-01-15 18:59:35 +08:00
|
|
|
class CC_DLL CCPoint
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CCPoint();
|
|
|
|
CCPoint(float x, float y);
|
2012-04-23 14:30:38 +08:00
|
|
|
CCPoint(const CCPoint& other);
|
2013-05-03 11:24:52 +08:00
|
|
|
CCPoint(const CCSize& size);
|
2012-04-23 14:30:38 +08:00
|
|
|
CCPoint& operator= (const CCPoint& other);
|
2013-05-03 11:24:52 +08:00
|
|
|
CCPoint& operator= (const CCSize& size);
|
2013-05-02 13:13:03 +08:00
|
|
|
CCPoint operator+(const CCPoint& right) const;
|
|
|
|
CCPoint operator-(const CCPoint& right) const;
|
2013-05-04 15:18:22 +08:00
|
|
|
CCPoint operator-() const;
|
2013-05-02 13:13:03 +08:00
|
|
|
CCPoint operator*(float a) const;
|
|
|
|
CCPoint operator/(float a) const;
|
2012-04-23 14:30:38 +08:00
|
|
|
void setPoint(float x, float y);
|
2012-08-01 15:30:12 +08:00
|
|
|
bool equals(const CCPoint& target) const;
|
2013-05-04 15:18:22 +08:00
|
|
|
bool fuzzyEquals(const CCPoint& target, float variance) const;
|
|
|
|
|
|
|
|
inline float length() const {
|
|
|
|
return sqrtf(x*x + y*y);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline float lengthSq() const {
|
|
|
|
return dot(*this); //x*x + y*y;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline float angle() const {
|
|
|
|
return atan2f(y, x);
|
|
|
|
};
|
|
|
|
|
|
|
|
float angle(const CCPoint& other) const;
|
|
|
|
|
|
|
|
inline float dot(const CCPoint& other) const {
|
|
|
|
return x*other.x + y*other.y;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline float cross(const CCPoint& other) const {
|
|
|
|
return x*other.y - y*other.x;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint perp() const {
|
|
|
|
return CCPoint(-y, x);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint rPerp() const {
|
|
|
|
return CCPoint(y, -x);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint project(const CCPoint& other) const {
|
|
|
|
return other * dot(other)/other.dot(other);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint rotate(const CCPoint& other) const {
|
|
|
|
return CCPoint(x*other.x - y*other.y, x*other.y + y*other.x);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint unrotate(const CCPoint& other) const {
|
|
|
|
return CCPoint(x*other.x + y*other.y, y*other.x - x*other.y);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint normalize() const {
|
|
|
|
return *this / length();
|
|
|
|
};
|
|
|
|
|
|
|
|
inline CCPoint lerp(const CCPoint& other, float alpha) const {
|
|
|
|
return *this * (1.f - alpha) + other * alpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
CCPoint rotateByAngle(const CCPoint& pivot, float angle) const;
|
|
|
|
|
|
|
|
static inline CCPoint forAngle(const float a)
|
|
|
|
{
|
|
|
|
return CCPoint(cosf(a), sinf(a));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2013-01-15 18:59:35 +08:00
|
|
|
class CC_DLL CCSize
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CCSize();
|
|
|
|
CCSize(float width, float height);
|
2012-04-23 14:30:38 +08:00
|
|
|
CCSize(const CCSize& other);
|
2013-05-03 11:24:52 +08:00
|
|
|
CCSize(const CCPoint& point);
|
2012-04-23 14:30:38 +08:00
|
|
|
CCSize& operator= (const CCSize& other);
|
2013-05-03 11:24:52 +08:00
|
|
|
CCSize& operator= (const CCPoint& point);
|
|
|
|
CCSize operator+(const CCSize& right) const;
|
|
|
|
CCSize operator-(const CCSize& right) const;
|
|
|
|
CCSize operator*(float a) const;
|
|
|
|
CCSize operator/(float a) const;
|
2012-04-23 14:30:38 +08:00
|
|
|
void setSize(float width, float height);
|
2012-08-01 15:30:12 +08:00
|
|
|
bool equals(const CCSize& target) const;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2013-01-15 18:59:35 +08:00
|
|
|
class CC_DLL CCRect
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCPoint origin;
|
|
|
|
CCSize size;
|
|
|
|
|
|
|
|
public:
|
2013-05-03 11:24:52 +08:00
|
|
|
CCRect();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRect(float x, float y, float width, float height);
|
2012-04-23 14:30:38 +08:00
|
|
|
CCRect(const CCRect& other);
|
2013-05-03 11:24:52 +08:00
|
|
|
CCRect& operator= (const CCRect& other);
|
2012-04-23 14:30:38 +08:00
|
|
|
void setRect(float x, float y, float width, float height);
|
2012-08-01 15:30:12 +08:00
|
|
|
float getMinX() const; /// return the leftmost x-value of current rect
|
|
|
|
float getMidX() const; /// return the midpoint x-value of current rect
|
|
|
|
float getMaxX() const; /// return the rightmost x-value of current rect
|
|
|
|
float getMinY() const; /// return the bottommost y-value of current rect
|
|
|
|
float getMidY() const; /// return the midpoint y-value of current rect
|
|
|
|
float getMaxY() const; /// return the topmost y-value of current rect
|
|
|
|
bool equals(const CCRect& rect) const;
|
|
|
|
bool containsPoint(const CCPoint& point) const;
|
|
|
|
bool intersectsRect(const CCRect& rect) const;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-25 16:18:04 +08:00
|
|
|
#define CCPointMake(x, y) CCPoint((float)(x), (float)(y))
|
|
|
|
#define CCSizeMake(width, height) CCSize((float)(width), (float)(height))
|
|
|
|
#define CCRectMake(x, y, width, height) CCRect((float)(x), (float)(y), (float)(width), (float)(height))
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
const CCPoint CCPointZero = CCPointMake(0,0);
|
|
|
|
|
|
|
|
/* The "zero" size -- equivalent to CCSizeMake(0, 0). */
|
|
|
|
const CCSize CCSizeZero = CCSizeMake(0,0);
|
|
|
|
|
|
|
|
/* The "zero" rectangle -- equivalent to CCRectMake(0, 0, 0, 0). */
|
|
|
|
const CCRect CCRectZero = CCRectMake(0,0,0,0);
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of data_structure group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CCGEMETRY_H__
|