#ifndef __CCGEMETRY_H__
#define __CCGEMETRY_H__ //Geometry 几何学
#include "platform/CCPlatformMacros.h"
#include "CCObject.h"
#include <math.h>
NS_CC_BEGIN
// for CCPoint assignement(分配 ,任务) operator and copy constructor
class CC_DLL CCSize;
class CC_DLL CCPoint
{
public:
float x;
float y;
public:
CCPoint();
CCPoint(float x, float y);
CCPoint(const CCPoint& other);
CCPoint(const CCSize& size);
CCPoint& operator= (const CCPoint& other);
CCPoint& operator= (const CCSize& size);
CCPoint operator+(const CCPoint& right) const;
CCPoint operator-(const CCPoint& right) const;
CCPoint operator-() const;
CCPoint operator*(float a) const;
CCPoint operator/(float a) const;
void setPoint(float x, float y);
bool equals(const CCPoint& target) const;
/** @returns if points have fuzzy equality which means equal with some degree of variance. */
bool fuzzyEquals(const CCPoint& target, float variance) const;
/** Calculates distance between point an origin*/ 长度
inline float getLength() const {
return sqrtf(x*x + y*y);
};
/** Calculates thesquare(平方) length of a CCPoint (not calling sqrt() ) */长度平方(sqrt虽然得到长度 但耗 不必要情况使用距离平方)
inline float getLengthSq() const {
return dot(*this); //x*x + y*y;
};
/
/** Calculates dot product of two points.
*/
inline float dot(const CCPoint& other) const {
return x*other.x + y*other.y;
};
///
/** Calculates the square distance between two points (not calling sqrt() ) */两点距离平方
inline float getDistanceSq(const CCPoint& other) const {
return (*this - other).getLengthSq();
};
/** Calculates the distance between two points */ 两点距离
inline float getDistance(const CCPoint& other) const {
return (*this - other).getLength();
};
/** @returns the angle in radians(弧度) between this vector and the x axis(轴)*/ 得到向量与x轴夹角弧度值
inline float getAngle() const {
return atan2f(y, x);
};
/** @returns the angle in radians between two vector directions*/ 得到两个向量夹角弧度值
float getAngle(const CCPoint& other) const;
/** Calculates dot product(产品 乘积) of two points. */ 得到两个向量点乘结果
inline float dot(const CCPoint& other) const {
return x*other.x + y*other.y;
};
/** Calculates cross product of two points.*/ 得到两个向量叉乘结果
inline float cross(const CCPoint& other) const {
return x*other.y - y*other.x;
};
/** Calculates perpendicular(垂直) of v, rotated 90 degrees counter-clockwise(逆时针方向) -- cross(v, perp(v)) >= 0*/计算垂线
inline CCPoint getPerp() const {
return CCPoint(-y, x);
};
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0*/ 计算垂线
inline CCPoint getRPerp() const {
return CCPoint(y, -x);
};
/** Calculates the projection(投影) of this over other.*/ 得到this在另一个向量上的投影向量
inline CCPoint project(const CCPoint& other) const {
return other * (dot(other)/other.dot(other));
};
/** Complex(复杂的) multiplication([数] 乘法;增加) of two points ("rotates" two points).
@return CCPoint vector with an angle of this.getAngle() + other.getAngle(),
and a length of this.getLength() * other.getLength().*/
inline CCPoint rotate(const CCPoint& other) const {
return CCPoint(x*other.x - y*other.y, x*other.y + y*other.x);
};
/** Unrotates two points.
@return CCPoint vector with an angle of this.getAngle() - other.getAngle(),
and a length of this.getLength() * other.getLength().
*/
inline CCPoint unrotate(const CCPoint& other) const {
return CCPoint(x*other.x + y*other.y, y*other.x - x*other.y);
};
/** Returns point multiplied to a length of 1.
* If the point is 0, it returns (1, 0)
*/
inline CCPoint normalize() const { 单位化向量
float length = getLength();
if(length == 0.) return CCPoint(1.f, 0);
return *this / getLength();
};
/** Linear(直线的) Interpolation(插入 添加) between two points a and b//将两个向量线性结合 alpha为权重值
@returns
alpha == 0 ? a
alpha == 1 ? b
otherwise a value between a..b
*/
inline CCPoint lerp(const CCPoint& other, float alpha) const {
return *this * (1.f - alpha) + other * alpha;
};
/** Rotates a point counter(相反的) clockwise(顺时针方向地) by the angle around a pivot(枢轴)
@param pivot is the pivot, naturally
@param angle is the angle of rotation ccw(反时针方向(counterclockwise)) in radians
@returns the rotated point
@since v2.1.4
*/
CCPoint rotateByAngle(const CCPoint& pivot, float angle) const; 得到 以参数a为轴 参数b为旋转角度旋转得到的向量
static inline CCPoint forAngle(const float a)
{
return CCPoint(cosf(a), sinf(a));
}
};
class CC_DLL CCSize //两成员 float width; float height; 只是一个大小概念 无位置
{
public:
float width;
float height;
public:
CCSize();
CCSize(float width, float height);
CCSize(const CCSize& other);
CCSize(const CCPoint& point);
CCSize& operator= (const CCSize& other);
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;
void setSize(float width, float height);
bool equals(const CCSize& target) const;
};
class CC_DLL CCRect//成员 CCPoint origin; CCSize size; 前者确定位置 后者确定大小
{
public:
CCPoint origin;
CCSize size;
public:
CCRect();
CCRect(float x, float y, float width, float height);
CCRect(const CCRect& other);
CCRect& operator= (const CCRect& other);
void setRect(float x, float y, float width, float height);
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; //是否包含一个ccpoint
bool intersectsRect(const CCRect& rect) const;//是否与另一个rect相交
};
//宏定义
#defineCCPointMake(x, y) CCPoint((float)(x), (float)(y))
#defineCCSizeMake(width, height) CCSize((float)(width), (float)(height))
#defineCCRectMake(x, y, width, height) CCRect((float)(x), (float)(y), (float)(width), (float)(height))
const CCPointCCPointZero = CCPointMake(0,0);
/* The "zero" size -- equivalent to CCSizeMake(0, 0). */
const CCSizeCCSizeZero = CCSizeMake(0,0);
/* The "zero" rectangle -- equivalent to CCRectMake(0, 0, 0, 0). */
const CCRect CCRectZero = CCRectMake(0,0,0,0);
// end of data_structure group
/// @}
NS_CC_END
#endif //