#include "Circle.h"



Circle::Circle(QObject *parent)
    :   QObject (parent)
    ,   _circleRadius(0)
    ,   _circleOrigin(QPointF(0,0))
{

}

Circle::Circle(double radius, QObject *parent)
    :   QObject (parent)
    ,   _circleRadius(radius >= 0 ? radius : 0)
    ,   _circleOrigin(QPointF(0,0))
{
}

Circle::Circle(double radius, QPointF origin, QObject *parent)
    :   QObject (parent)
    ,   _circleRadius(radius >= 0 ? radius : 0)
    ,   _circleOrigin(origin)
{
}

Circle::Circle(const Circle &other, QObject *parent)
    :   QObject (parent)
{
    *this = other;
}

Circle &Circle::operator=(const Circle &other)
{
    this->setRadius(other.radius());
    this->setOrigin(other.origin());

    return *this;
}

/*!
 * \fn void Circle::setRadius(double radius)
 * Sets the radius of the circle to \a radius
 */
void Circle::setRadius(double radius)
{
    if ( radius >= 0 && !qFuzzyCompare(_circleRadius, radius) ) {
        _circleRadius = radius;

        emit radiusChanged(_circleRadius);
    }
}

/*!
 * \fn void Circle::setOrigin(const QPointF &origin)
 * Sets the origin of the circle to \a origin
 *
 * \sa QPointF
 */
void Circle::setOrigin(const QPointF &origin)
{
    if (origin != _circleOrigin) {
        _circleOrigin = origin;

        emit originChanged(_circleOrigin);
    }
}

/*!
 * \fn double Circle::radius() const
 * Returns the radius of the circle.
 */
double Circle::radius() const
{
    return _circleRadius;
}

/*!
 * \fn QPointF Circle::origin() const
 * Returns the origin of the circle.
 *
 * \sa QPointF
 */
QPointF Circle::origin() const
{
    return _circleOrigin;
}


/*!
 * \fn QPolygonF Circle::approximate(int numberOfCorners)
 * Returns a polygon with \a numberOfCorners corners which approximates the circle.
 *
 * \sa QPointF
 */
QPolygonF Circle::approximate(int numberOfCorners) const
{
    if ( numberOfCorners < 3)
        return QPolygonF();
    return approximateSektor(numberOfCorners, 0, 2*M_PI);
}

QPolygonF Circle::approximate(double angleDiscretisation) const
{
    return approximateSektor(angleDiscretisation, 0, 2*M_PI);
}

QPolygonF Circle::approximateSektor(int numberOfCorners, double alpha1, double alpha2) const
{
    return approximateSektor((alpha2-alpha1)/double(numberOfCorners-1), alpha1, alpha2);
}

QPolygonF Circle::approximateSektor(double angleDiscretisation, double alpha1, double alpha2) const
{
    // truncate alpha1 to [0, 2*pi], fmod() does not work in this case
    alpha1              = PlanimetryCalculus::truncateAngle(alpha1);
    alpha2              = PlanimetryCalculus::truncateAngle(alpha2);
    double deltaAlpha   = PlanimetryCalculus::truncateAngle(alpha2 - alpha1);
    angleDiscretisation = PlanimetryCalculus::truncateAngle(angleDiscretisation);

    if (angleDiscretisation > deltaAlpha || qFuzzyIsNull(angleDiscretisation))
        return QPolygonF();


    QPolygonF polygon;
    QPointF vertex(-_circleRadius,0); // initial vertex
    double currentAngle = alpha1;
    // rotate the vertex numberOfCorners-1 times add the origin and append to the polygon.
    while(currentAngle < alpha2) {
        PlanimetryCalculus::rotateReference(vertex, currentAngle);
        polygon.append(vertex + _circleOrigin);
        currentAngle = PlanimetryCalculus::truncateAngle(currentAngle + angleDiscretisation);
    }

    // append last point if necessarry
    PlanimetryCalculus::rotateReference(vertex, alpha2);
    vertex = vertex + _circleOrigin;
    if (   !qFuzzyIsNull(PlanimetryCalculus::distance(polygon.first(), vertex))
        && !qFuzzyIsNull(PlanimetryCalculus::distance(polygon.last(), vertex )) ){
        polygon.append(vertex);
    }

    return polygon;
}

bool Circle::isNull() const
{
    return _circleRadius <= 0 ? true : false;
}

/*!
 * \class Circle
 * \brief Provies a circle with radius and origin.
 *
 * \sa QPointF
 */
