/** Calculates difference of two points. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpSub(CCPoint v1, CCPoint v2) { return(ccp(v1.x - v2.x, v1.y - v2.y)); }
/** 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 ccpCompMult(CCPoint a, CCPoint b) { return(ccp(a.x * b.x, a.y * b.y)); }
/// <summary> /// Calculates sum of two points. ///@since v0.7.2 /// <returns>CCPoint</returns> public static CCPoint ccpAdd(CCPoint v1, CCPoint v2) { return(ccp(v1.x + v2.x, v1.y + v2.y)); }
public static void LineToPolygon(CCPoint[] points, float stroke, ccVertex2F[] vertices, int offset, int nuPoints) { nuPoints += offset; if (nuPoints <= 1) { return; } stroke *= 0.5f; int idx; int nuPointsMinus = nuPoints - 1; for (int i = offset; i < nuPoints; i++) { idx = i * 2; CCPoint p1 = points[i]; CCPoint perpVector; if (i == 0) { perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p1, points[i + 1]))); } else if (i == nuPointsMinus) { perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(points[i - 1], p1))); } else { CCPoint p2 = points[i + 1]; CCPoint p0 = points[i - 1]; CCPoint p2p1 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p2, p1)); CCPoint p0p1 = CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p0, p1)); // Calculate angle between vectors float angle = (float)Math.Acos(CCPointExtension.ccpDot(p2p1, p0p1)); if (angle < ccMacros.CC_DEGREES_TO_RADIANS(70)) { perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(p2p1, p0p1))); } else if (angle < ccMacros.CC_DEGREES_TO_RADIANS(170)) { perpVector = CCPointExtension.ccpNormalize(CCPointExtension.ccpMidpoint(p2p1, p0p1)); } else { perpVector = CCPointExtension.ccpPerp(CCPointExtension.ccpNormalize(CCPointExtension.ccpSub(p2, p0))); } } perpVector = CCPointExtension.ccpMult(perpVector, stroke); vertices[idx] = new ccVertex2F(p1.x + perpVector.x, p1.y + perpVector.y); vertices[idx + 1] = new ccVertex2F(p1.x - perpVector.x, p1.y - perpVector.y); } // Validate vertexes offset = (offset == 0) ? 0 : (offset - 1); for (int i = offset; i < nuPointsMinus; i++) { idx = i * 2; int idx1 = idx + 2; ccVertex2F p1 = vertices[idx]; ccVertex2F p2 = vertices[idx + 1]; ccVertex2F p3 = vertices[idx1]; ccVertex2F p4 = vertices[idx1 + 1]; float s = 0f; //BOOL fixVertex = !ccpLineIntersect(ccp(p1.x, p1.y), ccp(p4.x, p4.y), ccp(p2.x, p2.y), ccp(p3.x, p3.y), &s, &t); bool fixVertex = !LineIntersect(p1.x, p1.y, p4.x, p4.y, p2.x, p2.y, p3.x, p3.y, out s); if (!fixVertex) { if (s < 0.0f || s > 1.0f) { fixVertex = true; } } if (fixVertex) { vertices[idx1] = p4; vertices[idx1 + 1] = p3; } } }
/** 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 ccpLerp(CCPoint a, CCPoint b, float alpha) { return(ccpAdd(ccpMult(a, 1.0f - alpha), ccpMult(b, alpha))); }
/** Calculates dot product of two points. * @return CGFloat * @since v0.7.2 */ public static float ccpDot(CCPoint v1, CCPoint v2) { return(v1.x * v2.x + v1.y * v2.y); }
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0 * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpPerp(CCPoint v) { return(ccp(-v.y, v.x)); }
/** Returns point multiplied to a length of 1. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpNormalize(CCPoint v) { return(ccpMult(v, 1.0f / ccpLength(v))); }
/** Converts a vector to radians. * @return CGFloat * @since v0.7.2 */ public static float ccpToAngle(CCPoint v) { return((float)Math.Atan2(v.y, v.x)); }
/** Calculates distance between point an origin * @return CGFloat * @since v0.7.2 */ public static float ccpLength(CCPoint v) { return((float)Math.Sqrt(ccpLengthSQ(v))); }
/** Calculates the distance between two points * @return CGFloat * @since v0.7.2 */ public static float ccpDistance(CCPoint v1, CCPoint v2) { return(ccpLength(ccpSub(v1, v2))); }
/** Calculates the square length of a CCPoint (not calling sqrt() ) * @return CGFloat * @since v0.7.2 */ public static float ccpLengthSQ(CCPoint v) { return(ccpDot(v, v)); }
/** Unrotates two points. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpUnrotate(CCPoint v1, CCPoint v2) { return(ccp(v1.x * v2.x + v1.y * v2.y, v1.y * v2.x - v1.x * v2.y)); }
/** Calculates the projection of v1 over v2. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpProject(CCPoint v1, CCPoint v2) { return(ccpMult(v2, ccpDot(v1, v2) / ccpDot(v2, v2))); }
/** Returns point multiplied by given factor. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpMult(CCPoint v, float s) { return(ccp(v.x * s, v.y * s)); }
/** Clamp a point between from and to. * @since v0.99.1 */ public static CCPoint ccpClamp(CCPoint p, CCPoint from, CCPoint to) { return(ccp(clampf(p.x, from.x, to.x), clampf(p.y, from.y, to.y))); }
/** Calculates midpoint between two points. * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpMidpoint(CCPoint v1, CCPoint v2) { return(ccpMult(ccpAdd(v1, v2), 0.5f)); }
/// <summary> /// Returns opposite of point. /// since v0.7.2 /// </summary> /// <returns>CCPoint</returns> public static CCPoint ccpNeg(CCPoint v) { return(ccp(-v.x, -v.y)); }
/** Calculates cross product of two points. * @return CGFloat * @since v0.7.2 */ public static float ccpCross(CCPoint v1, CCPoint v2) { return(v1.x * v2.y - v1.y * v2.x); }
public static CCPoint ccpCompOp(CCPoint p, ccpCompOpDelegate del) { return(ccp(del(p.x), del(p.y))); }
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0 * @return CCPoint * @since v0.7.2 */ public static CCPoint ccpRPerp(CCPoint v) { return(ccp(v.y, -v.x)); }
private CCPoint convertToWindowSpace(CCPoint nodePoint) { CCPoint worldPoint = this.convertToWorldSpace(nodePoint); return(CCDirector.sharedDirector().convertToUI(worldPoint)); }