]> git.sven.stormbind.net Git - sven/vym.git/blob - src/geometry.h
New upstream version 2.9.22
[sven/vym.git] / src / geometry.h
1 #ifndef GEOMETRY
2 #define GEOMETRY
3
4 #include <QPolygonF>
5
6 namespace Geometry {
7 qreal distance(const QPointF &p, const QPointF &q);
8 };
9
10 QRectF addBBox(QRectF r1, QRectF r2);
11 QSize addBBoxSize(QSize s1, QSize s2);
12 bool isInBox(const QPointF &p, const QRectF &box);
13
14 class Vector : public QPointF {
15   public:
16     Vector();
17     Vector(const QPointF &p);
18     Vector(qreal x, qreal y);
19     virtual ~Vector();
20
21     friend inline bool operator==(const Vector &v1, const Vector &v2)
22     {
23         return v1.x() == v2.x() && v1.y() == v2.y();
24     }
25
26     bool isNull();
27     virtual void normalize();
28     virtual qreal dotProduct(const QPointF &b);
29     virtual void scale(const qreal &f);
30     virtual void invert();
31     virtual QPointF toQPointF();
32 };
33
34 class ConvexPolygon : public QPolygonF {
35   public:
36     ConvexPolygon();
37     ConvexPolygon(QPolygonF p);
38     virtual ~ConvexPolygon();
39     void calcCentroid();
40     QPointF centroid() const;
41     qreal weight() const;
42     std::string toStdString();
43     Vector at(const int &i) const;
44     virtual void translate(const Vector &offset);
45     virtual void translate(qreal dx, qreal dy);
46
47   private:
48     Vector _centroid;
49     qreal _area;
50 };
51
52 class PolygonCollisionResult {
53   public:
54     // Are the polygons going to intersect forward in time?
55     bool willIntersect;
56
57     // Are the polygons currently intersecting?
58     bool intersect;
59
60     // The translation to apply to the first polygon to push the polygons apart.
61     QPointF minTranslation;
62 };
63
64 void projectPolygon(Vector axis, ConvexPolygon polygon, qreal &min, qreal &max);
65
66 qreal intervalDistance(qreal minA, qreal maxA, qreal minB, qreal maxB);
67 PolygonCollisionResult polygonCollision(ConvexPolygon polygonA,
68                                         ConvexPolygon polygonB,
69                                         Vector velocity);
70
71 #endif