bool ccpSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) { float S, T; if( ccpLineIntersect(A, B, C, D, &S, &T ) && (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f) ) return true; return false; }
CCPoint ccpIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) { float S, T; if( ccpLineIntersect(A, B, C, D, &S, &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 CCPointZero; }
/// // Update does the work of mapping the texture onto the triangles // It now doesn't occur the cost of free/alloc data every update cycle. // It also only changes the percentage point but no other points if they have not // been modified. // // It now deals with flipped texture. If you run into this problem, just use the // sprite property and enable the methods flipX, flipY. /// void CCProgressTimer::updateRadial() { if (!m_pSprite) { return; } float alpha = m_fPercentage / 100.f; float angle = 2.f*((float)M_PI) * ( m_bReverseDirection ? alpha : 1.0f - alpha); // We find the vector to do a hit detection based on the percentage // We know the first vector is the one @ 12 o'clock (top,mid) so we rotate // from that by the progress angle around the m_tMidpoint pivot CCPoint topMid = ccp(m_tMidpoint.x, 1.f); CCPoint percentagePt = ccpRotateByAngle(topMid, m_tMidpoint, angle); int index = 0; CCPoint hit = CCPoint::zero; if (alpha == 0.f) { // More efficient since we don't always need to check intersection // If the alpha is zero then the hit point is top mid and the index is 0. hit = topMid; index = 0; } else if (alpha == 1.f) { // More efficient since we don't always need to check intersection // If the alpha is one then the hit point is top mid and the index is 4. hit = topMid; index = 4; } else { // We run a for loop checking the edges of the texture to find the // intersection point // We loop through five points since the top is split in half float min_t = FLT_MAX; for (int i = 0; i <= kProgressTextureCoordsCount; ++i) { int pIndex = (i + (kProgressTextureCoordsCount - 1))%kProgressTextureCoordsCount; CCPoint edgePtA = boundaryTexCoord(i % kProgressTextureCoordsCount); CCPoint edgePtB = boundaryTexCoord(pIndex); // Remember that the top edge is split in half for the 12 o'clock position // Let's deal with that here by finding the correct endpoints if(i == 0){ edgePtB = ccpLerp(edgePtA, edgePtB, 1-m_tMidpoint.x); } else if(i == 4){ edgePtA = ccpLerp(edgePtA, edgePtB, 1-m_tMidpoint.x); } // s and t are returned by ccpLineIntersect float s = 0, t = 0; if(ccpLineIntersect(edgePtA, edgePtB, m_tMidpoint, percentagePt, &s, &t)) { // Since our hit test is on rays we have to deal with the top edge // being in split in half so we have to test as a segment if ((i == 0 || i == 4)) { // s represents the point between edgePtA--edgePtB if (!(0.f <= s && s <= 1.f)) { continue; } } // As long as our t isn't negative we are at least finding a // correct hitpoint from m_tMidpoint to percentagePt. if (t >= 0.f) { // Because the percentage line and all the texture edges are // rays we should only account for the shortest intersection if (t < min_t) { min_t = t; index = i; } } } } // Now that we have the minimum magnitude we can use that to find our intersection hit = ccpAdd(m_tMidpoint, ccpMult(ccpSub(percentagePt, m_tMidpoint),min_t)); } // The size of the vertex data is the index from the hitpoint // the 3 is for the m_tMidpoint, 12 o'clock point and hitpoint position. bool sameIndexCount = true; if(m_nVertexDataCount != index + 3){ sameIndexCount = false; CC_SAFE_FREE(m_pVertexData); m_nVertexDataCount = 0; } if(!m_pVertexData) { m_nVertexDataCount = index + 3; m_pVertexData = (ccV2F_C4B_T2F*)malloc(m_nVertexDataCount * sizeof(ccV2F_C4B_T2F)); CCAssert( m_pVertexData, "CCProgressTimer. Not enough memory"); } updateColor(); if (!sameIndexCount) { // First we populate the array with the m_tMidpoint, then all // vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint m_pVertexData[0].texCoords = textureCoordFromAlphaPoint(m_tMidpoint); m_pVertexData[0].vertices = vertexFromAlphaPoint(m_tMidpoint); m_pVertexData[1].texCoords = textureCoordFromAlphaPoint(topMid); m_pVertexData[1].vertices = vertexFromAlphaPoint(topMid); for(int i = 0; i < index; ++i){ CCPoint alphaPoint = boundaryTexCoord(i); m_pVertexData[i+2].texCoords = textureCoordFromAlphaPoint(alphaPoint); m_pVertexData[i+2].vertices = vertexFromAlphaPoint(alphaPoint); } } // hitpoint will go last m_pVertexData[m_nVertexDataCount - 1].texCoords = textureCoordFromAlphaPoint(hit); m_pVertexData[m_nVertexDataCount - 1].vertices = vertexFromAlphaPoint(hit); }
bool Bug1174Layer::init() { if (BugsTestBaseLayer::init()) { // // seed // srand(0); Point A,B,C,D,p1,p2,p3,p4; float s,t; int err=0; int ok=0; // // Test 1. // CCLog("Test1 - Start"); for( int i=0; i < 10000; i++) { // A | b // ----- // c | d float ax = CCRANDOM_0_1() * -5000; float ay = CCRANDOM_0_1() * 5000; // a | b // ----- // c | D float dx = CCRANDOM_0_1() * 5000; float dy = CCRANDOM_0_1() * -5000; // a | B // ----- // c | d float bx = CCRANDOM_0_1() * 5000; float by = CCRANDOM_0_1() * 5000; // a | b // ----- // C | d float cx = CCRANDOM_0_1() * -5000; float cy = CCRANDOM_0_1() * -5000; A = ccp(ax,ay); B = ccp(bx,by); C = ccp(cx,cy); D = ccp(dx,dy); if( ccpLineIntersect( A, D, B, C, &s, &t) ) { if( check_for_error(A, D, B, C, s, t) ) err++; else ok++; } } CCLog("Test1 - End. OK=%i, Err=%i", ok, err); // // Test 2. // CCLog("Test2 - Start"); p1 = ccp(220,480); p2 = ccp(304,325); p3 = ccp(264,416); p4 = ccp(186,416); s = 0.0f; t = 0.0f; if( ccpLineIntersect(p1, p2, p3, p4, &s, &t) ) check_for_error(p1, p2, p3, p4, s,t ); CCLog("Test2 - End"); // // Test 3 // CCLog("Test3 - Start"); ok=0; err=0; for( int i=0;i<10000;i++) { // A | b // ----- // c | d float ax = CCRANDOM_0_1() * -500; float ay = CCRANDOM_0_1() * 500; p1 = ccp(ax,ay); // a | b // ----- // c | D float dx = CCRANDOM_0_1() * 500; float dy = CCRANDOM_0_1() * -500; p2 = ccp(dx,dy); ////// float y = ay - ((ay - dy) /2.0f); // a | b // ----- // C | d float cx = CCRANDOM_0_1() * -500; p3 = ccp(cx,y); // a | B // ----- // c | d float bx = CCRANDOM_0_1() * 500; p4 = ccp(bx,y); s = 0.0f; t = 0.0f; if( ccpLineIntersect(p1, p2, p3, p4, &s, &t) ) { if( check_for_error(p1, p2, p3, p4, s,t ) ) err++; else ok++; } } CCLog("Test3 - End. OK=%i, err=%i", ok, err); return true; } return false; }