1
#ifndef SEEN_Geom_POINT_H
2
#define SEEN_Geom_POINT_H
5
* Cartesian point class.
15
enum Dim2 { X=0, Y=1 };
25
{ _pt[X] = _pt[Y] = 0; }
27
inline Point(Coord x, Coord y) {
28
_pt[X] = x; _pt[Y] = y;
31
inline Point(Point const &p) {
32
for (unsigned i = 0; i < 2; ++i)
36
inline Point &operator=(Point const &p) {
37
for (unsigned i = 0; i < 2; ++i)
42
inline Coord operator[](unsigned i) const { return _pt[i]; }
43
inline Coord &operator[](unsigned i) { return _pt[i]; }
45
Coord operator[](Dim2 d) const throw() { return _pt[d]; }
46
Coord &operator[](Dim2 d) throw() { return _pt[d]; }
48
static inline Point polar(Coord angle, Coord radius) {
49
return Point(radius * std::cos(angle), radius * std::sin(angle));
52
inline Coord length() const { return hypot(_pt[0], _pt[1]); }
54
/** Return a point like this point but rotated -90 degrees.
55
(If the y axis grows downwards and the x axis grows to the
56
right, then this is 90 degrees counter-clockwise.)
59
return Point(_pt[Y], -_pt[X]);
62
/** Return a point like this point but rotated +90 degrees.
63
(If the y axis grows downwards and the x axis grows to the
64
right, then this is 90 degrees clockwise.)
67
return Point(-_pt[Y], _pt[X]);
71
\brief A function to lower the precision of the point
72
\param places The number of decimal places that should be in
75
inline void round (int places = 0) {
76
_pt[X] = (Coord)(decimal_round((double)_pt[X], places));
77
_pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
83
inline Point operator+(Point const &o) const {
84
return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
86
inline Point operator-(Point const &o) const {
87
return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
89
inline Point &operator+=(Point const &o) {
90
for ( unsigned i = 0 ; i < 2 ; ++i ) {
95
inline Point &operator-=(Point const &o) {
96
for ( unsigned i = 0 ; i < 2 ; ++i ) {
102
inline Point operator-() const {
103
return Point(-_pt[X], -_pt[Y]);
105
inline Point operator*(double const s) const {
106
return Point(_pt[X] * s, _pt[Y] * s);
108
inline Point operator/(double const s) const {
110
return Point(_pt[X] / s, _pt[Y] / s);
112
inline Point &operator*=(double const s) {
113
for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
116
inline Point &operator/=(double const s) {
118
for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
122
Point &operator*=(Matrix const &m);
124
inline int operator == (const Point &in_pnt) {
125
return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
128
friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
131
inline Point operator*(double const s, Point const &p) { return p * s; }
133
/** A function to print out the Point. It just prints out the coords
134
on the given output stream */
135
inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
136
out_file << "X: " << in_pnt[X] << " Y: " << in_pnt[Y];
140
/** This is a rotation (sort of). */
141
inline Point operator^(Point const &a, Point const &b) {
142
Point const ret(a[0] * b[0] - a[1] * b[1],
143
a[1] * b[0] + a[0] * b[1]);
147
//IMPL: boost::EqualityComparableConcept
148
inline bool operator==(Point const &a, Point const &b) {
149
return (a[X] == b[X]) && (a[Y] == b[Y]);
151
inline bool operator!=(Point const &a, Point const &b) {
152
return (a[X] != b[X]) || (a[Y] != b[Y]);
155
/** This is a lexicographical ordering for points. It is remarkably useful for sweepline algorithms*/
156
inline bool operator<=(Point const &a, Point const &b) {
157
return ( ( a[Y] < b[Y] ) ||
158
(( a[Y] == b[Y] ) && ( a[X] < b[X] )));
161
Coord L1(Point const &p);
163
/** Compute the L2, or euclidean, norm of \a p. */
164
inline Coord L2(Point const &p) { return p.length(); }
166
/** Compute the square of L2 norm of \a p. Warning: this can overflow where L2 won't.*/
167
inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }
169
double LInfty(Point const &p);
170
bool is_zero(Point const &p);
171
bool is_unit_vector(Point const &p);
173
extern double atan2(Point const p);
174
/** compute the angle turning from a to b (signed). */
175
extern double angle_between(Point const a, Point const b);
178
inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
179
return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
182
/** Returns p * Geom::rotate_degrees(90), but more efficient.
184
* Angle direction in Inkscape code: If you use the traditional mathematics convention that y
185
* increases upwards, then positive angles are anticlockwise as per the mathematics convention. If
186
* you take the common non-mathematical convention that y increases downwards, then positive angles
187
* are clockwise, as is common outside of mathematics.
189
* There is no rot_neg90 function: use -rot90(p) instead.
191
inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }
193
/** Given two points and a parameter t \in [0, 1], return a point
194
* proportionally from a to b by t. Akin to 1 degree bezier.*/
195
inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }
197
Point unit_vector(Point const &a);
199
/** compute the dot product (inner product) between the vectors a and b. */
200
inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
201
/** Defined as dot(a, b.cw()). */
202
inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }
204
/** compute the euclidean distance between points a and b. TODO: hypot safer/faster? */
205
inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }
207
/** compute the square of the distance between points a and b. */
208
inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }
210
Point abs(Point const &b);
212
Point operator*(Point const &v, Matrix const &m);
214
Point operator/(Point const &p, Matrix const &m);
216
} /* namespace Geom */
218
#endif /* !SEEN_Geom_POINT_H */
223
c-file-style:"stroustrup"
224
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
229
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :