public bool ContainsPoint(CCPoint point) { return(point.X >= MinX && point.X <= MaxX && point.Y >= MinY && point.Y <= MaxY); }
public CCRect(CCSize sz) { Origin = CCPoint.Zero; Size = sz; }
public static bool ContainsPoint(ref CCRect rect, ref CCPoint point) { bool bRet = false; if (float.IsNaN(point.X)) { point.X = 0; } if (float.IsNaN(point.Y)) { point.Y = 0; } if (point.X >= rect.MinX && point.X <= rect.MaxX && point.Y >= rect.MinY && point.Y <= rect.MaxY) { bRet = true; } return bRet; }
/** Unrotates two points. @return CCPoint @since v0.7.2 */ public static CCPoint Unrotate(CCPoint v1, CCPoint v2) { CCPoint pt; pt.X = v1.X * v2.X + v1.Y * v2.Y; pt.Y = v1.Y * v2.X - v1.X * v2.Y; return pt; }
public float DistanceSQ(ref CCPoint v2) { return Sub(ref v2).LengthSQ; }
/** Calculates cross product of two points. @return CGFloat @since v0.7.2 */ public static float CrossProduct(CCPoint v1, CCPoint v2) { return v1.X * v2.Y - v1.Y * v2.X; }
/** Calculates the projection of v1 over v2. @return CCPoint @since v0.7.2 */ public static CCPoint Project(CCPoint v1, CCPoint v2) { float dp1 = v1.X * v2.X + v1.Y * v2.Y; float dp2 = v2.LengthSQ; float f = dp1 / dp2; CCPoint pt; pt.X = v2.X * f; pt.Y = v2.Y * f; return pt; // return Multiply(v2, DotProduct(v1, v2) / DotProduct(v2, v2)); }
/* ccpSegmentIntersect returns YES if Segment A-B intersects with segment C-D @since v1.0.0 */ public static bool SegmentIntersect(CCPoint A, CCPoint B, CCPoint C, CCPoint D) { float S = 0, T = 0; if (LineIntersect(A, B, C, D, ref S, ref T) && (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f)) { return true; } return false; }
/* ccpIntersectPoint returns the intersection point of line A-B, C-D @since v1.0.0 */ public static CCPoint IntersectPoint(CCPoint A, CCPoint B, CCPoint C, CCPoint D) { float S = 0, T = 0; if (LineIntersect(A, B, C, D, ref S, ref T)) { // Point of intersection CCPoint P; P.X = A.X + S * (B.X - A.X); P.Y = A.Y + S * (B.Y - A.Y); return P; } return Zero; }
/** Rotates a point counter clockwise by the angle around a pivot @param v is the point to rotate @param pivot is the pivot, naturally @param angle is the angle of rotation cw in radians @returns the rotated point @since v0.99.1 */ public static CCPoint RotateByAngle(CCPoint v, CCPoint pivot, float angle) { CCPoint r = v - pivot; float cosa = (float)Math.Cos(angle), sina = (float)Math.Sin(angle); float t = r.X; r.X = t * cosa - r.Y * sina + pivot.X; r.Y = t * sina + r.Y * cosa + pivot.Y; return r; }
/** A general line-line intersection test @param p1 is the startpoint for the first line P1 = (p1 - p2) @param p2 is the endpoint for the first line P1 = (p1 - p2) @param p3 is the startpoint for the second line P2 = (p3 - p4) @param p4 is the endpoint for the second line P2 = (p3 - p4) @param s is the range for a hitpoint in P1 (pa = p1 + s*(p2 - p1)) @param t is the range for a hitpoint in P3 (pa = p2 + t*(p4 - p3)) @return bool indicating successful intersection of a line 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 p3 + t * (p4 - p3); the hit point also is p1 + s * (p2 - p1); @since v0.99.1 */ public static bool LineIntersect(CCPoint A, CCPoint B, CCPoint C, CCPoint D, ref float S, ref float T) { // FAIL: Line undefined if ((A.X == B.X && A.Y == B.Y) || (C.X == D.X && C.Y == D.Y)) { return false; } float BAx = B.X - A.X; float BAy = B.Y - A.Y; float DCx = D.X - C.X; float DCy = D.Y - C.Y; float ACx = A.X - C.X; float ACy = A.Y - C.Y; float denom = DCy * BAx - DCx * BAy; S = DCx * ACy - DCy * ACx; T = BAx * ACy - BAy * ACx; if (denom == 0) { if (S == 0 || T == 0) { // Lines incident return true; } // Lines parallel and not incident return false; } S = S / denom; T = T / denom; // Point of intersection // CGPoint P; // P.X = A.X + *S * (B.X - A.X); // P.y = A.y + *S * (B.y - A.y); return true; }
/** @returns the signed angle in radians between two vector directions @since v0.99.1 */ public static float AngleSigned(CCPoint a, CCPoint b) { // On Arm float.Epsilon is too small and evalutates to 0 // http://msdn.microsoft.com/en-us/library/system.single.epsilon(v=vs.110).aspx const float FLT_EPSILON = 1.175494351E-38f; CCPoint a2 = Normalize(a); CCPoint b2 = Normalize(b); var angle = (float)Math.Atan2(a2.X * b2.Y - a2.Y * b2.X, DotProduct(a2, b2)); if (Math.Abs(angle) < FLT_EPSILON) { return 0.0f; } return angle; }
/** Multiplies a nd b components, a.X*b.X, a.y*b.y @returns a component-wise multiplication @since v0.99.1 */ public static CCPoint MultiplyComponents(CCPoint a, CCPoint b) { CCPoint pt; pt.X = a.X * b.X; pt.Y = a.Y * b.Y; return pt; }
/** @returns if points have fuzzy equality which means equal with some degree of variance. @since v0.99.1 */ public static bool FuzzyEqual(CCPoint a, CCPoint b, float variance) { if (a.X - variance <= b.X && b.X <= a.X + variance) if (a.Y - variance <= b.Y && b.Y <= a.Y + variance) return true; return false; }
/** Linear Interpolation between two points a and b @returns alpha == 0 ? a alpha == 1 ? b otherwise a value between a..b @since v0.99.1 */ public static CCPoint Lerp(CCPoint a, CCPoint b, float alpha) { return (a * (1f - alpha) + b * alpha); }
public CCPoint(CCPoint pt) { X = pt.X; Y = pt.Y; }
public static float DotProduct(CCPoint v1, CCPoint v2) { return v1.X * v2.X + v1.Y * v2.Y; }
/** Converts a vector to radians. @return CGFloat @since v0.7.2 */ public static float ToAngle(CCPoint v) { return (float)Math.Atan2(v.Y, v.X); }
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0 @return CCPoint @since v0.7.2 */ public static CCPoint PerpendicularClockwise(CCPoint v) { CCPoint pt; pt.X = v.Y; pt.Y = -v.X; return pt; }
/** Clamp a point between from and to. @since v0.99.1 */ public static CCPoint Clamp(CCPoint p, CCPoint from, CCPoint to) { CCPoint pt; pt.X = Clamp(p.X, from.X, to.X); pt.Y = Clamp(p.Y, from.Y, to.Y); return pt; // return CreatePoint(Clamp(p.X, from.X, to.X), Clamp(p.Y, from.Y, to.Y)); }
public static bool Equal(ref CCPoint point1, ref CCPoint point2) { return ((point1.X == point2.X) && (point1.Y == point2.Y)); }
public static CCPoint Perp(CCPoint p) { CCPoint pt; pt.X = -p.Y; pt.Y = p.X; return pt; }
public bool Equals(CCPoint p) { return X == p.X && Y == p.Y; }
public static float Dot(CCPoint p1, CCPoint p2) { return p1.X * p2.X + p1.Y * p2.Y; }
public CCPoint Sub(ref CCPoint v2) { CCPoint pt; pt.X = X - v2.X; pt.Y = Y - v2.Y; return pt; }
public static float Distance(CCPoint v1, CCPoint v2) { return (v1 - v2).Length; }
public bool ContainsPoint(CCPoint point) { return point.X >= MinX && point.X <= MaxX && point.Y >= MinY && point.Y <= MaxY; }
public static CCPoint Normalize(CCPoint p) { float x = p.X; float y = p.Y; float l = 1f / (float)Math.Sqrt(x * x + y * y); CCPoint pt; pt.X = x * l; pt.Y = y * l; return pt; }
public static CCPoint Midpoint(CCPoint p1, CCPoint p2) { CCPoint pt; pt.X = (p1.X + p2.X) / 2f; pt.Y = (p1.Y + p2.Y) / 2f; return pt; }
/// <summary> /// Returns the Cardinal Spline position for a given set of control points, tension and time /// </summary> /// <param name="p0"></param> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <param name="tension"></param> /// <param name="t"></param> /// <returns></returns> public static CCPoint CCCardinalSplineAt(CCPoint p0, CCPoint p1, CCPoint p2, CCPoint p3, float tension, float t) { float t2 = t * t; float t3 = t2 * t; /* * Formula: s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt - tt)P4 */ float s = (1 - tension) / 2; float b1 = s * ((-t3 + (2 * t2)) - t); // s(-t3 + 2 t2 - t)P1 float b2 = s * (-t3 + t2) + (2 * t3 - 3 * t2 + 1); // s(-t3 + t2)P2 + (2 t3 - 3 t2 + 1)P2 float b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3 float b4 = s * (t3 - t2); // s(t3 - t2)P4 float x = (p0.X * b1 + p1.X * b2 + p2.X * b3 + p3.X * b4); float y = (p0.Y * b1 + p1.Y * b2 + p2.Y * b3 + p3.Y * b4); return new CCPoint(x, y); }
public static CCPoint ComputationOperation(CCPoint p, ComputationOperationDelegate del) { CCPoint pt; pt.X = del(p.X); pt.Y = del(p.Y); return pt; }