MLIB
Loading...
Searching...
No Matches
point.h
Go to the documentation of this file.
1
7#pragma once
8
9#if __has_include("defs.h")
10#include "defs.h"
11#endif
12
13#define _USE_MATH_DEFINES
14#include <math.h>
15
16#include <ostream>
17
18namespace mlib {
19
29template <typename T>
31{
32 static T tolerance ()
33 {
34 return 1e-7;
35 };
36};
37
38template <>
39struct point_traits<int>
40{
41 static int tolerance ()
42 {
43 return 0;
44 };
45};
46
48template <typename T>
49class Point
50{
51public:
52 typedef point_traits<T> traits;
53 T x, y;
54
55 Point (T a, T b);
57
58 double azimuth (const Point<T>& P2) const;
59 double angle (const Point<T>& P1, const Point<T>& P2) const;
60
61 Point<T> operator+ (const Point<T>& p) const
62 {
63 return {x + p.x, y + p.y};
64 }
65 Point<T> operator- (const Point<T>& p) const
66 {
67 return {x - p.x, y - p.y};
68 }
69 Point<T> operator* (double scalar) const
70 {
71 return {x * scalar, y * scalar};
72 }
73 Point<T> operator/ (double scalar) const
74 {
75 return {x / scalar, y / scalar};
76 }
77
80 {
81 return {-x, -y};
82 }
83
85 T operator* (const Point<T>& p) const
86 {
87 return x * p.x + y * p.y;
88 }
89
90 Point<T>& operator+= (const Point<T>& other)
91 {
92 x += other.x;
93 y += other.y;
94 return *this;
95 }
96 Point<T>& operator-= (const Point<T>& other)
97 {
98 x -= other.x;
99 y -= other.y;
100 return *this;
101 }
102 Point<T>& operator*= (double scalar)
103 {
104 x *= scalar;
105 y *= scalar;
106 return *this;
107 }
108 Point<T>& operator/= (double scalar)
109 {
110 x /= scalar;
111 y /= scalar;
112 return *this;
113 }
114
115 double distance (const Point<T>& P2) const;
116 double magnitude () const
117 {
118 return hypot (x, y);
119 }
120 int operator== (const Point<T>& p) const;
121 int operator!= (const Point<T>& p) const;
122
123 bool leftof (const Point<T>& a, const Point<T>& b) const;
124 bool collinear (const Point<T>& a, const Point<T>& b) const;
125 void rotate (double angle);
126};
127
129template <class T>
130Point<T> operator* (double scalar, const Point<T>& p)
131{
132 return {p.x * scalar, p.y * scalar};
133}
134
136template <class T>
137double abs (const Point<T>& p)
138{
139 return p.magnitude ();
140}
141
144template <class T>
145std::ostream& operator<< (std::ostream& s, const Point<T>& p)
146{
147 s << '(' << p.x << ',' << p.y << ')';
148 return s;
149}
150
153
154// Member functions templates --------------------------------------------------
155
157template <class T>
159 : x (a)
160 , y (b)
161{}
162
164template <class T>
166 : x (0)
167 , y (0)
168{}
169
175template <class T>
176double Point<T>::azimuth (const Point<T>& P2) const
177{
178 if (P2 == *this)
179 return 0.;
180 else
181 {
182 double t = atan2 (P2.x - x, P2.y - y);
183 return (t > 0) ? t : 2 * M_PI + t;
184 }
185}
186
188template <class T>
190{
191 return distance (p) < point_traits<T>::tolerance ();
192}
193
195template <class T>
197{
198 return !(p == *this);
199}
200
202template <class T>
203double Point<T>::distance (const Point<T>& P2) const
204{
205 return hypot (x - P2.x, y - P2.y);
206}
207
214template <class T>
215double Point<T>::angle (const Point<T>& P1, const Point<T>& P2) const
216{
217 if (P1 == *this || P2 == *this)
218 return 0.;
219
220 double cang =
221 ((P1.x - x) * (P2.x - x) + (P1.y - y) * (P2.y - y)) / (distance (P1) * distance (P2));
222 return (cang <= -1) ? M_PI : (cang >= 1) ? 0. : acos (cang);
223}
224
226template <class T>
227bool Point<T>::leftof (const Point<T>& a, const Point<T>& b) const
228{
229 return (a.x - x) * (b.y - y) - (a.y - y) * (b.x - x) > point_traits<T>::tolerance ();
230}
231
233template <class T>
234bool Point<T>::collinear (const Point& a, const Point& b) const
235{
236 return ::abs ((a.x - x) * (b.y - y) - (a.y - y) * (b.x - x)) <= point_traits<T>::tolerance ();
237}
238
239template <class T>
240void Point<T>::rotate (double angle)
241{
242 double c, s;
243 double x1 = x * (c = cos (angle)) - y * (s = sin (angle));
244 double y1 = x * s + y * c;
245 x = x1;
246 y = y1;
247}
248
249} // namespace mlib
Generic 2D point.
Definition point.h:50
Point()
Default constructor.
Definition point.h:165
bool collinear(const Point< T > &a, const Point< T > &b) const
Return true if points a, this, b are collinear.
Definition point.h:234
int operator==(const Point< T > &p) const
Return TRUE if p and this are closer than tolerance.
Definition point.h:189
Point< T > operator-() const
unary minus operator
Definition point.h:79
double distance(const Point< T > &P2) const
Return distance between this and P2.
Definition point.h:203
Point(T a, T b)
Build a Point from a pair of T's.
Definition point.h:158
bool leftof(const Point< T > &a, const Point< T > &b) const
Return true if this is left of the line (a,b)
Definition point.h:227
double azimuth(const Point< T > &P2) const
Return azimuth from North of line this-P2.
Definition point.h:176
double angle(const Point< T > &P1, const Point< T > &P2) const
Return inside angle P1-this-P2.
Definition point.h:215
int operator!=(const Point< T > &p) const
Return TRUE if p and this apart by more than tolerance.
Definition point.h:196
double abs(const Point< T > &p)
Alias for magnitude function.
Definition point.h:137
Definition point.h:31