Code

Merge from fe-moved
[inkscape.git] / src / 2geom / point.h
1 #ifndef SEEN_Geom_POINT_H
2 #define SEEN_Geom_POINT_H
4 /**
5  *  \file
6  *  \brief Defines a Cartesian 2D Point class.
7  */
9 #include <iostream>
11 #include <2geom/coord.h>
12 #include <2geom/isnan.h>
13 #include <2geom/utils.h>
15 namespace Geom {
17 enum Dim2 { X=0, Y=1 };
19 class Matrix;
21 /// Cartesian 2D point.
22 class Point {
23     Coord _pt[2];
25   public:
26     /// The default constructor creates an Point(0,0)  DO NOT RELY ON THIS, BEST NOT TO USE THIS CONSTRUCTOR
27     inline Point()
28     { _pt[X] = _pt[Y] = 0; }
30     inline Point(Coord x, Coord y) {
31         _pt[X] = x; _pt[Y] = y;
32     }
34     inline Point(Point const &p) {
35         for (unsigned i = 0; i < 2; ++i)
36             _pt[i] = p._pt[i];
37     }
39     inline Point &operator=(Point const &p) {
40         for (unsigned i = 0; i < 2; ++i)
41             _pt[i] = p._pt[i];
42         return *this;
43     }
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));
53     }
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.)
60     **/
61     Point ccw() const {
62         return Point(_pt[Y], -_pt[X]);
63     }
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.)
68     **/
69     Point cw() const {
70         return Point(-_pt[Y], _pt[X]);
71     }
73     /**
74         \brief A function to lower the precision of the point
75         \param  places  The number of decimal places that should be in
76                         the final number.
77     */
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));
81         return;
82     }
84     void normalize();
86     inline bool isFinite() const {
87         for ( unsigned i = 0 ; i < 2 ; ++i ) {
88             if(!IS_FINITE(_pt[i])) return false;
89         }
90         return true;
91     }
93     inline Point operator+(Point const &o) const {
94         return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
95     }
96     inline Point operator-(Point const &o) const {
97         return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
98     }
99     inline Point &operator+=(Point const &o) {
100         for ( unsigned i = 0 ; i < 2 ; ++i ) {
101             _pt[i] += o._pt[i];
102         }
103         return *this;
104     }
105     inline Point &operator-=(Point const &o) {
106         for ( unsigned i = 0 ; i < 2 ; ++i ) {
107             _pt[i] -= o._pt[i];
108         }
109         return *this;
110     }
112     inline Point operator-() const {
113         return Point(-_pt[X], -_pt[Y]);
114     }
115     inline Point operator*(double const s) const {
116         return Point(_pt[X] * s, _pt[Y] * s);
117     }
118     inline Point operator/(double const s) const {
119         //TODO: s == 0?
120         return Point(_pt[X] / s, _pt[Y] / s);
121     }
122     inline Point &operator*=(double const s) {
123         for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
124         return *this;
125     }
126     inline Point &operator/=(double const s) {
127         //TODO: s == 0?
128         for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
129         return *this;
130     }
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]));
136     }
138     friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
139 };
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];
147     return out_file;
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]);
154     return ret;
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);
187 //IMPL: NearConcept
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) );
192 inline
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.
199  *
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.
204  *
205  * There is no rot_neg90 function: use -rot90(p) instead.
206  */
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 } /* namespace Geom */
234 #endif /* !SEEN_Geom_POINT_H */
236 /*
237   Local Variables:
238   mode:c++
239   c-file-style:"stroustrup"
240   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
241   indent-tabs-mode:nil
242   fill-column:99
243   End:
244 */
245 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :