Code

moving trunk for module inkscape
[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 /// A NRPoint consists of x and y coodinates.
20 /// \todo
21 /// This class appears to be obsoleted out in favour of NR::Point.
22 struct NRPoint {
23     NR::Coord x, y;
24 };
26 namespace NR {
28 class Matrix;
30 /// Cartesian point.
31 class Point {
32 public:
33     inline Point()
34     { _pt[X] = _pt[Y] = 0; }
36     inline Point(Coord x, Coord y) {
37         _pt[X] = x;
38         _pt[Y] = y;
39     }
41     inline Point(NRPoint const &p) {
42         _pt[X] = p.x;
43         _pt[Y] = p.y;
44     }
46     inline Point(Point const &p) {
47         for (unsigned i = 0; i < 2; ++i) {
48             _pt[i] = p._pt[i];
49         }
50     }
52     inline Point &operator=(Point const &p) {
53         for (unsigned i = 0; i < 2; ++i) {
54             _pt[i] = p._pt[i];
55         }
56         return *this;
57     }
59     inline Coord operator[](unsigned i) const {
60         return _pt[i];
61     }
63     inline Coord &operator[](unsigned i) {
64         return _pt[i];
65     }
67     Coord operator[](Dim2 d) const throw() { return _pt[d]; }
68     Coord &operator[](Dim2 d) throw() { return _pt[d]; }
70     /** Return a point like this point but rotated -90 degrees.
71         (If the y axis grows downwards and the x axis grows to the
72         right, then this is 90 degrees counter-clockwise.)
73     **/
74     Point ccw() const {
75         return Point(_pt[Y], -_pt[X]);
76     }
78     /** Return a point like this point but rotated +90 degrees.
79         (If the y axis grows downwards and the x axis grows to the
80         right, then this is 90 degrees clockwise.)
81     **/
82     Point cw() const {
83         return Point(-_pt[Y], _pt[X]);
84     }
86     /**
87         \brief A function to lower the precision of the point
88         \param  places  The number of decimal places that should be in
89                         the final number.
90     */
91     inline void round (int places = 0) {
92         _pt[X] = (Coord)(Inkscape::decimal_round((double)_pt[X], places));
93         _pt[Y] = (Coord)(Inkscape::decimal_round((double)_pt[Y], places));
94         return;
95     }
97     void normalize();
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   
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/=(double const s) {
114         for ( unsigned i = 0 ; i < 2 ; ++i ) {
115             _pt[i] /= s;
116         }
117         return *this;
118     }
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     Point &operator*=(Matrix const &m);
129     inline int operator == (const Point &in_pnt) {
130         return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
131     }
133     friend inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt);
135 private:
136     Coord _pt[2];
137 };
139 /** A function to print out the Point.  It just prints out the coords
140     on the given output stream */
141 inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt) {
142     out_file << "X: " << in_pnt[X] << "  Y: " << in_pnt[Y];
143     return out_file;
146 } /* namespace NR */
148 #endif /* !SEEN_NR_POINT_H */
150 /*
151   Local Variables:
152   mode:c++
153   c-file-style:"stroustrup"
154   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155   indent-tabs-mode:nil
156   fill-column:99
157   End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :