34 #ifndef __ENKI_GEOMETRY_H 35 #define __ENKI_GEOMETRY_H 38 #define _USE_MATH_DEFINES 51 #define round(x) floor((x) + 0.5) 78 Vector(
double v) { this->x = v; this->y = v; }
80 Vector(
double x,
double y) { this->x =
x; this->y =
y; }
82 Vector(
double array[2]) { x = array[0]; y = array[1]; }
106 double norm(
void)
const {
return sqrt(x*x + y*y); }
108 double norm2(
void)
const {
return x*x+y*
y; }
114 double angle(
void)
const {
return atan2(y, x); }
124 bool operator <(
const Vector& that)
const {
if (this->x == that.
x)
return (this->y < that.
y);
else return (this->x < that.
x); }
157 Matrix22(
double _11,
double _21,
double _12,
double _22) { this->_11 = _11; this->_21 = _21; this->_12 = _12; this->_22 = _22; }
159 Matrix22(
double alpha) { _11 = cos(alpha); _21 = sin(alpha); _12 = -_21; _22 = _11; }
161 Matrix22(
double array[4]) { _11=array[0]; _21=array[1]; _12=array[2]; _22=array[3]; }
164 void zeros() { _11 = _21 = _12 = _22 = 0; }
171 void operator *=(
double f) { _11 *= f; _21 *= f; _12 *= f; _22 *= f; }
173 void operator /=(
double f) { _11 /= f; _21 /= f; _12 /= f; _22 /= f; }
186 Point
operator*(
const Point &v)
const { Point n; n.
x = v.
x*_11 + v.
y*_12; n.
y = v.
x*_21 + v.
y*_22;
return n; }
199 Segment(
double ax,
double ay,
double bx,
double by) { this->a.x = ax; this->a.y = ay; this->b.x = bx; this->b.y = by; }
201 Segment(
double array[4]) { a.x = array[0]; a.y = array[1]; b.x = array[2]; b.y = array[3]; }
203 Segment(
const Point &p1,
const Point &p2) { a = p1; b = p2; }
211 double dist(
const Point &p)
const 222 const double s2da = dist (o.
a);
223 const double s2db = dist (o.
b);
224 const double s1da = o.
dist (a);
225 const double s1db = o.
dist (b);
226 return (s2da*s2db<0) && (s1da*s1db<0);
243 return Segment((*
this)[i], (*
this)[(i + 1) % size()]);
249 for (
size_t i = 0; i < size(); i++)
251 if (getSegment(i).dist(p) < 0)
263 bottomLeft = (*this)[0];
264 topRight = (*this)[0];
266 extendAxisAlignedBoundingBox(bottomLeft, topRight);
274 for (const_iterator it = begin(); it != end(); ++it)
276 const Point& p = *it;
278 if (p.
x < bottomLeft.
x)
280 else if (p.
x > topRight.
x)
283 if (p.
y < bottomLeft.
y)
285 else if (p.
y > topRight.
y)
294 for (
size_t i = 0; i < size(); i++)
295 radius = std::max<double>(radius, (*
this)[i].norm());
302 for (iterator it = begin(); it != end(); ++it)
316 for (iterator it = begin(); it != end(); ++it)
323 for (
size_t i = 0; i < size(); i++)
324 (*
this)[i].x = -(*this)[i].x;
325 for (
size_t i = 0; i < size() / 2; i++)
327 Point p = (*this)[i];
328 (*this)[i] = (*this)[size() - i - 1];
329 (*this)[size() - i - 1] = p;
336 for (
size_t i = 0; i < size(); i++)
337 (*
this)[i].y = -(*this)[i].y;
338 for (
size_t i = 0; i < size() / 2; i++)
340 Point p = (*this)[i];
341 (*this)[i] = (*this)[size() - i - 1];
342 (*this)[size() - i - 1] = p;
360 while (angle < -M_PI)
368 inline double getTriangleArea(
const Point &a,
const Point &b,
const Point &c)
370 return (a.
x-c.
x) * (b.
y-c.
y) - (a.
y-c.
y) * (b.
x-c.
x);
381 const double c1 = s1.
a.
y + (-s1.
a.
x / (s1.
b.
x - s1.
a.
x)) * (s1.
b.
y - s1.
a.
y);
382 const double m1 = (s1.
b.
y - s1.
a.
y) / (s1.
b.
x - s1.
a.
x);
385 const double c2 = s2.
a.
y + (-s2.
a.
x / (s2.
b.
x - s2.
a.
x)) * (s2.
b.
y - s2.
a.
y);
386 const double m2 = (s2.
b.
y - s2.
a.
y) / (s2.
b.
x - s2.
a.
x);
390 return Point(HUGE_VAL, HUGE_VAL);
442 if (x > x3 && x < x4 && y > y1 && y <y2)
445 return Point(HUGE_VAL, HUGE_VAL);
453 if (x > x1 && x < x2 && y > y3 && y < y4)
456 return Point(HUGE_VAL, HUGE_VAL);
460 x = (c2 - c1) / (m1 - m2);
463 if (x > x1 && x < x2 && x > x3 && x < x4)
464 return Point(x, m1 * x + c1);
466 return Point(HUGE_VAL, HUGE_VAL);
477 return (a.
x - c.
x) * (b.
y - c.
y) - (a.
y - c.
y) * (b.
x - c.
x);
487 const double ba_norm = (b-a).
norm();
488 if (ba_norm < std::numeric_limits<double>::epsilon())
A vector in a 2D space.
Definition: Geometry.h:68
Point getMiddlePoint() const
Return the middle point.
Definition: Geometry.h:230
A 2x2 matrix.
Definition: Geometry.h:142
Matrix22(double array[4])
Constructor, create matrix with array[0] array[1] array[2] array[3].
Definition: Geometry.h:161
Vector Point
A point in a 2D space, another name for a vector.
Definition: Geometry.h:132
double _11
11 components
Definition: Geometry.h:146
static Matrix22 identity()
Create an identity matrix.
Definition: Geometry.h:191
double angle(void) const
Return the angle with the horizontal (arc tangant (y/x))
Definition: Geometry.h:114
Matrix22 transpose() const
Return the transpose of the matrix.
Definition: Geometry.h:183
std::ostream & operator<<(std::ostream &outs, const Vector &vector)
Print a vector to a stream.
Definition: Geometry.cpp:38
bool operator<(const Vector &that) const
Comparison operator.
Definition: Geometry.h:124
double y
y component
Definition: Geometry.h:73
static Matrix22 fromDiag(double _1, double _2)
Creates a diagonal matrix.
Definition: Geometry.h:189
void operator-=(const Vector &v)
Substract vector v component by component.
Definition: Geometry.h:87
Vector(double v)
Constructor, create vector with coordinates (v, v)
Definition: Geometry.h:78
Vector crossFromZVector(double l) const
Return the cross from (other x this) a (virtual, as we are in 2D) perpendicular vector (on axis z) of...
Definition: Geometry.h:121
void rotate(const double angle)
Rotate by a specific angle.
Definition: Geometry.h:313
Vector operator*(double f) const
Divive each component by scalar f and return the resulting vector.
Definition: Geometry.h:99
void operator*=(double f)
Multiply each component by scalar f.
Definition: Geometry.h:89
void translate(const Vector &delta)
Translate of a specific distance.
Definition: Geometry.h:300
Enki is the namespace of the Enki simulator. All Enki functions and classes excepted the math one are...
Definition: BluetoothBase.cpp:44
double getTriangleAreaTwice(const Point &a, const Point &b, const Point &c)
Returns 2 times the signed triangle area.
Definition: Geometry.h:475
bool isPointInside(const Point &p) const
Return true if p is inside this polygone.
Definition: Geometry.h:247
Matrix22()
Constructor, create matrix with 0.
Definition: Geometry.h:155
Vector crossWithZVector(double l) const
Return the cross with (this x other) a (virtual, as we are in 2D) perpendicular vector (on axis z) of...
Definition: Geometry.h:119
double cross(const Vector &v) const
Return the cross product with vector v.
Definition: Geometry.h:110
Segment(double array[4])
Constructor, create segment from point (array[0], array[1]) to point (array[2], array[3]) ...
Definition: Geometry.h:201
double norm2(void) const
Return the square norm of this vector (and thus avoid a square root)
Definition: Geometry.h:108
bool doesIntersect(const Segment &o) const
Return true if o intersect this segment.
Definition: Geometry.h:220
void extendAxisAlignedBoundingBox(Point &bottomLeft, Point &topRight) const
Extend an axis aligned bounding box with this object.
Definition: Geometry.h:272
Segment getSegment(size_t i) const
Return the i-th segment.
Definition: Geometry.h:241
Vector()
Constructor, create vector with coordinates (0, 0)
Definition: Geometry.h:76
void operator+=(const Vector &v)
Add vector v component by component.
Definition: Geometry.h:85
Point getIntersection(const Segment &s1, const Segment &s2)
get the intersection point between two line segments returns Point(HUGE_VAL, HUGE_VAL) if there's no ...
Definition: Geometry.h:378
void operator/=(double f)
Divive each component by scalar f.
Definition: Geometry.h:91
double getBoundingRadius() const
Return the bounding radius of this polygon.
Definition: Geometry.h:291
Vector operator+(const Vector &v) const
Add vector v component by component and return the resulting vector.
Definition: Geometry.h:93
double norm(void) const
Return the norm of this vector.
Definition: Geometry.h:106
Matrix22(double _11, double _21, double _12, double _22)
Constructor, create matrix with _11 _21 _12 _22.
Definition: Geometry.h:157
Vector unitary(void) const
Return a unitary vector of same direction.
Definition: Geometry.h:112
Matrix22(double alpha)
Constructor, create rotation matrix of angle alpha in radian.
Definition: Geometry.h:159
double x
x component
Definition: Geometry.h:71
double _22
22 components
Definition: Geometry.h:152
Vector operator-() const
Invert this vector.
Definition: Geometry.h:101
Segment(const Point &p1, const Point &p2)
Constructor, create segment from point p1 to point p2.
Definition: Geometry.h:203
Polygone, which is a vector of points. Anti-clockwise, standard trigonometric orientation.
Definition: Geometry.h:238
double getTriangleHeight(const Point &a, const Point &b, const Point &c)
Returns signed height of triangle abc with base ab.
Definition: Geometry.h:485
void flipX()
Flip coordinates on x.
Definition: Geometry.h:321
bool getAxisAlignedBoundingBox(Point &bottomLeft, Point &topRight) const
Get the axis aligned bounding box and return whether it exists.
Definition: Geometry.h:258
Vector operator/(double f) const
Multiply each component by scalar f and return the resulting vector.
Definition: Geometry.h:97
double _21
21 components
Definition: Geometry.h:148
Segment(double ax, double ay, double bx, double by)
Constructor, create segment from point (ax, ay) to point (bx, by)
Definition: Geometry.h:199
Vector perp(void) const
Return the perpendicular of the same norm in math. orientation (CCW)
Definition: Geometry.h:116
Vector(double array[2])
Constructor, create vector with coordinates (array[0], array[1])
Definition: Geometry.h:82
Vector(double x, double y)
Constructor, create vector with coordinates (x, y)
Definition: Geometry.h:80
Point operator*(const Point &v) const
Multiply vector v and return the resulting vector.
Definition: Geometry.h:186
void zeros()
Fill with zero.
Definition: Geometry.h:164
Point b
End point.
Definition: Geometry.h:208
A segment in a 2D space, basically two points.
Definition: Geometry.h:196
Point a
Start point.
Definition: Geometry.h:206
void flipY()
Flip coordinates on y.
Definition: Geometry.h:334
double _12
12 components
Definition: Geometry.h:150
void translate(const double x, const double y)
Translate of a specific distance, overload for convenience.
Definition: Geometry.h:307
double dist(const Point &p) const
Compute the distance of p to this segment.
Definition: Geometry.h:211
double normalizeAngle(double angle)
Normlize an angle to be between -PI and +PI.
Definition: Geometry.h:356