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>
15 #include <libnr/nr-forward.h>
17 //#include "round.h"
18 #include "decimal-round.h"
20 #include <2geom/point.h>
22 namespace NR {
24 /// Cartesian point.
25 class Point {
26 public:
27 inline Point()
28 { _pt[X] = _pt[Y] = 0; }
30 inline Point(Coord x, Coord y) {
31 _pt[X] = x;
32 _pt[Y] = y;
33 }
35 inline Point(Point const &p) {
36 for (unsigned i = 0; i < 2; ++i) {
37 _pt[i] = p._pt[i];
38 }
39 }
41 inline Point(Geom::Point const &p) {
42 _pt[X] = p[Geom::X];
43 _pt[Y] = p[Geom::Y];
44 }
46 inline Point &operator=(Point const &p) {
47 for (unsigned i = 0; i < 2; ++i) {
48 _pt[i] = p._pt[i];
49 }
50 return *this;
51 }
53 inline Coord operator[](unsigned i) const {
54 return _pt[i];
55 }
57 inline Coord &operator[](unsigned i) {
58 return _pt[i];
59 }
61 Coord operator[](Dim2 d) const throw() { return _pt[d]; }
62 Coord &operator[](Dim2 d) throw() { return _pt[d]; }
64 /** Return a point like this point but rotated -90 degrees.
65 (If the y axis grows downwards and the x axis grows to the
66 right, then this is 90 degrees counter-clockwise.)
67 **/
68 Point ccw() const {
69 return Point(_pt[Y], -_pt[X]);
70 }
72 /** Return a point like this point but rotated +90 degrees.
73 (If the y axis grows downwards and the x axis grows to the
74 right, then this is 90 degrees clockwise.)
75 **/
76 Point cw() const {
77 return Point(-_pt[Y], _pt[X]);
78 }
80 /**
81 \brief A function to lower the precision of the point
82 \param places The number of decimal places that should be in
83 the final number.
84 */
85 inline void round (int places = 0) {
86 _pt[X] = (Coord)(Inkscape::decimal_round((double)_pt[X], places));
87 _pt[Y] = (Coord)(Inkscape::decimal_round((double)_pt[Y], places));
88 return;
89 }
91 void normalize();
93 inline Point &operator+=(Point const &o) {
94 for ( unsigned i = 0 ; i < 2 ; ++i ) {
95 _pt[i] += o._pt[i];
96 }
97 return *this;
98 }
100 inline Point &operator-=(Point const &o) {
101 for ( unsigned i = 0 ; i < 2 ; ++i ) {
102 _pt[i] -= o._pt[i];
103 }
104 return *this;
105 }
107 inline Point &operator/=(double const s) {
108 for ( unsigned i = 0 ; i < 2 ; ++i ) {
109 _pt[i] /= s;
110 }
111 return *this;
112 }
114 inline Point &operator*=(double const s) {
115 for ( unsigned i = 0 ; i < 2 ; ++i ) {
116 _pt[i] *= s;
117 }
118 return *this;
119 }
121 Point &operator*=(Matrix const &m);
123 inline int operator == (const Point &in_pnt) {
124 return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
125 }
127 friend inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt);
129 inline operator Geom::Point() const { return Geom::Point(_pt[X], _pt[Y]); }
131 private:
132 Coord _pt[2];
133 };
135 /** A function to print out the Point. It just prints out the coords
136 on the given output stream */
137 inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt) {
138 out_file << "X: " << in_pnt[X] << " Y: " << in_pnt[Y];
139 return out_file;
140 }
142 } /* namespace NR */
144 #endif /* !SEEN_NR_POINT_H */
146 /*
147 Local Variables:
148 mode:c++
149 c-file-style:"stroustrup"
150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
151 indent-tabs-mode:nil
152 fill-column:99
153 End:
154 */
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :