Example #1
0
void AngleTool::drawCircle()
{
    double *firstPoint = m_mainPolyline->getPoint(0);
    double *circleCentre = m_mainPolyline->getPoint(1);
    double *lastPoint = m_mainPolyline->getPoint(2);

    int xIndex, yIndex, zIndex;
    m_2DViewer->getView().getXYZIndexes(xIndex, yIndex, zIndex);
    QVector3D firstPointProjected(firstPoint[xIndex], firstPoint[yIndex], 0.0);
    QVector3D circleCentreProjected(circleCentre[xIndex], circleCentre[yIndex], 0.0);
    QVector3D lastPointProjected(lastPoint[xIndex], lastPoint[yIndex], 0.0);

    // Calculem l'angle que formen els dos segments
    QVector3D firstSegment = MathTools::directorVector(circleCentreProjected, firstPointProjected);
    QVector3D secondSegment = MathTools::directorVector(circleCentreProjected, lastPointProjected);
    m_currentAngle = MathTools::angleInDegrees(firstSegment, secondSegment);

    // Calculem el radi de l'arc de circumferència que mesurarà un quart del segment més curt dels dos que formen l'angle
    double distance1 = firstSegment.length();
    double distance2 = secondSegment.length();
    double radius = qMin(distance1, distance2) / 4.0;

    // Calculem el rang de les iteracions per pintar l'angle correctament
    double initialAngle = MathTools::angleInRadians(firstSegment.toVector2D());
    if (initialAngle < 0.0)
    {
        initialAngle += 2.0 * MathTools::PiNumber;
    }
    double finalAngle = MathTools::angleInRadians(secondSegment.toVector2D());
    if (finalAngle < 0.0)
    {
        finalAngle += 2.0 * MathTools::PiNumber;
    }
    // Tenim els dos angles al rang [0,2pi)
    // Assegurem que girem en sentit horari -> tindrem angle positiu
    if (finalAngle < initialAngle)
    {
        std::swap(initialAngle, finalAngle);
    }
    double angle = finalAngle - initialAngle;
    // Assegurem que tenim l'angle al rang [0, pi]
    if (angle > MathTools::PiNumber)
    {
        angle = 2.0 * MathTools::PiNumber - angle;
        std::swap(initialAngle, finalAngle);
    }
    int degrees = qRound(angle * MathTools::RadiansToDegreesAsDouble);
    double increment = angle / degrees;

    // Reconstruim l'arc de circumferència
    m_circlePolyline->deleteAllPoints();
    for (int i = 0; i <= degrees; i++)
    {
        angle = initialAngle + i * increment;
        double newPoint[3];
        newPoint[xIndex] = cos(angle) * radius + circleCentre[xIndex];
        newPoint[yIndex] = sin(angle) * radius + circleCentre[yIndex];
        newPoint[zIndex] = 0.0;
        m_circlePolyline->addPoint(newPoint);
    }

    m_circlePolyline->update();
}