initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,126 @@
|
||||
#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
|
||||
Reference in new issue
Block a user