deprecate Point, using Vector2 instead

This commit is contained in:
Huabing.Xu 2014-04-15 17:46:44 +08:00
parent 5303f50904
commit 917ae6e88b
2 changed files with 615 additions and 609 deletions

View File

@ -30,280 +30,280 @@ THE SOFTWARE.
// implementation of Point
NS_CC_BEGIN
Point::Point(void) : x(0), y(0)
{
}
// Point::Point(void) : x(0), y(0)
// {
// }
Point::Point(float xx, float yy) : x(xx), y(yy)
{
}
// Point::Point(float xx, float yy) : x(xx), y(yy)
// {
// }
Point::Point(const Point& other) : x(other.x), y(other.y)
{
}
// Point::Point(const Point& other) : x(other.x), y(other.y)
// {
// }
Point::Point(const Size& size) : x(size.width), y(size.height)
{
}
// Point::Point(const Size& size) : x(size.width), y(size.height)
// {
// }
Point& Point::operator= (const Point& other)
{
setPoint(other.x, other.y);
return *this;
}
// Point& Point::operator= (const Point& other)
// {
// setPoint(other.x, other.y);
// return *this;
// }
Point& Point::operator= (const Size& size)
{
setPoint(size.width, size.height);
return *this;
}
// Point& Point::operator= (const Size& size)
// {
// setPoint(size.width, size.height);
// return *this;
// }
Point Point::operator+(const Point& right) const
{
return Point(this->x + right.x, this->y + right.y);
}
// Point Point::operator+(const Point& right) const
// {
// return Point(this->x + right.x, this->y + right.y);
// }
Point& Point::operator+=(const Point& right)
{
this->x += right.x;
this->y += right.y;
return *this;
}
// Point& Point::operator+=(const Point& right)
// {
// this->x += right.x;
// this->y += right.y;
// return *this;
// }
Point Point::operator-(const Point& right) const
{
return Point(this->x - right.x, this->y - right.y);
}
// Point Point::operator-(const Point& right) const
// {
// return Point(this->x - right.x, this->y - right.y);
// }
Point& Point::operator-=(const Point& right)
{
this->x -= right.x;
this->y -= right.y;
return *this;
}
// Point& Point::operator-=(const Point& right)
// {
// this->x -= right.x;
// this->y -= right.y;
// return *this;
// }
Point Point::operator-() const
{
return Point(-x, -y);
}
// Point Point::operator-() const
// {
// return Point(-x, -y);
// }
bool Point::operator==(const Point& right)
{
return this->x == right.x && this->y == right.y;
}
// bool Point::operator==(const Point& right)
// {
// return this->x == right.x && this->y == right.y;
// }
bool Point::operator!=(const Point& right)
{
return this->x != right.x || this->y != right.y;
}
// bool Point::operator!=(const Point& right)
// {
// return this->x != right.x || this->y != right.y;
// }
bool Point::operator==(const Point& right) const
{
return this->x == right.x && this->y == right.y;
}
// bool Point::operator==(const Point& right) const
// {
// return this->x == right.x && this->y == right.y;
// }
bool Point::operator!=(const Point& right) const
{
return this->x != right.x || this->y != right.y;
}
// bool Point::operator!=(const Point& right) const
// {
// return this->x != right.x || this->y != right.y;
// }
Point Point::operator*(float a) const
{
return Point(this->x * a, this->y * a);
}
// Point Point::operator*(float a) const
// {
// return Point(this->x * a, this->y * a);
// }
Point Point::operator/(float a) const
{
CCASSERT(a!=0, "CCPoint division by 0.");
return Point(this->x / a, this->y / a);
}
// Point Point::operator/(float a) const
// {
// CCASSERT(a!=0, "CCPoint division by 0.");
// return Point(this->x / a, this->y / a);
// }
void Point::setPoint(float xx, float yy)
{
this->x = xx;
this->y = yy;
}
// void Point::setPoint(float xx, float yy)
// {
// this->x = xx;
// this->y = yy;
// }
bool Point::equals(const Point& target) const
{
return (fabs(this->x - target.x) < FLT_EPSILON)
&& (fabs(this->y - target.y) < FLT_EPSILON);
}
// bool Point::equals(const Point& target) const
// {
// return (fabs(this->x - target.x) < FLT_EPSILON)
// && (fabs(this->y - target.y) < FLT_EPSILON);
// }
bool Point::fuzzyEquals(const Point& b, float var) const
{
if(x - var <= b.x && b.x <= x + var)
if(y - var <= b.y && b.y <= y + var)
return true;
return false;
}
// bool Point::fuzzyEquals(const Point& b, float var) const
// {
// if(x - var <= b.x && b.x <= x + var)
// if(y - var <= b.y && b.y <= y + var)
// return true;
// return false;
// }
float Point::getAngle(const Point& other) const
{
Point a2 = normalize();
Point b2 = other.normalize();
float angle = atan2f(a2.cross(b2), a2.dot(b2));
if( fabs(angle) < FLT_EPSILON ) return 0.f;
return angle;
}
// float Point::getAngle(const Point& other) const
// {
// Point a2 = normalize();
// Point b2 = other.normalize();
// float angle = atan2f(a2.cross(b2), a2.dot(b2));
// if( fabs(angle) < FLT_EPSILON ) return 0.f;
// return angle;
// }
Point Point::rotateByAngle(const Point& pivot, float angle) const
{
return pivot + (*this - pivot).rotate(Point::forAngle(angle));
}
// Point Point::rotateByAngle(const Point& pivot, float angle) const
// {
// return pivot + (*this - pivot).rotate(Point::forAngle(angle));
// }
bool Point::isOneDimensionSegmentOverlap(float A, float B, float C, float D, float *S, float *E)
{
float ABmin = MIN(A, B);
float ABmax = MAX(A, B);
float CDmin = MIN(C, D);
float CDmax = MAX(C, D);
// bool Point::isOneDimensionSegmentOverlap(float A, float B, float C, float D, float *S, float *E)
// {
// float ABmin = MIN(A, B);
// float ABmax = MAX(A, B);
// float CDmin = MIN(C, D);
// float CDmax = MAX(C, D);
if (ABmax < CDmin || CDmax < ABmin)
{
// ABmin->ABmax->CDmin->CDmax or CDmin->CDmax->ABmin->ABmax
return false;
}
else
{
if (ABmin >= CDmin && ABmin <= CDmax)
{
// CDmin->ABmin->CDmax->ABmax or CDmin->ABmin->ABmax->CDmax
if (S != nullptr) *S = ABmin;
if (E != nullptr) *E = CDmax < ABmax ? CDmax : ABmax;
}
else if (ABmax >= CDmin && ABmax <= CDmax)
{
// ABmin->CDmin->ABmax->CDmax
if (S != nullptr) *S = CDmin;
if (E != nullptr) *E = ABmax;
}
else
{
// ABmin->CDmin->CDmax->ABmax
if (S != nullptr) *S = CDmin;
if (E != nullptr) *E = CDmax;
}
return true;
}
}
// if (ABmax < CDmin || CDmax < ABmin)
// {
// // ABmin->ABmax->CDmin->CDmax or CDmin->CDmax->ABmin->ABmax
// return false;
// }
// else
// {
// if (ABmin >= CDmin && ABmin <= CDmax)
// {
// // CDmin->ABmin->CDmax->ABmax or CDmin->ABmin->ABmax->CDmax
// if (S != nullptr) *S = ABmin;
// if (E != nullptr) *E = CDmax < ABmax ? CDmax : ABmax;
// }
// else if (ABmax >= CDmin && ABmax <= CDmax)
// {
// // ABmin->CDmin->ABmax->CDmax
// if (S != nullptr) *S = CDmin;
// if (E != nullptr) *E = ABmax;
// }
// else
// {
// // ABmin->CDmin->CDmax->ABmax
// if (S != nullptr) *S = CDmin;
// if (E != nullptr) *E = CDmax;
// }
// return true;
// }
// }
bool Point::isLineIntersect(const Point& A, const Point& B,
const Point& C, const Point& D,
float *S, float *T)
{
// FAIL: Line undefined
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
{
return false;
}
// bool Point::isLineIntersect(const Point& A, const Point& B,
// const Point& C, const Point& D,
// float *S, float *T)
// {
// // FAIL: Line undefined
// if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
// {
// return false;
// }
const float denom = crossProduct2Vector(A, B, C, D);
// const float denom = crossProduct2Vector(A, B, C, D);
if (denom == 0)
{
// Lines parallel or overlap
return false;
}
// if (denom == 0)
// {
// // Lines parallel or overlap
// return false;
// }
if (S != nullptr) *S = crossProduct2Vector(C, D, C, A) / denom;
if (T != nullptr) *T = crossProduct2Vector(A, B, C, A) / denom;
// if (S != nullptr) *S = crossProduct2Vector(C, D, C, A) / denom;
// if (T != nullptr) *T = crossProduct2Vector(A, B, C, A) / denom;
return true;
}
// return true;
// }
bool Point::isLineParallel(const Point& A, const Point& B,
const Point& C, const Point& D)
{
// FAIL: Line undefined
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
{
return false;
}
// bool Point::isLineParallel(const Point& A, const Point& B,
// const Point& C, const Point& D)
// {
// // FAIL: Line undefined
// if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
// {
// return false;
// }
if (crossProduct2Vector(A, B, C, D) == 0)
{
// line overlap
if (crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0)
{
return false;
}
// if (crossProduct2Vector(A, B, C, D) == 0)
// {
// // line overlap
// if (crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0)
// {
// return false;
// }
return true;
}
// return true;
// }
return false;
}
// return false;
// }
bool Point::isLineOverlap(const Point& A, const Point& B,
const Point& C, const Point& D)
{
// FAIL: Line undefined
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
{
return false;
}
// bool Point::isLineOverlap(const Point& A, const Point& B,
// const Point& C, const Point& D)
// {
// // FAIL: Line undefined
// if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
// {
// return false;
// }
if (crossProduct2Vector(A, B, C, D) == 0 &&
(crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0))
{
return true;
}
// if (crossProduct2Vector(A, B, C, D) == 0 &&
// (crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0))
// {
// return true;
// }
return false;
}
// return false;
// }
bool Point::isSegmentOverlap(const Point& A, const Point& B, const Point& C, const Point& D, Point* S, Point* E)
{
// bool Point::isSegmentOverlap(const Point& A, const Point& B, const Point& C, const Point& D, Point* S, Point* E)
// {
if (isLineOverlap(A, B, C, D))
{
return isOneDimensionSegmentOverlap(A.x, B.x, C.x, D.x, &S->x, &E->x) &&
isOneDimensionSegmentOverlap(A.y, B.y, C.y, D.y, &S->y, &E->y);
}
// if (isLineOverlap(A, B, C, D))
// {
// return isOneDimensionSegmentOverlap(A.x, B.x, C.x, D.x, &S->x, &E->x) &&
// isOneDimensionSegmentOverlap(A.y, B.y, C.y, D.y, &S->y, &E->y);
// }
return false;
}
// return false;
// }
bool Point::isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D)
{
float S, T;
// bool Point::isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D)
// {
// float S, T;
if (isLineIntersect(A, B, C, D, &S, &T )&&
(S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f))
{
return true;
}
// if (isLineIntersect(A, B, C, D, &S, &T )&&
// (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f))
// {
// return true;
// }
return false;
}
// return false;
// }
Point Point::getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D)
{
float S, T;
// Point Point::getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D)
// {
// float S, T;
if (isLineIntersect(A, B, C, D, &S, &T))
{
// Point of intersection
Point P;
P.x = A.x + S * (B.x - A.x);
P.y = A.y + S * (B.y - A.y);
return P;
}
// if (isLineIntersect(A, B, C, D, &S, &T))
// {
// // Point of intersection
// Point P;
// P.x = A.x + S * (B.x - A.x);
// P.y = A.y + S * (B.y - A.y);
// return P;
// }
return Point::ZERO;
}
// return Point::ZERO;
// }
const Point Point::ZERO = Point(0.0f, 0.0f);
const Point Point::ANCHOR_MIDDLE = Point(0.5f, 0.5f);
const Point Point::ANCHOR_BOTTOM_LEFT = Point(0.0f, 0.0f);
const Point Point::ANCHOR_TOP_LEFT = Point(0.0f, 1.0f);
const Point Point::ANCHOR_BOTTOM_RIGHT = Point(1.0f, 0.0f);
const Point Point::ANCHOR_TOP_RIGHT = Point(1.0f, 1.0f);
const Point Point::ANCHOR_MIDDLE_RIGHT = Point(1.0f, 0.5f);
const Point Point::ANCHOR_MIDDLE_LEFT = Point(0.0f, 0.5f);
const Point Point::ANCHOR_MIDDLE_TOP = Point(0.5f, 1.0f);
const Point Point::ANCHOR_MIDDLE_BOTTOM = Point(0.5f, 0.0f);
// const Point Point::ZERO = Point(0.0f, 0.0f);
// const Point Point::ANCHOR_MIDDLE = Point(0.5f, 0.5f);
// const Point Point::ANCHOR_BOTTOM_LEFT = Point(0.0f, 0.0f);
// const Point Point::ANCHOR_TOP_LEFT = Point(0.0f, 1.0f);
// const Point Point::ANCHOR_BOTTOM_RIGHT = Point(1.0f, 0.0f);
// const Point Point::ANCHOR_TOP_RIGHT = Point(1.0f, 1.0f);
// const Point Point::ANCHOR_MIDDLE_RIGHT = Point(1.0f, 0.5f);
// const Point Point::ANCHOR_MIDDLE_LEFT = Point(0.0f, 0.5f);
// const Point Point::ANCHOR_MIDDLE_TOP = Point(0.5f, 1.0f);
// const Point Point::ANCHOR_MIDDLE_BOTTOM = Point(0.5f, 0.0f);
// implementation of Size

