#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 QList<QPointF> Circle::approximate(int numberOfCorners)
 * Returns a polygon with \a numberOfCorners corners which approximates the circle.
 *
 * \sa QPointF
 */
QList<QPointF> Circle::approximate(int numberOfCorners) const
{
    if ( numberOfCorners < 3)
        return QList<QPointF>();
    return approximateSektor(numberOfCorners+1, 0, 2*M_PI);
}

QList<QPointF> Circle::approximate(double angleDiscretisation) const
{
    return approximateSektor(angleDiscretisation, 0, 2*M_PI);
}

QList<QPointF> Circle::approximateSektor(int numberOfCorners, double alpha1, double alpha2) const
{
    if ( numberOfCorners < 2) {
        qWarning("numberOfCorners < 2");
        return QList<QPointF>();
    }
    return approximateSektor(PlanimetryCalculus::truncateAngle(alpha2-alpha1)/double(numberOfCorners-1), alpha1, alpha2);
}

QList<QPointF> Circle::approximateSektor(double angleDiscretisation, double alpha1, double alpha2) const
{
    using namespace PlanimetryCalculus;
    // check if input is valid
    if ( qFuzzyCompare(alpha1, alpha2) )
        return QList<QPointF>();

    alpha1 = truncateAngle(alpha1);
    alpha2 = truncateAngle(alpha2);
    double deltaAlpha = fabs(alpha1 - alpha2);
    if (signum(angleDiscretisation*(alpha1 - alpha2)) == 1) {
        deltaAlpha = 2*M_PI - deltaAlpha;
    }

    // check if input is valid
    if (  qFuzzyIsNull(angleDiscretisation))
        return QList<QPointF>();


    QList<QPointF> sector;
    double currentAngle = alpha1;
    // how many nodes?
    int j = floor(fabs(deltaAlpha/angleDiscretisation));
    // rotate the vertex j+1 times add the origin and append to the sector.
    do {
        sector.append(toCoordinate(currentAngle));
        currentAngle = truncateAngle(currentAngle + angleDiscretisation);
    }while(j--);

    // append last point if necessarry
    QPointF vertex = toCoordinate(alpha2);
    if (   !qFuzzyIsNull(PlanimetryCalculus::distance(sector.first(), vertex))
        && !qFuzzyIsNull(PlanimetryCalculus::distance(sector.last(), vertex )) ){
        sector.append(vertex);
    }

    return sector;
}

/*!
 * \fn void Circle::toCoordinate(QPointF &coordinate, double alpha) const
 * Calculates the coordinates of a point on the circle with angle \a alpha.
 * Stores the result in \a coordiante.
 *
 * \sa QPointF
 */
void Circle::toCoordinate(QPointF &coordinate, double alpha) const
{
    using namespace PlanimetryCalculus;
    coordinate = QPointF(_circleRadius,0);
    rotateReference(coordinate, alpha);
    coordinate += _circleOrigin;
}

/*!
 * \overload QPointF Circle::toCoordinate(double alpha) const
 * Returns the coordinates of a point on the circle with angle \a alpha.
 *
 * \sa QPointF
 */
QPointF Circle::toCoordinate(double alpha) const
{
    QPointF coordinate;
    toCoordinate(coordinate, alpha);
    return coordinate;
}

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

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