teditor  1.8.0@@fee5e94
Terminal based editor written in C++
pos2d.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "utils.h"
4 
5 
6 namespace teditor {
7 
12 template <typename T>
13 struct Pos2d {
14  typedef T DataT;
15 
16  T x, y;
17 
18  Pos2d(T x_=0, T y_=0): x(x_), y(y_) {}
19 
20  const Pos2d<T>& operator=(const Pos2d<T>& in) {
21  x = in.x;
22  y = in.y;
23  return *this;
24  }
25 
26  bool operator>(const Pos2d<T>& b) const {
27  return (y > b.y) || (y == b.y && x > b.x);
28  }
29 
30  bool operator>=(const Pos2d<T>& b) const {
31  return (y > b.y) || (y == b.y && x >= b.x);
32  }
33 
34  bool operator==(const Pos2d<T>& b) const {
35  return (x == b.x) && (y == b.y);
36  }
37 
38  bool operator!=(const Pos2d<T>& b) const {
39  return (x != b.x) || (y != b.y);
40  }
41 
42  bool operator<=(const Pos2d<T>& b) const {
43  return (y < b.y) || (y == b.y && x <= b.x);
44  }
45 
46  bool operator<(const Pos2d<T>& b) const {
47  return (y < b.y) || (y == b.y && x < b.x);
48  }
49 
59  int find(Pos2d<T>& start, Pos2d<T>& end, const Pos2d<T>& other) const {
60  if(y < other.y) {
61  start = *this;
62  end = other;
63  return -1;
64  } else if(y > other.y) {
65  start = other;
66  end = *this;
67  return 1;
68  } else {
69  start = {std::min(x, other.x), y};
70  end = {std::max(x, other.x), y};
71  return 0;
72  }
73  }
74 
76  Pos2d& operator+=(const std::string& chars) {
77  for(const auto& c : chars) {
78  if(c == '\n') {
79  ++y;
80  x = 0;
81  } else {
82  ++x;
83  }
84  }
85  return *this;
86  }
87 
91  bool isInside(int _y, int _x, const Pos2d<T>& cu) const {
92  if (y == -1 && x == -1) return false;
93  Pos2d<T> start, end;
94  find(start, end, cu);
95  if(start.y < _y && _y < end.y) return true;
96  else if(start.y == end.y && start.y == _y) {
97  if(start.x <= _x && _x <= end.x) return true;
98  } else if(start.y == _y && _x >= start.x) return true;
99  else if(end.y == _y && _x <= end.x) return true;
100  return false;
101  }
102 };
103 
104 
112 typedef Pos2di Point;
113 
114 } // end namespace teditor
teditor::Pos2d::operator==
bool operator==(const Pos2d< T > &b) const
Definition: pos2d.h:34
teditor::Pos2d::operator<=
bool operator<=(const Pos2d< T > &b) const
Definition: pos2d.h:42
teditor::Pos2d::operator=
const Pos2d< T > & operator=(const Pos2d< T > &in)
Definition: pos2d.h:20
teditor::Pos2d::operator<
bool operator<(const Pos2d< T > &b) const
Definition: pos2d.h:46
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::Pos2ds
Pos2d< size_t > Pos2ds
Definition: pos2d.h:110
utils.h
teditor::Pos2d::DataT
T DataT
Definition: pos2d.h:14
teditor::Pos2di
Pos2d< int > Pos2di
Definition: pos2d.h:106
teditor::Pos2d
Any 2D position.
Definition: pos2d.h:13
teditor::Pos2d::find
int find(Pos2d< T > &start, Pos2d< T > &end, const Pos2d< T > &other) const
Finds the start/end points for the range defined by this and the other position.
Definition: pos2d.h:59
teditor::Pos2d::operator>=
bool operator>=(const Pos2d< T > &b) const
Definition: pos2d.h:30
teditor::Pos2d::isInside
bool isInside(int _y, int _x, const Pos2d< T > &cu) const
Check if the given location is in the regions.
Definition: pos2d.h:91
teditor::Pos2d::Pos2d
Pos2d(T x_=0, T y_=0)
Definition: pos2d.h:18
teditor::Pos2d::operator!=
bool operator!=(const Pos2d< T > &b) const
Definition: pos2d.h:38
teditor::Point
Pos2di Point
Definition: pos2d.h:112
teditor::Pos2du
Pos2d< unsigned > Pos2du
Definition: pos2d.h:108
teditor::Pos2d::x
T x
Definition: pos2d.h:16
teditor::Pos2d::operator+=
Pos2d & operator+=(const std::string &chars)
Definition: pos2d.h:76
teditor::Pos2d::operator>
bool operator>(const Pos2d< T > &b) const
Definition: pos2d.h:26
teditor
Definition: any.hpp:10