View File

@ -45,424 +45,430 @@ USING_NS_CC_MATH;
// for Point assignement operator and copy constructor
class CC_DLL Size;
class CC_DLL Point
{
public:
float x;
float y;
// class CC_DLL Point
// {
// public:
// float x;
// float y;
//conversion to and from Vector2
public:
operator Vector2() const { return Vector2(x, y); }
Point(const Vector2& v):x(v.x), y(v.y) {}
public:
/**
* @js NA
*/
Point();
/**
* @js NA
*/
Point(float x, float y);
/**
* @js NA
* @lua NA
*/
Point(const Point& other);
/**
* @js NA
* @lua NA
*/
explicit Point(const Size& size);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Point& other);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Size& size);
/**
* @js NA
* @lua NA
*/
Point operator+(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point& operator+=(const Point& right);
/**
* @js NA
* @lua NA
*/
Point operator-(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point& operator-=(const Point& right);
/**
* @js NA
* @lua NA
*/
Point operator-() const;
/**
* @js NA
* @lua NA
*/
bool operator==(const Point& right);
/**
* @js NA
* @lua NA
*/
bool operator!=(const Point& right);
/**
* @js NA
* @lua NA
*/
bool operator==(const Point& right) const;
/**
* @js NA
* @lua NA
*/
bool operator!=(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point operator*(float a) const;
/**
* @js NA
* @lua NA
*/
Point operator/(float a) const;
/**
* @js NA
* @lua NA
*/
void setPoint(float x, float y);
/**
* @js NA
*/
bool equals(const Point& target) const;
// //conversion to and from Vector2
// public:
// operator Vector2() const { return Vector2(x, y); }
// Point(const Vector2& v):x(v.x), y(v.y) {}
// public:
// /**
// * @js NA
// */
// Point();
// /**
// * @js NA
// */
// Point(float x, float y);
// /**
// * @js NA
// * @lua NA
// */
// Point(const Point& other);
// /**
// * @js NA
// * @lua NA
// */
// explicit Point(const Size& size);
// /**
// * @js NA
// * @lua NA
// */
// Point& operator= (const Point& other);
// /**
// * @js NA
// * @lua NA
// */
// Point& operator= (const Size& size);
// /**
// * @js NA
// * @lua NA
// */
// Point operator+(const Point& right) const;
// /**
// * @js NA
// * @lua NA
// */
// Point& operator+=(const Point& right);
// /**
// * @js NA
// * @lua NA
// */
// Point operator-(const Point& right) const;
// /**
// * @js NA
// * @lua NA
// */
// Point& operator-=(const Point& right);
// /**
// * @js NA
// * @lua NA
// */
// Point operator-() const;
// /**
// * @js NA
// * @lua NA
// */
// bool operator==(const Point& right);
// /**
// * @js NA
// * @lua NA
// */
// bool operator!=(const Point& right);
// /**
// * @js NA
// * @lua NA
// */
// bool operator==(const Point& right) const;
// /**
// * @js NA
// * @lua NA
// */
// bool operator!=(const Point& right) const;
// /**
// * @js NA
// * @lua NA
// */
// Point operator*(float a) const;
// /**
// * @js NA
// * @lua NA
// */
// Point operator/(float a) const;
// /**
// * @js NA
// * @lua NA
// */
// void setPoint(float x, float y);
// /**
// * @js NA
// */
// bool equals(const Point& target) const;
/** @returns if points have fuzzy equality which means equal with some degree of variance.
@since v2.1.4
* @js NA
* @lua NA
*/
bool fuzzyEquals(const Point& target, float variance) const;
// /** @returns if points have fuzzy equality which means equal with some degree of variance.
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// bool fuzzyEquals(const Point& target, float variance) const;
/** Calculates distance between point an origin
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLength() const {
return sqrtf(x*x + y*y);
};
// /** Calculates distance between point an origin
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float getLength() const {
// return sqrtf(x*x + y*y);
// };
/** Calculates the square length of a Point (not calling sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLengthSq() const {
return dot(*this); //x*x + y*y;
};
// /** Calculates the square length of a Point (not calling sqrt() )
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float getLengthSq() const {
// return dot(*this); //x*x + y*y;
// };
/** Calculates the square distance between two points (not calling sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistanceSq(const Point& other) const {
return (*this - other).getLengthSq();
};
// /** Calculates the square distance between two points (not calling sqrt() )
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float getDistanceSq(const Point& other) const {
// return (*this - other).getLengthSq();
// };
/** Calculates the distance between two points
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistance(const Point& other) const {
return (*this - other).getLength();
};
// /** Calculates the distance between two points
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float getDistance(const Point& other) const {
// return (*this - other).getLength();
// };
/** @returns the angle in radians between this vector and the x axis
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getAngle() const {
return atan2f(y, x);
};
// /** @returns the angle in radians between this vector and the x axis
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float getAngle() const {
// return atan2f(y, x);
// };
/** @returns the angle in radians between two vector directions
@since v2.1.4
* @js NA
* @lua NA
*/
float getAngle(const Point& other) const;
// /** @returns the angle in radians between two vector directions
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// float getAngle(const Point& other) const;
/** Calculates dot product of two points.
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float dot(const Point& other) const {
return x*other.x + y*other.y;
};
// /** Calculates dot product of two points.
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float dot(const Point& other) const {
// return x*other.x + y*other.y;
// };
/** Calculates cross product of two points.
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float cross(const Point& other) const {
return x*other.y - y*other.x;
};
// /** Calculates cross product of two points.
// @return float
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline float cross(const Point& other) const {
// return x*other.y - y*other.x;
// };
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getPerp() const {
return Point(-y, x);
};
// /** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
// @return Point
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point getPerp() const {
// return Point(-y, x);
// };
/** Calculates midpoint between two points.
@return Point
@since v3.0
* @js NA
* @lua NA
*/
inline Point getMidpoint(const Point& other) const
{
return Point((x + other.x) / 2.0f, (y + other.y) / 2.0f);
}
// /** Calculates midpoint between two points.
// @return Point
// @since v3.0
// * @js NA
// * @lua NA
// */
// inline Point getMidpoint(const Point& other) const
// {
// return Point((x + other.x) / 2.0f, (y + other.y) / 2.0f);
// }
/** Clamp a point between from and to.
@since v3.0
* @js NA
* @lua NA
*/
inline Point getClampPoint(const Point& min_inclusive, const Point& max_inclusive) const
{
return Point(clampf(x,min_inclusive.x,max_inclusive.x), clampf(y, min_inclusive.y, max_inclusive.y));
}
// /** Clamp a point between from and to.
// @since v3.0
// * @js NA
// * @lua NA
// */
// inline Point getClampPoint(const Point& min_inclusive, const Point& max_inclusive) const
// {
// return Point(clampf(x,min_inclusive.x,max_inclusive.x), clampf(y, min_inclusive.y, max_inclusive.y));
// }
/** Run a math operation function on each point component
* absf, fllorf, ceilf, roundf
* any function that has the signature: float func(float);
* For example: let's try to take the floor of x,y
* p.compOp(floorf);
@since v3.0
* @js NA
* @lua NA
*/
inline Point compOp(std::function<float(float)> function) const
{
return Point(function(x), function(y));
}
// /** Run a math operation function on each point component
// * absf, fllorf, ceilf, roundf
// * any function that has the signature: float func(float);
// * For example: let's try to take the floor of x,y
// * p.compOp(floorf);
// @since v3.0
// * @js NA
// * @lua NA
// */
// inline Point compOp(std::function<float(float)> function) const
// {
// return Point(function(x), function(y));
// }
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getRPerp() const {
return Point(y, -x);
};
// /** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
// @return Point
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point getRPerp() const {
// return Point(y, -x);
// };
/** Calculates the projection of this over other.
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point project(const Point& other) const {
return other * (dot(other)/other.dot(other));
};
// /** Calculates the projection of this over other.
// @return Point
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point project(const Point& other) const {
// return other * (dot(other)/other.dot(other));
// };
/** Complex multiplication of two points ("rotates" two points).
@return Point vector with an angle of this.getAngle() + other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point rotate(const Point& other) const {
return Point(x*other.x - y*other.y, x*other.y + y*other.x);
};
// /** Complex multiplication of two points ("rotates" two points).
// @return Point vector with an angle of this.getAngle() + other.getAngle(),
// and a length of this.getLength() * other.getLength().
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point rotate(const Point& other) const {
// return Point(x*other.x - y*other.y, x*other.y + y*other.x);
// };
/** Unrotates two points.
@return Point vector with an angle of this.getAngle() - other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point unrotate(const Point& other) const {
return Point(x*other.x + y*other.y, y*other.x - x*other.y);
};
// /** Unrotates two points.
// @return Point vector with an angle of this.getAngle() - other.getAngle(),
// and a length of this.getLength() * other.getLength().
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point unrotate(const Point& other) const {
// return Point(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)
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point normalize() const {
float length = getLength();
if(length == 0.) return Point(1.f, 0);
return *this / getLength();
};
// /** Returns point multiplied to a length of 1.
// * If the point is 0, it returns (1, 0)
// @return Point
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point normalize() const {
// float length = getLength();
// if(length == 0.) return Point(1.f, 0);
// return *this / getLength();
// };
/** Linear Interpolation between two points a and b
@returns
alpha == 0 ? a
alpha == 1 ? b
otherwise a value between a..b
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point lerp(const Point& other, float alpha) const {
return *this * (1.f - alpha) + other * alpha;
};
// /** Linear Interpolation between two points a and b
// @returns
// alpha == 0 ? a
// alpha == 1 ? b
// otherwise a value between a..b
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// inline Point lerp(const Point& 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 in radians
@returns the rotated point
@since v2.1.4
* @js NA
* @lua NA
*/
Point rotateByAngle(const Point& pivot, float angle) const;
// /** 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 in radians
// @returns the rotated point
// @since v2.1.4
// * @js NA
// * @lua NA
// */
// Point rotateByAngle(const Point& pivot, float angle) const;
/**
* @js NA
* @lua NA
*/
static inline Point forAngle(const float a)
{
return Point(cosf(a), sinf(a));
}
// /**
// * @js NA
// * @lua NA
// */
// static inline Point forAngle(const float a)
// {
// return Point(cosf(a), sinf(a));
// }
/** A general line-line intersection test
@param A the startpoint for the first line L1 = (A - B)
@param B the endpoint for the first line L1 = (A - B)
@param C the startpoint for the second line L2 = (C - D)
@param D the endpoint for the second line L2 = (C - D)
@param S the range for a hitpoint in L1 (p = A + S*(B - A))
@param T the range for a hitpoint in L2 (p = C + T*(D - C))
@returns whether these two lines interects.
// /** A general line-line intersection test
// @param A the startpoint for the first line L1 = (A - B)
// @param B the endpoint for the first line L1 = (A - B)
// @param C the startpoint for the second line L2 = (C - D)
// @param D the endpoint for the second line L2 = (C - D)
// @param S the range for a hitpoint in L1 (p = A + S*(B - A))
// @param T the range for a hitpoint in L2 (p = C + T*(D - C))
// @returns whether these two lines interects.
Note that to truly test intersection for segments we have to make
sure that S & T lie within [0..1] and for rays, make sure S & T > 0
the hit point is C + T * (D - C);
the hit point also is A + S * (B - A);
@since 3.0
* @js NA
* @lua NA
*/
static bool isLineIntersect(const Point& A, const Point& B,
const Point& C, const Point& D,
float *S = nullptr, float *T = nullptr);
// Note that to truly test intersection for segments we have to make
// sure that S & T lie within [0..1] and for rays, make sure S & T > 0
// the hit point is C + T * (D - C);
// the hit point also is A + S * (B - A);
// @since 3.0
// * @js NA
// * @lua NA
// */
// static bool isLineIntersect(const Point& A, const Point& B,
// const Point& C, const Point& D,
// float *S = nullptr, float *T = nullptr);
/**
returns true if Line A-B overlap with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineOverlap(const Point& A, const Point& B,
const Point& C, const Point& D);
// /**
// returns true if Line A-B overlap with segment C-D
// @since v3.0
// * @js NA
// * @lua NA
// */
// static bool isLineOverlap(const Point& A, const Point& B,
// const Point& C, const Point& D);
/**
returns true if Line A-B parallel with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineParallel(const Point& A, const Point& B,
const Point& C, const Point& D);
// /**
// returns true if Line A-B parallel with segment C-D
// @since v3.0
// * @js NA
// * @lua NA
// */
// static bool isLineParallel(const Point& A, const Point& B,
// const Point& C, const Point& D);
/**
returns true if Segment A-B overlap with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentOverlap(const Point& A, const Point& B,
const Point& C, const Point& D,
Point* S = nullptr, Point* E = nullptr);
// /**
// returns true if Segment A-B overlap with segment C-D
// @since v3.0
// * @js NA
// * @lua NA
// */
// static bool isSegmentOverlap(const Point& A, const Point& B,
// const Point& C, const Point& D,
// Point* S = nullptr, Point* E = nullptr);
/**
returns true if Segment A-B intersects with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D);
// /**
// returns true if Segment A-B intersects with segment C-D
// @since v3.0
// * @js NA
// * @lua NA
// */
// static bool isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D);
/**
returns the intersection point of line A-B, C-D
@since v3.0
* @js NA
* @lua NA
*/
static Point getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D);
// /**
// returns the intersection point of line A-B, C-D
// @since v3.0
// * @js NA
// * @lua NA
// */
// static Point getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D);
/** equals to Point(0,0) */
static const Point ZERO;
/** equals to Point(0.5, 0.5) */
static const Point ANCHOR_MIDDLE;
/** equals to Point(0, 0) */
static const Point ANCHOR_BOTTOM_LEFT;
/** equals to Point(0, 1) */
static const Point ANCHOR_TOP_LEFT;
/** equals to Point(1, 0) */
static const Point ANCHOR_BOTTOM_RIGHT;
/** equals to Point(1, 1) */
static const Point ANCHOR_TOP_RIGHT;
/** equals to Point(1, 0.5) */
static const Point ANCHOR_MIDDLE_RIGHT;
/** equals to Point(0, 0.5) */
static const Point ANCHOR_MIDDLE_LEFT;
/** equals to Point(0.5, 1) */
static const Point ANCHOR_MIDDLE_TOP;
/** equals to Point(0.5, 0) */
static const Point ANCHOR_MIDDLE_BOTTOM;
// /** equals to Point(0,0) */
// static const Point ZERO;
// /** equals to Point(0.5, 0.5) */
// static const Point ANCHOR_MIDDLE;
// /** equals to Point(0, 0) */
// static const Point ANCHOR_BOTTOM_LEFT;
// /** equals to Point(0, 1) */
// static const Point ANCHOR_TOP_LEFT;
// /** equals to Point(1, 0) */
// static const Point ANCHOR_BOTTOM_RIGHT;
// /** equals to Point(1, 1) */
// static const Point ANCHOR_TOP_RIGHT;
// /** equals to Point(1, 0.5) */
// static const Point ANCHOR_MIDDLE_RIGHT;
// /** equals to Point(0, 0.5) */
// static const Point ANCHOR_MIDDLE_LEFT;
// /** equals to Point(0.5, 1) */
// static const Point ANCHOR_MIDDLE_TOP;
// /** equals to Point(0.5, 0) */
// static const Point ANCHOR_MIDDLE_BOTTOM;
private:
// returns true if segment A-B intersects with segment C-D. S->E is the ovderlap part
static bool isOneDimensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E);
// private:
// // returns true if segment A-B intersects with segment C-D. S->E is the ovderlap part
// static bool isOneDimensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E);
// cross procuct of 2 vector. A->B X C->D
static float crossProduct2Vector(const Point& A, const Point& B, const Point& C, const Point& D) { return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); }
};
// // cross procuct of 2 vector. A->B X C->D
// static float crossProduct2Vector(const Point& A, const Point& B, const Point& C, const Point& D) { return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); }
// };
CC_DEPRECATED_ATTRIBUTE typedef Vector2 Point;
class CC_DLL Size
{
public:
float width;
float height;
public:
operator Vector2() const
{
return Vector2(width, height);
}
public:
/**