X-Git-Url: https://git.sven.stormbind.net/?a=blobdiff_plain;f=src%2Fgeometry.h;fp=src%2Fgeometry.h;h=4118ca4dabfee7dc61cdfd5ea48a2a6c444af279;hb=d483bd8e6523c23c6f1d8908a2e0611c2bc9ff4f;hp=0000000000000000000000000000000000000000;hpb=7dfa3fe589d1722d49681f42cdb0bf1e6efb5223;p=sven%2Fvym.git diff --git a/src/geometry.h b/src/geometry.h new file mode 100644 index 0000000..4118ca4 --- /dev/null +++ b/src/geometry.h @@ -0,0 +1,71 @@ +#ifndef GEOMETRY +#define GEOMETRY + +#include + +namespace Geometry { +qreal distance(const QPointF &p, const QPointF &q); +}; + +QRectF addBBox(QRectF r1, QRectF r2); +QSize addBBoxSize(QSize s1, QSize s2); +bool isInBox(const QPointF &p, const QRectF &box); + +class Vector : public QPointF { + public: + Vector(); + Vector(const QPointF &p); + Vector(qreal x, qreal y); + virtual ~Vector(); + + friend inline bool operator==(const Vector &v1, const Vector &v2) + { + return v1.x() == v2.x() && v1.y() == v2.y(); + } + + bool isNull(); + virtual void normalize(); + virtual qreal dotProduct(const QPointF &b); + virtual void scale(const qreal &f); + virtual void invert(); + virtual QPointF toQPointF(); +}; + +class ConvexPolygon : public QPolygonF { + public: + ConvexPolygon(); + ConvexPolygon(QPolygonF p); + virtual ~ConvexPolygon(); + void calcCentroid(); + QPointF centroid() const; + qreal weight() const; + std::string toStdString(); + Vector at(const int &i) const; + virtual void translate(const Vector &offset); + virtual void translate(qreal dx, qreal dy); + + private: + Vector _centroid; + qreal _area; +}; + +class PolygonCollisionResult { + public: + // Are the polygons going to intersect forward in time? + bool willIntersect; + + // Are the polygons currently intersecting? + bool intersect; + + // The translation to apply to the first polygon to push the polygons apart. + QPointF minTranslation; +}; + +void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max); + +qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB); +PolygonCollisionResult polygonCollision(ConvexPolygon polygonA, + ConvexPolygon polygonB, + Vector velocity); + +#endif \ No newline at end of file