155 lines
5.1 KiB
C++
155 lines
5.1 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#include "Cevo/Geometric.h"
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace Cevo {
|
|
|
|
int distance(QPoint p1, QPoint p2)
|
|
{
|
|
const QPoint d = p2 - p1;
|
|
return static_cast<int>(std::sqrt(static_cast<double>(d.x() * d.x() + d.y() * d.y())));
|
|
}
|
|
|
|
double dot(QPoint p1, QPoint p2)
|
|
{
|
|
return static_cast<double>(p1.x()) * p2.x() + static_cast<double>(p1.y()) * p2.y();
|
|
}
|
|
|
|
int pointToLineDistance(QPoint p, QPoint v, QPoint w, QPoint &intersect)
|
|
{
|
|
// l2 is computed via the truncating integer distance() first (matches
|
|
// the original's own precision loss - Distance(V, W) then squared).
|
|
double l2 = distance(v, w);
|
|
l2 = l2 * l2;
|
|
if (l2 == 0) {
|
|
intersect = v;
|
|
return distance(p, v); // v == w case
|
|
}
|
|
const double t = dot(p - v, w - v) / l2;
|
|
if (t < 0) {
|
|
intersect = v; // beyond the 'v' end of the segment
|
|
return distance(p, v);
|
|
}
|
|
if (t > 1) {
|
|
intersect = w; // beyond the 'w' end of the segment
|
|
return distance(p, w);
|
|
}
|
|
const QPoint tt(static_cast<int>(v.x() + t * (w.x() - v.x())), static_cast<int>(v.y() + t * (w.y() - v.y())));
|
|
intersect = tt;
|
|
return distance(p, tt);
|
|
}
|
|
|
|
QPoint rotatePoint(QPoint center, QPoint p, double angle)
|
|
{
|
|
p -= center;
|
|
return QPoint(center.x() + qRound(p.x() * std::cos(angle) - p.y() * std::sin(angle)),
|
|
center.y() + qRound(p.x() * std::sin(angle) + p.y() * std::cos(angle)));
|
|
}
|
|
|
|
QVector<QPoint> rotatePoints(QPoint center, const QVector<QPoint> &points, double angle)
|
|
{
|
|
QVector<QPoint> result;
|
|
result.reserve(points.size());
|
|
for (const QPoint &p : points)
|
|
result.append(rotatePoint(center, p, angle));
|
|
return result;
|
|
}
|
|
|
|
bool lineIntersect(QPoint lineAP1, QPoint lineAP2, QPoint lineBP1, QPoint lineBP2, QPoint &intersection)
|
|
{
|
|
if (lineAP1 == lineAP2 || lineBP1 == lineBP2)
|
|
return false;
|
|
|
|
const double detLineA = static_cast<double>(lineAP1.x()) * lineAP2.y() - static_cast<double>(lineAP1.y()) * lineAP2.x();
|
|
const double detLineB = static_cast<double>(lineBP1.x()) * lineBP2.y() - static_cast<double>(lineBP1.y()) * lineBP2.x();
|
|
|
|
const QPoint diffLA = lineAP1 - lineAP2;
|
|
const QPoint diffLB = lineBP1 - lineBP2;
|
|
|
|
const double d = static_cast<double>(diffLA.x()) * diffLB.y() - static_cast<double>(diffLA.y()) * diffLB.x();
|
|
if (d == 0)
|
|
return false; // parallel lines without intersection
|
|
const double detDivInv = 1 / d;
|
|
|
|
intersection.setX(static_cast<int>((detLineA * diffLB.x() - diffLA.x() * detLineB) * detDivInv));
|
|
intersection.setY(static_cast<int>((detLineA * diffLB.y() - diffLA.y() * detLineB) * detDivInv));
|
|
return true;
|
|
}
|
|
|
|
double arcTan2Point(QPoint point)
|
|
{
|
|
return std::atan2(static_cast<double>(point.y()), static_cast<double>(point.x()));
|
|
}
|
|
|
|
double arcTanPoint(QPoint point)
|
|
{
|
|
if (point.y() == 0)
|
|
return std::numeric_limits<double>::infinity();
|
|
return std::atan(static_cast<double>(point.x()) / point.y());
|
|
}
|
|
|
|
QRect rectEnlarge(QRect rect, int value)
|
|
{
|
|
return QRect(QPoint(rect.left() - value, rect.top() - value), QPoint(rect.right() + value, rect.bottom() + value));
|
|
}
|
|
|
|
QRect shiftRect(QRect rect, QPoint delta)
|
|
{
|
|
return rect.translated(delta);
|
|
}
|
|
|
|
double Vector::length() const
|
|
{
|
|
return std::sqrt(static_cast<double>(direction.x() * direction.x() + direction.y() * direction.y()));
|
|
}
|
|
|
|
double Vector::angle() const
|
|
{
|
|
return arcTan2Point(direction);
|
|
}
|
|
|
|
void Vector::setLength(double value)
|
|
{
|
|
const double a = angle();
|
|
direction = QPoint(qRound(std::cos(a) * value), qRound(std::sin(a) * value));
|
|
}
|
|
|
|
Vector Vector::create(QPoint p1, QPoint p2)
|
|
{
|
|
Vector v;
|
|
v.position = p1;
|
|
v.direction = p2 - p1;
|
|
return v;
|
|
}
|
|
|
|
} // namespace Cevo
|