Code

60c5586b0526ee8468be7b5440fb710b5fb58fcc
[inkscape.git] / src / libnr / nr-point.h
1 #ifndef SEEN_NR_POINT_H
2 #define SEEN_NR_POINT_H
4 /** \file
5  * Cartesian point class.
6  */
8 //#include <math.h>
9 //#include <stdexcept>
10 #include <iostream>
11 //#include <iomanip>
13 #include <libnr/nr-coord.h>
14 #include <libnr/nr-dim2.h>
16 //#include "round.h"
17 #include "decimal-round.h"
19 #include <2geom/point.h>
21 /// A NRPoint consists of x and y coodinates.
22 /// \todo
23 /// This class appears to be obsoleted out in favour of NR::Point.
24 struct NRPoint {
25     NR::Coord x, y;
26 };
28 namespace NR {
30 class Matrix;
32 /// Cartesian point.
33 class Point {
34 public:
35     inline Point()
36     { _pt[X] = _pt[Y] = 0; }
38     inline Point(Coord x, Coord y) {
39         _pt[X] = x;
40         _pt[Y] = y;
41     }
43     inline Point(NRPoint const &p) {
44         _pt[X] = p.x;
45         _pt[Y] = p.y;
46     }
48     inline Point(Point const &p) {
49         for (unsigned i = 0; i < 2; ++i) {
50             _pt[i] = p._pt[i];
51         }
52     }
54     inline Point(Geom::Point const &p) {
55         _pt[X] = p[Geom::X];
56         _pt[Y] = p[Geom::Y];
57     }
59     inline Point &operator=(Point const &p) {
60         for (unsigned i = 0; i < 2; ++i) {
61             _pt[i] = p._pt[i];
62         }
63         return *this;
64     }
66     inline Coord operator[](unsigned i) const {
67         return _pt[i];
68     }
70     inline Coord &operator[](unsigned i) {
71         return _pt[i];
72     }
74     Coord operator[](Dim2 d) const throw() { return _pt[d]; }
75     Coord &operator[](Dim2 d) throw() { return _pt[d]; }
77     /** Return a point like this point but rotated -90 degrees.
78         (If the y axis grows downwards and the x axis grows to the
79         right, then this is 90 degrees counter-clockwise.)
80     **/
81     Point ccw() const {
82         return Point(_pt[Y], -_pt[X]);
83     }
85     /** Return a point like this point but rotated +90 degrees.
86         (If the y axis grows downwards and the x axis grows to the
87         right, then this is 90 degrees clockwise.)
88     **/
89     Point cw() const {
90         return Point(-_pt[Y], _pt[X]);
91     }
93     /**
94         \brief A function to lower the precision of the point
95         \param  places  The number of decimal places that should be in
96                         the final number.
97     */
98     inline void round (int places = 0) {
99         _pt[X] = (Coord)(Inkscape::decimal_round((double)_pt[X], places));
100         _pt[Y] = (Coord)(Inkscape::decimal_round((double)_pt[Y], places));
101         return;
102     }
104     void normalize();
106     inline Point &operator+=(Point const &o) {
107         for ( unsigned i = 0 ; i < 2 ; ++i ) {
108             _pt[i] += o._pt[i];
109         }
110         return *this;
111     }
112   
113     inline Point &operator-=(Point const &o) {
114         for ( unsigned i = 0 ; i < 2 ; ++i ) {
115             _pt[i] -= o._pt[i];
116         }
117         return *this;
118     }
119   
120     inline Point &operator/=(double const s) {
121         for ( unsigned i = 0 ; i < 2 ; ++i ) {
122             _pt[i] /= s;
123         }
124         return *this;
125     }
127     inline Point &operator*=(double const s) {
128         for ( unsigned i = 0 ; i < 2 ; ++i ) {
129             _pt[i] *= s;
130         }
131         return *this;
132     }
134     Point &operator*=(Matrix const &m);
136     inline int operator == (const Point &in_pnt) {
137         return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
138     }
140     friend inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt);
142     inline Geom::Point to_2geom() const {
143         return Geom::Point(_pt[X], _pt[Y]);
144     }
146 private:
147     Coord _pt[2];
148 };
150 /** A function to print out the Point.  It just prints out the coords
151     on the given output stream */
152 inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt) {
153     out_file << "X: " << in_pnt[X] << "  Y: " << in_pnt[Y];
154     return out_file;
157 } /* namespace NR */
159 #endif /* !SEEN_NR_POINT_H */
161 /*
162   Local Variables:
163   mode:c++
164   c-file-style:"stroustrup"
165   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166   indent-tabs-mode:nil
167   fill-column:99
168   End:
169 */
170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :