The spica renderer
point2d_detail.h
1 #include "common.h"
2 
3 #include <sstream>
4 #include <iomanip>
5 
6 namespace spica {
7 
8  template <class T>
9  Point2_<T>::Point2_()
10  : x_{ 0 }
11  , y_{ 0 } {
12  }
13 
14  template <class T>
15  Point2_<T>::Point2_(T x, T y)
16  : x_{ x }
17  , y_{ y } {
18  }
19 
20  template <class T>
21  Point2_<T>::Point2_(const Point2_<T>& p)
22  : x_{ p.x_ }
23  , y_{ p.y_ } {
24  }
25 
26  template <class T>
27  Point2_<T>& Point2_<T>::operator=(const Point2_<T>& p) {
28  this->x_ = p.x_;
29  this->y_ = p.y_;
30  return *this;
31  }
32 
33  template <class T>
34  Point2_<T> Point2_<T>::operator-() const {
35  return { -x_, -y_ };
36  }
37 
38  template <class T>
39  Point2_<T>& Point2_<T>::operator+=(const Point2_<T>& p) {
40  this->x_ += p.x_;
41  this->y_ += p.y_;
42  return *this;
43  }
44 
45  template <class T>
46  Point2_<T>& Point2_<T>::operator-=(const Point2_<T>& p) {
47  this->x_ -= p.x_;
48  this->y_ -= p.y_;
49  return *this;
50  }
51 
52  template <class T>
53  T Point2_<T>::operator[](int i) const {
54  Assertion(i >= 0 && i <= 1, "Index out of bounds!!");
55  if (i == 0) {
56  return x_;
57  } else {
58  return y_;
59  }
60  }
61 
62  template <class T>
63  Point2_<T>& Point2_<T>::operator*=(T s) {
64  this->x_ *= s;
65  this->y_ *= s;
66  return *this;
67  }
68 
69  template <class T>
70  std::string Point2_<T>::toString() const {
71  std::stringstream ss;
72  ss << std::fixed;
73  ss << std::setprecision(8);
74  ss << "(" << x_ << ", " << y_ << ")";
75  return std::move(ss.str());
76  }
77 
78 } // namespace spica
79 
80 template <class T>
81 std::ostream& operator<<(std::ostream& os, const spica::Point2_<T>& p) {
82  os << p.toString();
83  return os;
84 }
85 
86 template <class T>
87 spica::Point2_<T> operator+(const spica::Point2_<T>& p1, const spica::Point2_<T>& p2) {
88  spica::Point2_<T> ret = p1;
89  ret += p2;
90  return ret;
91 }
92 
93 template <class T>
94 spica::Point2_<T> operator-(const spica::Point2_<T>& p1, const spica::Point2_<T>& p2) {
95  spica::Point2_<T> ret = p1;
96  ret -= p2;
97  return ret;
98 }
99 
100 template <class T>
101 spica::Point2_<T> operator*(const spica::Point2_<T>& p, T s) {
102  spica::Point2_<T> ret = p;
103  ret *= s;
104  return ret;
105 }
106 
107 template <class T>
108 spica::Point2_<T> operator*(T s, const spica::Point2_<T>& p) {
109  spica::Point2_<T> ret = p;
110  ret *= s;
111  return ret;
112 }
Definition: core.hpp:62