Code

e6e74242d5d7c4adae2982307fb1859ef15b2da3
[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/utils.h>
14 namespace Geom {
16 enum Dim2 { X=0, Y=1 };
18 class Matrix;
20 /// Cartesian 2D point.
21 class Point {
22     Coord _pt[2];
24   public:
25     inline Point()
26     { _pt[X] = _pt[Y] = 0; }
28     inline Point(Coord x, Coord y) {
29         _pt[X] = x; _pt[Y] = y;
30     }
32     inline Point(Point const &p) {
33         for (unsigned i = 0; i < 2; ++i)
34             _pt[i] = p._pt[i];
35     }
37     inline Point &operator=(Point const &p) {
38         for (unsigned i = 0; i < 2; ++i)
39             _pt[i] = p._pt[i];
40         return *this;
41     }
43     inline Coord operator[](unsigned i) const { return _pt[i]; }
44     inline Coord &operator[](unsigned i) { return _pt[i]; }
46     Coord operator[](Dim2 d) const throw() { return _pt[d]; }
47     Coord &operator[](Dim2 d) throw() { return _pt[d]; }
49     static inline Point polar(Coord angle, Coord radius) {
50         return Point(radius * std::cos(angle), radius * std::sin(angle));
51     }
53     inline Coord length() const { return hypot(_pt[0], _pt[1]); }
55     /** Return a point like this point but rotated -90 degrees.
56         (If the y axis grows downwards and the x axis grows to the
57         right, then this is 90 degrees counter-clockwise.)
58     **/
59     Point ccw() const {
60         return Point(_pt[Y], -_pt[X]);
61     }
63     /** Return a point like this point but rotated +90 degrees.
64         (If the y axis grows downwards and the x axis grows to the
65         right, then this is 90 degrees clockwise.)
66     **/
67     Point cw() const {
68         return Point(-_pt[Y], _pt[X]);
69     }
71     /**
72         \brief A function to lower the precision of the point
73         \param  places  The number of decimal places that should be in
74                         the final number.
75     */
76     inline void round (int places = 0) {
77         _pt[X] = (Coord)(decimal_round((double)_pt[X], places));
78         _pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
79         return;
80     }
82     void normalize();
84     inline Point operator+(Point const &o) const {
85         return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
86     }
87     inline Point operator-(Point const &o) const {
88         return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
89     }
90     inline Point &operator+=(Point const &o) {
91         for ( unsigned i = 0 ; i < 2 ; ++i ) {
92             _pt[i] += o._pt[i];
93         }
94         return *this;
95     }
96     inline Point &operator-=(Point const &o) {
97         for ( unsigned i = 0 ; i < 2 ; ++i ) {
98             _pt[i] -= o._pt[i];
99         }
100         return *this;
101     }
103     inline Point operator-() const {
104         return Point(-_pt[X], -_pt[Y]);
105     }
106     inline Point operator*(double const s) const {
107         return Point(_pt[X] * s, _pt[Y] * s);
108     }
109     inline Point operator/(double const s) const {
110         //TODO: s == 0?
111         return Point(_pt[X] / s, _pt[Y] / s);
112     }
113     inline Point &operator*=(double const s) {
114         for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
115         return *this;
116     }
117     inline Point &operator/=(double const s) {
118         //TODO: s == 0?
119         for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
120         return *this;
121     }
123     Point &operator*=(Matrix const &m);
125     inline int operator == (const Point &in_pnt) {
126         return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
127     }
129     friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
130 };
132 inline Point operator*(double const s, Point const &p) { return p * s; }
134 /** A function to print out the Point.  It just prints out the coords
135     on the given output stream */
136 inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
137     out_file << "X: " << in_pnt[X] << "  Y: " << in_pnt[Y];
138     return out_file;
141 /** This is a rotation (sort of). */
142 inline Point operator^(Point const &a, Point const &b) {
143     Point const ret(a[0] * b[0] - a[1] * b[1],
144                     a[1] * b[0] + a[0] * b[1]);
145     return ret;
148 //IMPL: boost::EqualityComparableConcept
149 inline bool operator==(Point const &a, Point const &b) {
150     return (a[X] == b[X]) && (a[Y] == b[Y]);
152 inline bool operator!=(Point const &a, Point const &b) {
153     return (a[X] != b[X]) || (a[Y] != b[Y]);
156 /** This is a lexicographical ordering for points.  It is remarkably useful for sweepline algorithms*/
157 inline bool operator<=(Point const &a, Point const &b) {
158     return ( ( a[Y] < b[Y] ) ||
159              (( a[Y] == b[Y] ) && ( a[X] < b[X] )));
162 Coord L1(Point const &p);
164 /** Compute the L2, or euclidean, norm of \a p. */
165 inline Coord L2(Point const &p) { return p.length(); }
167 /** Compute the square of L2 norm of \a p. Warning: this can overflow where L2 won't.*/
168 inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }
170 double LInfty(Point const &p);
171 bool is_zero(Point const &p);
172 bool is_unit_vector(Point const &p);
174 extern double atan2(Point const p);
175 /** compute the angle turning from a to b (signed). */
176 extern double angle_between(Point const a, Point const b);
178 //IMPL: NearConcept
179 inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
180     return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
183 inline
184 Point middle_point(Point const& P1, Point const& P2)
186     return (P1 + P2) / 2;
189 /** Returns p * Geom::rotate_degrees(90), but more efficient.
190  *
191  * Angle direction in Inkscape code: If you use the traditional mathematics convention that y
192  * increases upwards, then positive angles are anticlockwise as per the mathematics convention.  If
193  * you take the common non-mathematical convention that y increases downwards, then positive angles
194  * are clockwise, as is common outside of mathematics.
195  *
196  * There is no rot_neg90 function: use -rot90(p) instead.
197  */
198 inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }
200 /** Given two points and a parameter t \in [0, 1], return a point
201  * proportionally from a to b by t.  Akin to 1 degree bezier.*/
202 inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }
204 Point unit_vector(Point const &a);
206 /** compute the dot product (inner product) between the vectors a and b. */
207 inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
208 /** Defined as dot(a, b.cw()). */
209 inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }
211 /** compute the euclidean distance between points a and b.  TODO: hypot safer/faster? */
212 inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }
214 /** compute the square of the distance between points a and b. */
215 inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }
217 Point abs(Point const &b);
219 Point operator*(Point const &v, Matrix const &m);
221 Point operator/(Point const &p, Matrix const &m);
223 } /* namespace Geom */
225 #endif /* !SEEN_Geom_POINT_H */
227 /*
228   Local Variables:
229   mode:c++
230   c-file-style:"stroustrup"
231   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
232   indent-tabs-mode:nil
233   fill-column:99
234   End:
235 */
236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :