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 namespace NR {
23 class Matrix;
25 /// Cartesian point.
26 class Point {
27 public:
28 inline Point()
29 { _pt[X] = _pt[Y] = 0; }
31 inline Point(Coord x, Coord y) {
32 _pt[X] = x;
33 _pt[Y] = y;
34 }
36 inline Point(Point const &p) {
37 for (unsigned i = 0; i < 2; ++i) {
38 _pt[i] = p._pt[i];
39 }
40 }
42 inline Point(Geom::Point const &p) {
43 _pt[X] = p[Geom::X];
44 _pt[Y] = p[Geom::Y];
45 }
47 inline Point &operator=(Point const &p) {
48 for (unsigned i = 0; i < 2; ++i) {
49 _pt[i] = p._pt[i];
50 }
51 return *this;
52 }
54 inline Coord operator[](unsigned i) const {
55 return _pt[i];
56 }
58 inline Coord &operator[](unsigned i) {
59 return _pt[i];
60 }
62 Coord operator[](Dim2 d) const throw() { return _pt[d]; }
63 Coord &operator[](Dim2 d) throw() { return _pt[d]; }
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 counter-clockwise.)
68 **/
69 Point ccw() const {
70 return Point(_pt[Y], -_pt[X]);
71 }
73 /** Return a point like this point but rotated +90 degrees.
74 (If the y axis grows downwards and the x axis grows to the
75 right, then this is 90 degrees clockwise.)
76 **/
77 Point cw() const {
78 return Point(-_pt[Y], _pt[X]);
79 }
81 /**
82 \brief A function to lower the precision of the point
83 \param places The number of decimal places that should be in
84 the final number.
85 */
86 inline void round (int places = 0) {
87 _pt[X] = (Coord)(Inkscape::decimal_round((double)_pt[X], places));
88 _pt[Y] = (Coord)(Inkscape::decimal_round((double)_pt[Y], places));
89 return;
90 }
92 void normalize();
94 inline Point &operator+=(Point const &o) {
95 for ( unsigned i = 0 ; i < 2 ; ++i ) {
96 _pt[i] += o._pt[i];
97 }
98 return *this;
99 }
101 inline Point &operator-=(Point const &o) {
102 for ( unsigned i = 0 ; i < 2 ; ++i ) {
103 _pt[i] -= o._pt[i];
104 }
105 return *this;
106 }
108 inline Point &operator/=(double const s) {
109 for ( unsigned i = 0 ; i < 2 ; ++i ) {
110 _pt[i] /= s;
111 }
112 return *this;
113 }
115 inline Point &operator*=(double const s) {
116 for ( unsigned i = 0 ; i < 2 ; ++i ) {
117 _pt[i] *= s;
118 }
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 NR::Point &in_pnt);
130 inline Geom::Point to_2geom() const {
131 return Geom::Point(_pt[X], _pt[Y]);
132 }
134 private:
135 Coord _pt[2];
136 };
138 /** A function to print out the Point. It just prints out the coords
139 on the given output stream */
140 inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt) {
141 out_file << "X: " << in_pnt[X] << " Y: " << in_pnt[Y];
142 return out_file;
143 }
145 } /* namespace NR */
147 #endif /* !SEEN_NR_POINT_H */
149 /*
150 Local Variables:
151 mode:c++
152 c-file-style:"stroustrup"
153 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
154 indent-tabs-mode:nil
155 fill-column:99
156 End:
157 */
158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :