1 #ifndef SEEN_Geom_POINT_H
2 #define SEEN_Geom_POINT_H
4 /** \file
5 * Cartesian point class.
6 */
8 #include <iostream>
10 #include "coord.h"
11 #include "utils.h"
13 namespace Geom {
15 enum Dim2 { X=0, Y=1 };
17 class Matrix;
19 /// Cartesian point.
20 class Point {
21 Coord _pt[2];
23 public:
24 inline Point()
25 { _pt[X] = _pt[Y] = 0; }
27 inline Point(Coord x, Coord y) {
28 _pt[X] = x; _pt[Y] = y;
29 }
31 inline Point(Point const &p) {
32 for (unsigned i = 0; i < 2; ++i)
33 _pt[i] = p._pt[i];
34 }
36 inline Point &operator=(Point const &p) {
37 for (unsigned i = 0; i < 2; ++i)
38 _pt[i] = p._pt[i];
39 return *this;
40 }
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));
50 }
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.)
57 **/
58 Point ccw() const {
59 return Point(_pt[Y], -_pt[X]);
60 }
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.)
65 **/
66 Point cw() const {
67 return Point(-_pt[Y], _pt[X]);
68 }
70 /**
71 \brief A function to lower the precision of the point
72 \param places The number of decimal places that should be in
73 the final number.
74 */
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));
78 return;
79 }
81 void normalize();
83 inline Point operator+(Point const &o) const {
84 return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
85 }
86 inline Point operator-(Point const &o) const {
87 return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
88 }
89 inline Point &operator+=(Point const &o) {
90 for ( unsigned i = 0 ; i < 2 ; ++i ) {
91 _pt[i] += o._pt[i];
92 }
93 return *this;
94 }
95 inline Point &operator-=(Point const &o) {
96 for ( unsigned i = 0 ; i < 2 ; ++i ) {
97 _pt[i] -= o._pt[i];
98 }
99 return *this;
100 }
102 inline Point operator-() const {
103 return Point(-_pt[X], -_pt[Y]);
104 }
105 inline Point operator*(double const s) const {
106 return Point(_pt[X] * s, _pt[Y] * s);
107 }
108 inline Point operator/(double const s) const {
109 //TODO: s == 0?
110 return Point(_pt[X] / s, _pt[Y] / s);
111 }
112 inline Point &operator*=(double const s) {
113 for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
114 return *this;
115 }
116 inline Point &operator/=(double const s) {
117 //TODO: s == 0?
118 for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
119 return *this;
120 }
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]));
126 }
128 friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
129 };
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];
137 return out_file;
138 }
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]);
144 return ret;
145 }
147 //IMPL: boost::EqualityComparableConcept
148 inline bool operator==(Point const &a, Point const &b) {
149 return (a[X] == b[X]) && (a[Y] == b[Y]);
150 }
151 inline bool operator!=(Point const &a, Point const &b) {
152 return (a[X] != b[X]) || (a[Y] != b[Y]);
153 }
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] )));
159 }
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);
177 //IMPL: NearConcept
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) );
180 }
182 /** Returns p * Geom::rotate_degrees(90), but more efficient.
183 *
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.
188 *
189 * There is no rot_neg90 function: use -rot90(p) instead.
190 */
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 */
220 /*
221 Local Variables:
222 mode:c++
223 c-file-style:"stroustrup"
224 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
225 indent-tabs-mode:nil
226 fill-column:99
227 End:
228 */
229 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :