Пример #1
0
qreal BezierCurve::findDistance(BezierCurve curve, int i, QPointF P, QPointF& nearestPoint, qreal& t)   //finds the distance between a cubic section and a point
{
    //qDebug() << "---- INTER CUBIC SEGMENT";
    int nSteps = 24;
    int k0 = 0;
    QPointF Q;
    Q = curve.getVertex(i-1);
    qreal distMin = eLength(Q-P);
    nearestPoint = Q;
    t = 0;
    for(int k=1; k<=nSteps; k++)
    {
        qreal s = (k+0.0)/nSteps;
        Q = curve.getPointOnCubic(i, s);
        qreal dist = eLength(Q-P);
        if (dist <= distMin)
        {
            distMin = dist;
            nearestPoint = Q;
            t = s;
            k0 = k;
        }
    }
    //QPointF Q1 = curve.getPointOnCubic(i, t);
    return distMin;
}
Пример #2
0
bool BezierCurve::findIntersection(BezierCurve curve1, int i1, BezierCurve curve2, int i2, QList<Intersection>& intersections)   //finds the intersection between two cubic sections
{
    bool result = false;
    //qDebug() << "---- INTER CUBIC CUBIC"  << i1 << i2;
    QPointF P1, Q1, P2, Q2;
    QLineF L1, L2;
    QRectF R1;
    QRectF R2;

    P1 = curve1.getVertex(i1-1);
    Q1 = curve1.getVertex(i1);
    P2 = curve2.getVertex(i2-1);
    Q2 = curve2.getVertex(i2);
    L1 = QLineF(P1, Q1);
    L2 = QLineF(P2, Q2);

    //qDebug() << "-------------------- ";

    R1.setTopLeft(P1);
    R1.setBottomRight(Q1);
    R2.setTopLeft(P2);
    R2.setBottomRight(Q2);

    QPointF intersectionPoint = QPointF(50.0, 50.0); // bogus point
    QPointF* cubicIntersection = &intersectionPoint;
    if ( R1.intersects(R2) || L2.intersect(L1, cubicIntersection) == QLineF::BoundedIntersection )
    {
        //if (L2.intersect(L1, intersection) == QLineF::BoundedIntersection) {
        //qDebug() << "                   FOUND rectangle intersection ";
        //if (intersectionPoint != curve1.getVertex(i1-1) && intersectionPoint != curve1.getVertex(i1)) {
        //	qDebug() << "                   it's not one of the points ";
        // find the cubic intersection
        int nSteps = 24;
        P1 = curve1.getVertex(i1-1);
        for(int i=1; i<=nSteps; i++)
        {
            qreal s = (i+0.0)/nSteps;
            Q1 = curve1.getPointOnCubic(i1, s);
            P2 = curve2.getVertex(i2-1);
            for(int j=1; j<=nSteps; j++)
            {
                qreal t = (j+0.0)/nSteps;
                Q2 = curve2.getPointOnCubic(i2, t);
                L1 = QLineF(P1, Q1);
                L2 = QLineF(P2, Q2);
                if (L2.intersect(L1, cubicIntersection) == QLineF::BoundedIntersection)
                {
                    QPointF intersectionPoint = *cubicIntersection;
                    if (intersectionPoint != curve1.getVertex(i1-1) && intersectionPoint != curve1.getVertex(i1))
                    {
                        qreal fraction1 = eLength(intersectionPoint-Q1)/(0.0+eLength(Q1-P1));
                        qreal fraction2 = eLength(intersectionPoint-Q2)/(0.0+eLength(Q2-P2));
                        qreal t1 = (i - fraction1)/nSteps;
                        qreal t2 = (j - fraction2)/nSteps;
                        Intersection intersection;
                        intersection.point = intersectionPoint;
                        intersection.t1 = t1;
                        intersection.t2 = t2;
                        intersections.append( intersection );
                        result = true;
                        //qDebug() << "FOUND cubic interesection " << intersectionPoint << i << j;
                    }
                }
                P2 = Q2;
            }
            P1 = Q1;
        }
    }
    else
    {
        //return false; // approximation to speed up the calculation
    }
    //qDebug() << "------";
    return result;
}