system
The vocabulary types everything else shares
Loading...
Searching...
No Matches
Shape.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "Vector2.hpp"
14#include "Constants.hpp"
15
16// Rectangle
17//
18// (x, y) is the TOP-LEFT corner, w/h the size.
19//
20// This used to be documented as the centre, and every use in the codebase
21// contradicted it. ISprite::setCrop() and ISprite::getBounds() are the only
22// two places a Rect actually travels through, and all four vendors read a
23// corner : sfml hands x/y straight to sf::IntRect's position, raylib to a
24// raylib Rectangle, both SDL to an SDL_Rect. getBounds() likewise returns
25// the sprite's position, which is its corner.
26//
27// So the code was right and the comment was wrong. Fixing the comment
28// rather than the four vendors is the only choice that changes no
29// behaviour - and centre semantics would have needed a decision the
30// drawing APIs never offered in the first place.
31template <typename T>
32struct Rect {
33 T x, y, w, h;
34
35 // Derived, not stored : the corner is what a drawing API asks for, the
36 // centre is what a hit test asks for, and only one of them can be the
37 // field without lying to the other.
38 Vector2<T> center() const { return {x + w / T(2), y + h / T(2)}; }
39 Vector2<T> half() const { return {w / T(2), h / T(2)}; }
40
41 Vector2<T> corner() const { return {x, y}; }
42 Vector2<T> size() const { return {w, h}; }
43
44 // AABB overlap with optional margin. Corner semantics, like the fields.
46 bool intersects(const Rect& o, T margin = T(0)) const {
47 return !(x + w + margin < o.x ||
48 x - margin > o.x + o.w ||
49 y + h + margin < o.y ||
50 y - margin > o.y + o.h);
51 }
52};
53
57
58// Line
59template <typename T>
60struct Line {
62
64 bool operator==(const Line& other) const {
65 // Edges are undirected: (p1,p2) == (p2,p1)
66 return (p1 == other.p1 && p2 == other.p2) ||
67 (p1 == other.p2 && p2 == other.p1);
68 }
69
70 bool same(const Line& other) const {
71 // Edges are undirected: (p1,p2) == (p2,p1)
72 return (p1.same(other.p1) && p2.same(other.p2)) ||
73 (p1.same(other.p2) && p2.same(other.p1));
74 }
75};
76
80
81// Triangle
82template <typename T>
83struct Triangle {
85
88 //p become the origin (0,0)
89 Vector2<T> a = {p1.x - p.x, p1.y - p.y};
90 Vector2<T> b = {p2.x - p.x, p2.y - p.y};
91 Vector2<T> c = {p3.x - p.x, p3.y - p.y};
92
93 //geometric condition (squared distances from P to each vertex)
94 T a2 = a.square_magnitude(); // ||p1'||^2
95 T b2 = b.square_magnitude(); // ||p2'||^2
96 T c2 = c.square_magnitude(); // ||p3'||^2
97
98 T term1 = a.x * (b.y * c2 - c.y * b2); // contribution de la coordonnée x de A
99 T term2 = - a.y * (b.x * c2 - c.x * b2); // contribution de la coordonnée y de A
100 T term3 = a2 * b.cross(c); // contribution de la distance au carré de A (z) combinée à B et C // B' × C'
101 T det = term1 + term2 + term3; // La somme (det) nous dit si P est au-dessus ou en dessous du plan → donc à l’intérieur ou à l’extérieur du cercle.
102
103 return (det < Epsilon<T>::value);
104 }
105
106
108 const double abc = 0.5 * abs(p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y));
109
110 const double abp = 0.5 * abs(p1.x * (p2.y - P.y) + p2.x * (P.y - p1.y) + P.x * (p1.y - p2.y));
111 const double bcp = 0.5 * abs(p2.x * (p3.y - P.y) + p3.x * (P.y - p2.y) + P.x * (p2.y - p3.y));
112 const double cap = 0.5 * abs(p3.x * (p1.y - P.y) + p1.x * (P.y - p3.y) + P.x * (p3.y - p1.y));
113
114 //if sum of abp, bcp, cap is equal to abc, then the point is inside the triangle, otherwise it's outside.
115 return std::abs(abc - (abp + bcp + cap)) < Epsilon<T>::value;
116 }
117
118 bool operator==(const Triangle<T>& other) const {
119 // Edges are undirected: (A,B,C) == any permutation of (A,B,C)
120 return ((p1 == other.p1) && (p2 == other.p2) && (p3 == other.p3)) ||
121 ((p1 == other.p1) && (p2 == other.p3) && (p3 == other.p2)) ||
122 ((p1 == other.p2) && (p2 == other.p1) && (p3 == other.p3)) ||
123 ((p1 == other.p2) && (p2 == other.p3) && (p3 == other.p1)) ||
124 ((p1 == other.p3) && (p2 == other.p1) && (p3 == other.p2)) ||
125 ((p1 == other.p3) && (p2 == other.p2) && (p3 == other.p1));
126 }
127
128 bool same(const Triangle<T>& other) const {
129
130 // Edges are undirected: (A,B) == (B,A)
131 return (p1.same(other.p1) && p2.same(other.p2) && p3.same(other.p3)) ||
132 (p1.same(other.p1) && p2.same(other.p3) && p3.same(other.p2)) ||
133 (p1.same(other.p2) && p2.same(other.p1) && p3.same(other.p3)) ||
134 (p1.same(other.p2) && p2.same(other.p3) && p3.same(other.p1)) ||
135 (p1.same(other.p3) && p2.same(other.p1) && p3.same(other.p2)) ||
136 (p1.same(other.p3) && p2.same(other.p2) && p3.same(other.p1));
137 }
138
139};
140
constexpr T epsilon_v
Definition Constants.hpp:32
Definition Constants.hpp:14
Definition Shape.hpp:60
Vector2< T > p1
Definition Shape.hpp:61
bool operator==(const Line &other) const
Definition Shape.hpp:64
bool same(const Line &other) const
Definition Shape.hpp:70
Vector2< T > p2
Definition Shape.hpp:61
Definition Shape.hpp:32
Vector2< T > size() const
Definition Shape.hpp:42
Vector2< T > half() const
Definition Shape.hpp:39
T w
Definition Shape.hpp:33
T h
Definition Shape.hpp:33
T y
Definition Shape.hpp:33
T x
Definition Shape.hpp:33
bool intersects(const Rect &o, T margin=T(0)) const
Definition Shape.hpp:46
Vector2< T > corner() const
Definition Shape.hpp:41
Vector2< T > center() const
Definition Shape.hpp:38
Definition Shape.hpp:83
bool isInsideCircumcircle(const Vector2< T > &p)
Definition Shape.hpp:87
Vector2< T > p3
Definition Shape.hpp:84
bool same(const Triangle< T > &other) const
Definition Shape.hpp:128
bool isInside(Vector2< T > P)
Definition Shape.hpp:107
Vector2< T > p2
Definition Shape.hpp:84
Vector2< T > p1
Definition Shape.hpp:84
bool operator==(const Triangle< T > &other) const
Definition Shape.hpp:118
Definition Vector2.hpp:20
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21
T cross(const Vector2 &other) const
Definition Vector2.hpp:65
T square_magnitude() const
Definition Vector2.hpp:68