SDL Window Engine  20200905
swe_rect.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_RECT_
24 #define _SWE_RECT_
25 
26 #include <string>
27 #include <initializer_list>
28 
29 #include "swe_types.h"
30 
32 namespace SWE
33 {
34 
36  struct Size
37  {
38  int w, h;
39 
40  Size() : w(0), h(0) {}
41  Size(int sw, int sh) : w(sw), h(sh) {}
42  Size(std::initializer_list<int>);
43 
44  static Size parse(const std::string &, int sep = 'x');
45 
46  bool isEmpty(void) const;
47 
48  bool operator== (const Size &) const;
49  bool operator!= (const Size &) const;
50  bool operator<= (const Size &) const;
51  bool operator>= (const Size &) const;
52 
53  Size & operator+= (const Size &);
54  Size & operator-= (const Size &);
55 
56  Size operator+ (const Size &) const;
57  Size operator- (const Size &) const;
58 
59  Size operator* (const Size &) const;
60  Size operator/ (const Size &) const;
61  Size operator* (int) const;
62  Size operator/ (int) const;
63 
64  bool operator< (const Size &) const;
65  bool operator> (const Size &) const;
66 
67  Size swap(void) const { return Size(h, w); }
68  std::string toString(void) const;
69  };
70 
72  struct Point
73  {
74  int x, y;
75 
76  Point() : x(0), y(0) {}
77  Point(const Size & sz) : x(sz.w), y(sz.h) {}
78  Point(int px, int py) : x(px), y(py) {}
79  Point(std::initializer_list<int>);
80 
81  static Point parse(const std::string &, int sep = ':');
82 
83  bool isNull(void) const;
84 
89  bool inABC(const Point & A, const Point & B, const Point & C) const;
90 
91  bool operator== (const Point &) const;
92  bool operator!= (const Point &) const;
93 
94  Point & operator+= (const Point &);
95  Point & operator-= (const Point &);
96 
97  Point operator+ (const Point &) const;
98  Point operator- (const Point &) const;
99 
100  Point operator* (const Point &) const;
101  Point operator/ (const Point &) const;
102  Point operator* (int) const;
103  Point operator/ (int) const;
104 
105  Point swap(void) const { return Point(y, x); }
106  Size toSize(void) const { return Size(x, y); }
107 
108  std::string toString(void) const;
109 
111  static int distance(const Point &, const Point &);
112  };
113 
115  struct ZPoint : public Point
116  {
117  int z;
118 
119  ZPoint() : z(0) {}
120  ZPoint(int px, int py, int pz) : Point(px, py), z(pz) {}
121  ZPoint(const Point & pt, int pz) : Point(pt), z(pz) {}
122  ZPoint(std::initializer_list<int>);
123 
124  static ZPoint parse(const std::string &, int sep = ':');
125 
126  bool operator== (const ZPoint &) const;
127  bool operator!= (const ZPoint &) const;
128 
129  ZPoint & operator+= (const ZPoint &);
130  ZPoint & operator-= (const ZPoint &);
131 
132  ZPoint operator+ (const ZPoint &) const;
133  ZPoint operator- (const ZPoint &) const;
134 
135  ZPoint operator* (const ZPoint &) const;
136  ZPoint operator/ (const ZPoint &) const;
137  ZPoint operator* (int) const;
138  ZPoint operator/ (int) const;
139 
140  std::string toString(void) const;
141  };
142 
144  struct Rect : public Point, public Size
145  {
146  Rect() {}
147  Rect(int, int, int, int);
148  Rect(const Point &, const Size &);
149  Rect(const Point &, const Point &);
150  Rect(const SDL_Rect &);
151  Rect(std::initializer_list<int>);
152 
153  bool operator== (const Rect &) const;
154  bool operator!= (const Rect &) const;
155 
156  Rect operator+ (const Point &) const;
157  Rect operator- (const Point &) const;
158  Rect operator+ (const Size &) const;
159  Rect operator- (const Size &) const;
160  Rect operator+ (const Rect &) const;
161  Rect operator- (const Rect &) const;
162 
163  Point topLeft(void) const;
164  Point topRight(void) const;
165  Point bottomLeft(void) const;
166  Point bottomRight(void) const;
167 
168  void setPoint(const Point &);
169  void setSize(const Size &);
170  Rect swapSize(void) const { return Rect(x, y, h, w); }
171 
172  Point toPoint(void) const;
173  Size toSize(void) const;
174 
175  SDL_Rect toSDLRect(void) const;
176 
177  Rect intersected(const Rect &) const;
178  bool intersects(const Rect &) const;
179  bool contains(const Point &) const;
180  bool contains(const Rect &) const;
181 
183  bool operator& (const Point &) const;
185  bool operator& (const Rect &) const;
186 
188  static bool intersection(const Rect &, const Rect &, Rect* res = nullptr);
189  static Rect around(const Rect &, const Rect &);
190  std::string toString(void) const;
191  };
192 
194  struct Points : std::vector<Point>
195  {
196  Points() {}
197  Points(const std::vector<Point> & v) : std::vector<Point>(v) {}
198  Points(const Points & v) : std::vector<Point>(v) {}
199  Points(Points && v) noexcept
200  {
201  swap(v);
202  }
203 
204  Points & operator= (const Points & v)
205  {
206  assign(v.begin(), v.end());
207  return *this;
208  }
209  Points & operator= (Points && v) noexcept
210  {
211  swap(v);
212  return *this;
213  }
214 
215  Rect around(void) const;
216 
217  Points & push_back(const Point &);
218  Points & push_back(const Points &);
219 
220  Points & operator<< (const Point &);
221  Points & operator<< (const Points &);
222 
223  std::string toString(void) const;
224  };
225 
226  struct Rects : std::vector<Rect>
227  {
228  Rects() {}
229  Rects(const std::vector<Rect> & v) : std::vector<Rect>(v) {}
230  Rects(const Rects & v) : std::vector<Rect>(v) {}
231  Rects(Rects && v) noexcept
232  {
233  swap(v);
234  }
235 
236  Rects & operator= (const Rects & v)
237  {
238  assign(v.begin(), v.end());
239  return *this;
240  }
241  Rects & operator= (Rects && v) noexcept
242  {
243  swap(v);
244  return *this;
245  }
246 
247  int index(const Point &) const;
248  Rect around(void) const;
249 
250  Rects & push_back(const Rect &);
251  Rects & push_back(const Rects &);
252 
253  Rects & operator<< (const Rect &);
254  Rects & operator<< (const Rects &);
255 
256  std::string toString(void) const;
257  };
258 
259  struct Polygon : Points
260  {
261  Polygon() {}
262  Polygon(const Points &);
263 
264  bool operator& (const Point &) const;
265  };
266 
267  Point operator+ (const Point &, const Size &);
268  Point operator- (const Point &, const Size &);
269  Point operator* (const Point &, const Size &);
270  Point operator/ (const Point &, const Size &);
271 
272 } // SWE
273 #endif
static bool intersection(const Rect &, const Rect &, Rect *res=nullptr)
функция проверки пересечения Rect c Rect.
Definition: swe_rect.cpp:552
пространство SWE.
Definition: swe_binarybuf.cpp:30
класс прямоугольника
Definition: swe_rect.h:144
Definition: swe_rect.h:259
bool inABC(const Point &A, const Point &B, const Point &C) const
функция определения включения объекта Point в треугольник
Definition: swe_rect.cpp:72
класс точки с двумя координатами
Definition: swe_rect.h:72
класс последовательности точек
Definition: swe_rect.h:194
bool operator &(const Point &) const
функция проверки включения Point в Rect.
класс точки с тремя координатами
Definition: swe_rect.h:115
static int distance(const Point &, const Point &)
функция определения расстояния по двум точкам
Definition: swe_rect.cpp:135
класс двухмерной размерности
Definition: swe_rect.h:36
Definition: swe_rect.h:226