Scribus
Open source desktop publishing at your fingertips
point.h
Go to the documentation of this file.
1 #ifndef SEEN_Geom_POINT_H
2 #define SEEN_Geom_POINT_H
3 
8 #include <iostream>
9 
10 #include "coord.h"
11 #include "utils.h"
12 
13 namespace Geom {
14 
15 enum Dim2 { X=0, Y=1 };
16 
17 class Matrix;
18 
20 class Point {
21  Coord _pt[2];
22 
23  public:
24  inline Point()
25  { _pt[X] = _pt[Y] = 0; }
26 
27  inline Point(Coord x, Coord y) {
28  _pt[X] = x; _pt[Y] = y;
29  }
30 
31  inline Point(Point const &p) {
32  for (unsigned i = 0; i < 2; ++i)
33  _pt[i] = p._pt[i];
34  }
35 
36  inline Point &operator=(Point const &p) {
37  for (unsigned i = 0; i < 2; ++i)
38  _pt[i] = p._pt[i];
39  return *this;
40  }
41 
42  inline Coord operator[](unsigned i) const { return _pt[i]; }
43  inline Coord &operator[](unsigned i) { return _pt[i]; }
44 
45  Coord operator[](Dim2 d) const throw() { return _pt[d]; }
46  Coord &operator[](Dim2 d) throw() { return _pt[d]; }
47 
48  static inline Point polar(Coord angle, Coord radius) {
49  return Point(radius * std::cos(angle), radius * std::sin(angle));
50  }
51 
52  inline Coord length() const { return hypot(_pt[0], _pt[1]); }
53 
58  Point ccw() const {
59  return Point(_pt[Y], -_pt[X]);
60  }
61 
66  Point cw() const {
67  return Point(-_pt[Y], _pt[X]);
68  }
69 
75  inline void round (int places = 0) {
76  _pt[X] = (Coord)(decimal_round((double)_pt[X], places));
77  _pt[Y] = (Coord)(decimal_round((double)_pt[Y], places));
78  return;
79  }
80 
81  void normalize();
82 
83  inline Point operator+(Point const &o) const {
84  return Point(_pt[X] + o._pt[X], _pt[Y] + o._pt[Y]);
85  }
86  inline Point operator-(Point const &o) const {
87  return Point(_pt[X] - o._pt[X], _pt[Y] - o._pt[Y]);
88  }
89  inline Point &operator+=(Point const &o) {
90  for ( unsigned i = 0 ; i < 2 ; ++i ) {
91  _pt[i] += o._pt[i];
92  }
93  return *this;
94  }
95  inline Point &operator-=(Point const &o) {
96  for ( unsigned i = 0 ; i < 2 ; ++i ) {
97  _pt[i] -= o._pt[i];
98  }
99  return *this;
100  }
101 
102  inline Point operator-() const {
103  return Point(-_pt[X], -_pt[Y]);
104  }
105  inline Point operator*(double const s) const {
106  return Point(_pt[X] * s, _pt[Y] * s);
107  }
108  inline Point operator/(double const s) const {
109  //TODO: s == 0?
110  return Point(_pt[X] / s, _pt[Y] / s);
111  }
112  inline Point &operator*=(double const s) {
113  for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] *= s;
114  return *this;
115  }
116  inline Point &operator/=(double const s) {
117  //TODO: s == 0?
118  for ( unsigned i = 0 ; i < 2 ; ++i ) _pt[i] /= s;
119  return *this;
120  }
121 
122  Point &operator*=(Matrix const &m);
123 
124  inline int operator == (const Point &in_pnt) {
125  return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
126  }
127 
128  friend inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt);
129 };
130 
131 inline Point operator*(double const s, Point const &p) { return p * s; }
132 
135 inline std::ostream &operator<< (std::ostream &out_file, const Geom::Point &in_pnt) {
136  out_file << "X: " << in_pnt[X] << " Y: " << in_pnt[Y];
137  return out_file;
138 }
139 
141 inline Point operator^(Point const &a, Point const &b) {
142  Point const ret(a[0] * b[0] - a[1] * b[1],
143  a[1] * b[0] + a[0] * b[1]);
144  return ret;
145 }
146 
147 //IMPL: boost::EqualityComparableConcept
148 inline bool operator==(Point const &a, Point const &b) {
149  return (a[X] == b[X]) && (a[Y] == b[Y]);
150 }
151 inline bool operator!=(Point const &a, Point const &b) {
152  return (a[X] != b[X]) || (a[Y] != b[Y]);
153 }
154 
156 inline bool operator<=(Point const &a, Point const &b) {
157  return ( ( a[Y] < b[Y] ) ||
158  (( a[Y] == b[Y] ) && ( a[X] < b[X] )));
159 }
160 
161 Coord L1(Point const &p);
162 
164 inline Coord L2(Point const &p) { return p.length(); }
165 
167 inline Coord L2sq(Point const &p) { return p[0]*p[0] + p[1]*p[1]; }
168 
169 double LInfty(Point const &p);
170 bool is_zero(Point const &p);
171 bool is_unit_vector(Point const &p);
172 
173 extern double atan2(Point const p);
175 extern double angle_between(Point const a, Point const b);
176 
177 //IMPL: NearConcept
178 inline bool are_near(Point const &a, Point const &b, double const eps=EPSILON) {
179  return ( are_near(a[X],b[X],eps) && are_near(a[Y],b[Y],eps) );
180 }
181 
191 inline Point rot90(Point const &p) { return Point(-p[Y], p[X]); }
192 
195 inline Point lerp(double const t, Point const a, Point const b) { return (a * (1 - t) + b * t); }
196 
197 Point unit_vector(Point const &a);
198 
200 inline Coord dot(Point const &a, Point const &b) { return a[0] * b[0] + a[1] * b[1]; }
202 inline Coord cross(Point const &a, Point const &b) { return dot(a, b.cw()); }
203 
205 inline Coord distance (Point const &a, Point const &b) { return L2(a - b); }
206 
208 inline Coord distanceSq (Point const &a, Point const &b) { return L2sq(a - b); }
209 
210 Point abs(Point const &b);
211 
212 Point operator*(Point const &v, Matrix const &m);
213 
214 Point operator/(Point const &p, Matrix const &m);
215 
216 } /* namespace Geom */
217 
218 #endif /* !SEEN_Geom_POINT_H */
219 
220 /*
221  Local Variables:
222  mode:c++
223  c-file-style:"stroustrup"
224  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
225  indent-tabs-mode:nil
226  fill-column:99
227  End:
228 */
229 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
Definition: angle.h:38
double decimal_round(double const x, int const places)
Definition: utils.h:73
Point ccw() const
Definition: point.h:58
double Coord
Definition: coord.h:45
Point unit_vector(Point const &a)
Definition: point.cpp:123
Coord angle_between(Point const a, Point const b)
Definition: point.cpp:108
bool operator<=(Point const &a, Point const &b)
Definition: point.h:156
void normalize()
Definition: point.cpp:20
friend std::ostream & operator<<(std::ostream &out_file, const Geom::Point &in_pnt)
Definition: point.h:135
Coord LInfty(Point const &p)
Definition: point.cpp:73
Coord distance(Point const &a, Point const &b)
Definition: point.h:205
void round(int places=0)
A function to lower the precision of the point.
Definition: point.h:75
Point operator^(Point const &a, Point const &b)
Definition: point.h:141
Cartesian point.
Definition: point.h:20
Coord L2sq(Point const &p)
Definition: point.h:167
Coord distanceSq(Point const &a, Point const &b)
Definition: point.h:208
bool is_zero(Point const &p)
Definition: point.cpp:86
Point cw() const
Definition: point.h:66
Coord L1(Point const &p)
Definition: point.cpp:64