pixled-lib  1.0
geometry.h
1 #ifndef PIXLED_GEOMETRY_H
2 #define PIXLED_GEOMETRY_H
3 
4 #include <functional>
5 #include <cmath>
6 #include <iostream>
7 #include <vector>
8 
9 namespace pixled {
13  static const float PI = 3.14159265358979323846f;
14 
18  typedef float coordinate;
19 
23  struct point {
37  point() : point(0, 0) {}
38 
46  : x(x), y(y) {}
47  };
48 
52  class angle {
53  private:
54  friend angle operator+(const angle&, const angle&);
55  friend angle operator-(const angle&, const angle&);
56 
57  // value in radian
58  float value;
59  angle(float value) : value(value) {}
60 
61  public:
62 
68  static angle fromRad(float value);
74  static angle fromDeg(float value);
75 
81  float toRad() const {
82  return value;
83  }
84 
90  float toDeg() const {
91  return value * 180.f / PI;
92  }
93 
100  angle& operator+=(const angle& a) {
101  value+=a.value;
102  return *this;
103  }
104 
111  angle& operator-=(const angle& a) {
112  value-=a.value;
113  return *this;
114  }
115  };
116 
122  angle operator+(const angle& a, const angle& b);
123 
129  angle operator-(const angle& a, const angle& b);
130 
134  struct line {
155  : a(a), b(b), c(c) {}
156 
161  line(point p, angle a);
165  line(point p0, point p1);
166  };
167 
171  struct point_hash {
185  std::size_t operator()(const point& p) const;
186  };
187 
194  bool operator==(const point& p1, const point& p2);
195 
199  struct point_equal {
205  bool operator()(const point& p1, const point& p2) const;
206  };
207 
211  std::ostream& operator<<(std::ostream& o, const point& p);
212 
220  float distance(const point& p1, const point& p2);
227  float cos(const angle& a);
234  float sin(const angle& a);
241  float tan(const angle& a);
242 
243 
247  class box {
248  protected:
266  box();
267 
268  public:
277 
281  point position() const {return _position;}
285  coordinate width() const {return _width;}
289  coordinate height() const {return _height;}
290  };
291 
296  class bounding_box : public box {
297  private:
298  bool init = false;
299  public:
303  bounding_box();
309  bounding_box(std::vector<point> points);
310 
317  void stretchTo(point p);
318  };
319 
325  class spiral {
326  private:
327  float a;
328  float b;
329  point center;
330 
331  public:
339  spiral(float a, float b, point center)
340  : a(a), b(b), center(center) {}
341 
351  point operator()(angle theta);
352  };
353 }
354 
355 #endif
Definition: geometry.h:52
friend angle operator+(const angle &, const angle &)
Definition: geometry.cpp:15
friend angle operator-(const angle &, const angle &)
Definition: geometry.cpp:19
float toRad() const
Definition: geometry.h:81
static angle fromRad(float value)
Definition: geometry.cpp:8
float toDeg() const
Definition: geometry.h:90
static angle fromDeg(float value)
Definition: geometry.cpp:11
angle & operator-=(const angle &a)
Definition: geometry.h:111
angle & operator+=(const angle &a)
Definition: geometry.h:100
Definition: geometry.h:296
void stretchTo(point p)
Definition: geometry.cpp:80
bounding_box()
Definition: geometry.cpp:74
Definition: geometry.h:247
point position() const
Definition: geometry.h:281
box()
Definition: geometry.cpp:70
point _position
Definition: geometry.h:252
coordinate height() const
Definition: geometry.h:289
coordinate _width
Definition: geometry.h:256
coordinate width() const
Definition: geometry.h:285
coordinate _height
Definition: geometry.h:260
Definition: geometry.h:325
spiral(float a, float b, point center)
Definition: geometry.h:339
point operator()(angle theta)
Definition: geometry.cpp:102
Definition: animation.cpp:3
arithmetic::Minus< typename std::common_type< typename std::remove_reference< Arg1 >::type::Type, typename std::remove_reference< Arg2 >::type::Type >::type, typename std::remove_reference< Arg1 >::type::Type, typename std::remove_reference< Arg2 >::type::Type > operator-(Arg1 &&f1, Arg2 &&f2)
Definition: arithmetic.h:182
float tan(const angle &a)
Definition: geometry.cpp:65
float distance(const point &p1, const point &p2)
Definition: geometry.cpp:4
float coordinate
Definition: geometry.h:18
float sin(const angle &a)
Definition: geometry.cpp:61
std::ostream & operator<<(std::ostream &o, const point &p)
Definition: geometry.cpp:52
arithmetic::Plus< typename std::common_type< typename std::remove_reference< Arg1 >::type::Type, typename std::remove_reference< Arg2 >::type::Type >::type, typename std::remove_reference< Arg1 >::type::Type, typename std::remove_reference< Arg2 >::type::Type > operator+(Arg1 &&f1, Arg2 &&f2)
Definition: arithmetic.h:60
static const float PI
Definition: geometry.h:13
bool operator==(const color &c1, const color &c2)
Definition: color.cpp:181
float cos(const angle &a)
Definition: geometry.cpp:57
Definition: geometry.h:134
coordinate b
Definition: geometry.h:142
coordinate c
Definition: geometry.h:146
coordinate a
Definition: geometry.h:138
line(coordinate a, coordinate b, coordinate c)
Definition: geometry.h:154
Definition: geometry.h:199
bool operator()(const point &p1, const point &p2) const
Definition: geometry.cpp:48
Definition: geometry.h:171
std::size_t operator()(const point &p) const
Definition: geometry.cpp:38
Definition: geometry.h:23
coordinate x
Definition: geometry.h:27
point()
Definition: geometry.h:37
point(coordinate x, coordinate y)
Definition: geometry.h:45
coordinate y
Definition: geometry.h:31