1
#ifndef SEEN_Geom_POINT_H
2
#define SEEN_Geom_POINT_H
6
* \brief Defines a Cartesian 2D Point class.
11
#include <2geom/coord.h>
12
#include <2geom/isnan.h>
13
#include <2geom/utils.h>
17
enum Dim2 { X=0, Y=1 };
21
/// Cartesian 2D point.
26
/// The default constructor creates an Point(0,0) DO NOT RELY ON THIS, BEST NOT TO USE THIS CONSTRUCTOR
28
{ _pt[X] = _pt[Y] = 0; }
30
inline Point(Coord x, Coord y) {
31
_pt[X] = x; _pt[Y] = y;
34
inline Point(Point const &p) {
35
for (unsigned i = 0; i < 2; ++i)
39
inline Point &operator=(Point const &p) {
40
for (unsigned i = 0; i < 2; ++i)
45
inline Coord operator[](unsigned i) const { return _pt[i]; }
46
inline Coord &operator[](unsigned i) { return _pt[i]; }
48
Coord operator[](Dim2 d) const throw() { return _pt[d]; }
49
Coord &operator[](Dim2 d) throw() { return _pt[d]; }
51
static inline Point polar(Coord angle, Coord radius) {
52
return Point(radius * std::cos(angle), radius * std::sin(angle));
55
inline Coord length() const { return hypot(_pt[0], _pt[1]); }
57
/** Return a point like this point but rotated -90 degrees.
58
(If the y axis grows downwards and the x axis grows to the
59
right, then this is 90 degrees counter-clockwise.)
62
return Point(_pt[Y], -_pt[X]);
65
/** Return a point like this point but rotated +90 degrees.
66
(If the y axis grows downwards and the x axis grows to the
67
right, then this is 90 degrees clockwise.)
70
return Point(-_pt[Y], _pt[X]);
74
\brief A function to lower the precision of the point
75
\param places The number of decimal places that should be in
78
inline void round (int places = 0) {
79
_pt[X] = (Coord)(decimal_round((double)_pt[X], places));
80
_pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
86
inline bool isFinite() const {
87
for ( unsigned i = 0 ; i < 2 ; ++i ) {
88
if(!IS_FINITE(_pt[i])) return false;
93
inline Point operator+(Point const &o) const {
94
return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
96
inline Point operator-(Point const &o) const {
97
return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
99
inline Point &operator+=(Point const &o) {
100
for ( unsigned i = 0 ; i < 2 ; ++i ) {
105
inline Point &operator-=(Point const &o) {
106
for ( unsigned i = 0 ; i < 2 ; ++i ) {
112
inline Point operator-() const {
113
return Point(-_pt[X], -_pt[Y]);
115
inline Point operator*(double const s) const {
116
return Point(_pt[X] * s, _pt[Y] * s);
118
inline Point operator/(double const s) const {
120
return Point(_pt[X] / s, _pt[Y] / s);
122
inline Point &operator*=(double const s) {
123
for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
126
inline Point &operator/=(double const s) {
128
for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
132
Point &operator*=(Matrix const &m);
134
inline int operator == (const Point &in_pnt) {
135
return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
138
friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
141
inline Point operator*(double const s, Point const &p) { return p * s; }
143
/** A function to print out the Point. It just prints out the coords
144
on the given output stream */
145
inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
146
out_file << "X: " << in_pnt[X] << " Y: " << in_pnt[Y];
150
/** This is a rotation (sort of). */
151
inline Point operator^(Point const &a, Point const &b) {
152
Point const ret(a[0] * b[0] - a[1] * b[1],
153
a[1] * b[0] + a[0] * b[1]);
157
//IMPL: boost::EqualityComparableConcept
158
inline bool operator==(Point const &a, Point const &b) {
159
return (a[X] == b[X]) && (a[Y] == b[Y]);
161
inline bool operator!=(Point const &a, Point const &b) {
162
return (a[X] != b[X]) || (a[Y] != b[Y]);
165
/** This is a lexicographical ordering for points. It is remarkably useful for sweepline algorithms*/
166
inline bool operator<=(Point const &a, Point const &b) {
167
return ( ( a[Y] < b[Y] ) ||
168
(( a[Y] == b[Y] ) && ( a[X] < b[X] )));
171
Coord L1(Point const &p);
173
/** Compute the L2, or euclidean, norm of \a p. */
174
inline Coord L2(Point const &p) { return p.length(); }
176
/** Compute the square of L2 norm of \a p. Warning: this can overflow where L2 won't.*/
177
inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }
179
double LInfty(Point const &p);
180
bool is_zero(Point const &p);
181
bool is_unit_vector(Point const &p);
183
extern double atan2(Point const p);
184
/** compute the angle turning from a to b (signed). */
185
extern double angle_between(Point const a, Point const b);
188
inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
189
return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
193
Point middle_point(Point const& P1, Point const& P2)
195
return (P1 + P2) / 2;
198
/** Returns p * Geom::rotate_degrees(90), but more efficient.
200
* Angle direction in Inkscape code: If you use the traditional mathematics convention that y
201
* increases upwards, then positive angles are anticlockwise as per the mathematics convention. If
202
* you take the common non-mathematical convention that y increases downwards, then positive angles
203
* are clockwise, as is common outside of mathematics.
205
* There is no rot_neg90 function: use -rot90(p) instead.
207
inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }
209
/** Given two points and a parameter t \in [0, 1], return a point
210
* proportionally from a to b by t. Akin to 1 degree bezier.*/
211
inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }
213
Point unit_vector(Point const &a);
215
/** compute the dot product (inner product) between the vectors a and b. */
216
inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
217
/** Defined as dot(a, b.cw()). */
218
inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }
220
/** compute the euclidean distance between points a and b. TODO: hypot safer/faster? */
221
inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }
223
/** compute the square of the distance between points a and b. */
224
inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }
226
Point abs(Point const &b);
228
Point operator*(Point const &v, Matrix const &m);
230
Point operator/(Point const &p, Matrix const &m);
232
/** Constrains the angle (with respect to dir) of the line
233
* joining A and B to a multiple of pi/n.
235
Point constrain_angle(Point const &A, Point const &B, unsigned int n = 4, Geom::Point const &dir = Geom::Point(1,0));
237
} /* namespace Geom */
239
#endif /* !SEEN_Geom_POINT_H */
244
c-file-style:"stroustrup"
245
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :