Beispiel #1
0
void bezierSplit( const Bezier& b, Bezier* b0, Bezier* b1 ) {
	Point p01 = midLine( b.p0, b.p1 );
	Point p12 = midLine( b.p1, b.p2 );
	Point p23 = midLine( b.p2, b.p3 );
	Point p02 = midLine( p01, p12 );
	Point p13 = midLine( p12, p23 );
	Point p03 = midLine( p02, p13 );
	
	b0->set( b.p0, p01, p02, p03 );
	b1->set( p03, p13, p23, b.p3 );
}
vector<VertexObject*> DivideAndConquerFor3DCH::find2DConvexHull( list<VertexObject*> *pointSet )
{
    vector<VertexObject*> upper;
    vector<VertexObject*> lower;

    list<VertexObject*>::iterator pointIter;
    D3DXVECTOR2 midLine( pointSet->back()->v->x - pointSet->front()->v->x, pointSet->back()->v->y - pointSet->front()->v->y );

    for( pointIter = pointSet->begin(); pointIter != pointSet->end(); pointIter++ )
    {
        D3DXVECTOR2 currentLine( (*pointIter)->v->x - pointSet->front()->v->x, (*pointIter)->v->y - pointSet->front()->v->y );
        float crossProduct = midLine.x * currentLine.y - midLine.y * currentLine.x;

        if( crossProduct > 0 || (*pointIter) == pointSet->front() || (*pointIter) == pointSet->back() )
        {
            upper.push_back( (*pointIter) );
        }
    }

    list<VertexObject*>::reverse_iterator re_pointIter;
    midLine = D3DXVECTOR2( pointSet->front()->v->x - pointSet->back()->v->x, pointSet->front()->v->y - pointSet->back()->v->y );
    for( re_pointIter = pointSet->rbegin(); re_pointIter != pointSet->rend(); re_pointIter++ )
    {
        D3DXVECTOR2 currentLine( (*re_pointIter)->v->x - pointSet->back()->v->x, (*re_pointIter)->v->y - pointSet->back()->v->y );
        float crossProduct = midLine.x * currentLine.y - midLine.y * currentLine.x;

        if( crossProduct > 0 || (*re_pointIter) == pointSet->front() || (*re_pointIter) == pointSet->back() )
        {
            lower.push_back( (*re_pointIter) );
        }
    }

    vector<VertexObject*> upperChain;
    vector<VertexObject*> lowerChain;

    upperChain.push_back( upper[ 0 ] );
    upperChain.push_back( upper[ 1 ] );
    for( int i = 2; i < upper.size(); i++ )
    {
        for( ; upperChain.size() >= 2 ; )
        {
            int upperChainSize = upperChain.size();
            D3DXVECTOR2 currentLine( upper[ i ]->v->x - upperChain[ upperChainSize - 1 ]->v->x, upper[ i ]->v->y - upperChain[ upperChainSize - 1 ]->v->y );
            D3DXVECTOR2 preLine( upperChain[ upperChainSize - 1 ]->v->x - upperChain[ upperChainSize - 2 ]->v->x, upperChain[ upperChainSize - 1 ]->v->y - upperChain[ upperChainSize - 2 ]->v->y );

            float crossProduct = preLine.x * currentLine.y - preLine.y * currentLine.x;
            if( crossProduct > 0 )
            {
                upperChain.pop_back();
            }
            else
            {
                break;
            }
        }

        upperChain.push_back( upper[ i ] );
    }

    lowerChain.push_back( lower[ 0 ] );
    lowerChain.push_back( lower[ 1 ] );
    for( int i = 2; i < lower.size(); i++ )
    {
        for( ; lowerChain.size() >= 2 ; )
        {
            int lowerChainSize = lowerChain.size();
            D3DXVECTOR2 currentLine( lower[ i ]->v->x - lowerChain[ lowerChainSize - 1 ]->v->x, lower[ i ]->v->y - lowerChain[ lowerChainSize - 1 ]->v->y );
            D3DXVECTOR2 preLine( lowerChain[ lowerChainSize - 1 ]->v->x - lowerChain[ lowerChainSize - 2 ]->v->x, lowerChain[ lowerChainSize - 1 ]->v->y - lowerChain[ lowerChainSize - 2 ]->v->y );

            float crossProduct = preLine.x * currentLine.y - preLine.y * currentLine.x;
            if( crossProduct > 0 )
            {
                lowerChain.pop_back();
            }
            else
            {
                break;
            }
        }

        lowerChain.push_back( lower[ i ] );
    }

    upperChain.pop_back();
    lowerChain.pop_back();

    for( int i = 0; i < lowerChain.size(); i++ )
    {
        upperChain.push_back( lowerChain[ i ] );
    }

    return upperChain;
}