SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Rect.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 
6 #include <Framework/IShape.h>
7 #include <SDL.h>
8 
9 namespace SDX
10 {
11  class Complex;
12  class Point;
13  class Line;
14  class Circle;
15  class Rect;
16 
17  class Camera;
18  class Color;
19 
22  class Rect : public IShape
23  {
24  public:
25  double x = 0;
26  double y = 0;
27 
28  double widthLeft = 0;
29  double widthRight = 0;
30 
31  double heightUp = 0;
32  double heightDown = 0;
33 
34  Rect() = default;
35 
39  Rect(double X座標, double Y座標, double 横幅A, double 高さA, double 横幅B = 0, double 高さB = 0) :
40  x(X座標),
41  y(Y座標),
42  heightUp(高さB),
43  heightDown(高さA),
44  widthLeft(横幅B),
45  widthRight(横幅A)
46  {}
47 
48  template <class T1,class T2,class T3,class T4>
52  Rect(T1 X座標, T2 Y座標, T3 横幅A, T4 高さA, T3 横幅B = 0, T4 高さB = 0) :
53  x((double)X座標),
54  y((double)Y座標),
55  heightUp((double)高さB),
56  heightDown((double)高さA),
57  widthLeft((double)横幅B),
58  widthRight((double)横幅A)
59  {}
60 
61  virtual IShape* Clone(double X座標, double Y座標) const override
62  {
63  auto shape = new Rect(X座標, Y座標, widthRight, heightDown, widthLeft, heightUp);
64  shape->zoomX = this->zoomX;
65  shape->zoomY = this->zoomY;
66  return shape;
67  }
68 
69  void SetPos(double X座標, double Y座標) override
70  {
71  this->x = X座標;
72  this->y = Y座標;
73  }
74 
75  void Move(double X移動量, double Y移動量) override
76  {
77  this->x += X移動量;
78  this->y += Y移動量;
79  }
80 
81  void MultiZoom(double X倍率, double Y倍率) override
82  {
83  widthLeft *= X倍率;
84  widthRight *= X倍率;
85 
86  heightUp *= Y倍率;
87  heightDown *= Y倍率;
88  }
89 
90  void Rotate(double 回転する角度) override
91  {
92 
93  }
94 
95  void Draw(const Color &描画色) const override;
96 
97  inline double GetX() const override
98  {
99  return x;
100  }
101 
102  inline double GetY() const override
103  {
104  return y;
105  }
106 
107  inline double GetW() const override
108  {
109  return widthLeft + widthRight;
110  }
111 
112  inline double GetH() const override
113  {
114  return heightUp + heightDown;
115  }
116 
118  inline double GetLeft() const
119  {
120  return x - widthLeft;
121  }
122 
124  inline double GetTop() const
125  {
126  return y - heightUp;
127  }
128 
130  inline double GetRight() const
131  {
132  return x + widthRight;
133  }
134 
136  inline double GetBottom() const
137  {
138  return y + heightDown;
139  }
140 
141  bool Hit(const IShape *shape) const override
142  {
143  return shape->Hit(this);
144  }
145 
146  bool Hit(const Complex *complex) const override
147  {
148  for (auto it : complex->shapes)
149  {
150  if (it->Hit(this)) return true;
151  }
152  return false;
153  }
154  bool Hit(const Point *point) const override
155  {
156  return (
157  (
158  point->x < this->GetRight()
159  ) && (
160  point->x > this->GetLeft()
161  ) && (
162  point->y < this->GetBottom()
163  ) && (
164  point->y > this->GetTop()
165  )
166  );
167  }
168  bool Hit(const Line *line) const override
169  {
170  //完全に外部にある
171  if (
172  line->GetMaxX() + line->GetThickHarf() < this->GetLeft() ||
173  line->GetMinX() - line->GetThickHarf() > this->GetRight() ||
174  line->GetMaxY() + line->GetThickHarf() < this->GetTop() ||
175  line->GetMinY() - line->GetThickHarf() > this->GetBottom()
176  )
177  {
178  return false;
179  }
180 
181  //四角形の対角線との交差チェック
182  //√2/2≒0.7
183  if (LineLine(line->GetXA(), line->GetYA(), line->GetXB(), line->GetYB() , this->GetLeft() - int(line->GetThickHarf() * 0.7), this->GetTop() - int(line->GetThickHarf() * 0.7), this->GetRight() + int(line->GetThickHarf() * 0.7), this->GetBottom() + int(line->GetThickHarf() * 0.7)))
184  {
185  return true;
186  }
187  if (LineLine(line->GetXA(), line->GetYA(), line->GetXB(), line->GetYB() , this->GetRight() + int(line->GetThickHarf() * 0.7), this->GetTop() - int(line->GetThickHarf() * 0.7), this->GetLeft() - int(line->GetThickHarf() * 0.7), this->GetBottom() + int(line->GetThickHarf() * 0.7)))
188  {
189  return true;
190  }
191 
192  //端が四角内にあるか判定
193  if (
194  line->GetXA() + line->GetThickHarf() > this->GetLeft() && line->GetXA() - line->GetThickHarf() < this->GetRight() &&
195  line->GetYA() + line->GetThickHarf() > this->GetTop() && line->GetYA() - line->GetThickHarf() < this->GetBottom()
196  )
197  {
198  //端の丸い所判定
199  if (line->GetXA() < this->GetLeft() && line->GetYA() < this->GetTop())
200  {//左上
201  if ((line->GetXA() - this->GetLeft()) * (line->GetXA() - this->GetLeft()) +
202  (line->GetYA() - this->GetTop()) * (line->GetYA() - this->GetTop()) < line->GetThickPow()) return true;
203  }
204  else if (line->GetXA() > this->GetRight() && line->GetYA() < this->GetTop())
205  {//右上
206  if ((line->GetXA() - this->GetRight()) * (line->GetXA() - this->GetRight()) +
207  (line->GetYA() - this->GetTop()) * (line->GetYA() - this->GetTop()) < line->GetThickPow()) return true;
208  }
209  else if (line->GetXA() < this->GetLeft() && line->GetYA() > this->GetBottom())
210  {//左下
211  if ((line->GetXA() - this->GetLeft()) * (line->GetXA() - this->GetLeft()) +
212  (line->GetYA() - this->GetBottom()) * (line->GetYA() - this->GetBottom()) < line->GetThickPow()) return true;
213  }
214  else if (line->GetXA() > this->GetRight() && line->GetYA() > this->GetBottom())
215  {//右下
216  if ((line->GetXA() - this->GetRight()) * (line->GetXA() - this->GetRight()) +
217  (line->GetYA() - this->GetBottom()) * (line->GetYA() - this->GetBottom()) < line->GetThickPow()) return true;
218  }
219  else
220  {
221  return true;
222  }
223  }
224 
225 
226  //端が四角内にあるか判定
227  if (
228  line->GetXB() + line->GetThickHarf() > this->GetLeft() && line->GetXB() - line->GetThickHarf() < this->GetRight() &&
229  line->GetYB() + line->GetThickHarf() > this->GetTop() && line->GetYB() - line->GetThickHarf() < this->GetBottom()
230  )
231  {
232  //端の丸い所判定
233  if (line->GetXB() < this->GetLeft() && line->GetYB() < this->GetTop())
234  {//左上
235  if ((line->GetXB() - this->GetLeft()) * (line->GetXB() - this->GetLeft()) +
236  (line->GetYB() - this->GetTop()) * (line->GetYB() - this->GetTop()) < line->GetThickPow()) return true;
237  }
238  else if (line->GetXB() > this->GetRight() && line->GetYB() < this->GetTop())
239  {//右上
240  if ((line->GetXB() - this->GetRight()) * (line->GetXB() - this->GetRight()) +
241  (line->GetYB() - this->GetTop()) * (line->GetYB() - this->GetTop()) < line->GetThickPow()) return true;
242  }
243  else if (line->GetXB() < this->GetLeft() && line->GetYB() > this->GetBottom())
244  {//左下
245  if ((line->GetXB() - this->GetLeft()) * (line->GetXB() - this->GetLeft()) +
246  (line->GetYB() - this->GetBottom()) * (line->GetYB() - this->GetBottom()) < line->GetThickPow()) return true;
247  }
248  else if (line->GetXB() > this->GetRight() && line->GetYB() > this->GetBottom())
249  {//右下
250  if ((line->GetXB() - this->GetRight()) * (line->GetXB() - this->GetRight()) +
251  (line->GetYB() - this->GetBottom()) * (line->GetYB() - this->GetBottom()) < line->GetThickPow()) return true;
252  }
253  else
254  {
255  return true;
256  }
257  }
258 
259  return false;
260  }
261  bool Hit(const Rect *rect) const override
262  {
263  return !(GetRight() < rect->GetLeft() || GetLeft() > rect->GetRight() || GetBottom() < rect->GetTop() || GetTop() > rect->GetBottom());
264  }
265  bool Hit(const Circle *circle) const override;
266 
268  Point GetPoint() const
269  {
270  return { x , y};
271  }
272 
273  Point GetCenter() const
274  {
275  return{ x + (widthRight - widthLeft) / 2, y + (heightDown - heightUp) / 2 };
276  }
277 
279  operator SDL_Rect() const
280  {
281  return{ (int)GetLeft(), (int)GetTop(), (int)GetW(), (int)GetH() };
282  }
283 
285  Rect operator +(Point &加算値) const
286  {
287  return {this->x + 加算値.x,this->y + 加算値.y ,widthRight , heightDown , widthLeft , heightUp};
288  }
289  };
290 }
double GetX() const override
X座標を取得.
Definition: Rect.h:97
double GetThickHarf() const
太さの半分を取得.
Definition: Line.h:208
double y
起点座標
Definition: Rect.h:26
Rect(T1 X座標, T2 Y座標, T3 横幅A, T4 高さA, T3 横幅B=0, T4 高さB=0)
座標と大きさを指定.
Definition: Rect.h:52
double GetYB() const
頂点BのY座標を取得.
Definition: Line.h:176
矩形を表す図形クラス.
Definition: Rect.h:22
double y
座標
Definition: Point.h:26
太さのある線を表す図形クラス.
Definition: Line.h:20
double GetBottom() const
下端のY座標を取得.
Definition: Rect.h:136
double widthRight
起点から右側の幅
Definition: Rect.h:29
double GetTop() const
上端のY座標を取得.
Definition: Rect.h:124
double GetY() const override
Y座標を取得.
Definition: Rect.h:102
double GetRight() const
右端のX座標を取得.
Definition: Rect.h:130
double GetMaxY() const
頂点AとBのY座標の大きい値を取得.
Definition: Line.h:238
void SetPos(double X座標, double Y座標) override
指定座標に移動.
Definition: Rect.h:69
double GetLeft() const
左端のX座標を取得.
Definition: Rect.h:118
std::vector< IShape * > shapes
保持するShape
Definition: Complex.h:23
Rect(double X座標, double Y座標, double 横幅A, double 高さA, double 横幅B=0, double 高さB=0)
座標と大きさを指定.
Definition: Rect.h:39
点を表す図形クラス.
Definition: Point.h:22
衝突判定可能な図形の抽象クラス.
Definition: IShape.h:21
virtual bool Hit(const IShape *iShape) const =0
衝突判定.
複合図形を表すクラス.
Definition: Complex.h:20
bool Hit(const Point *point) const override
衝突判定.
Definition: Rect.h:154
Point GetPoint() const
Pointを取得.
Definition: Rect.h:268
void Move(double X移動量, double Y移動量) override
相対座標で移動.
Definition: Rect.h:75
bool Hit(const Rect *rect) const override
衝突判定.
Definition: Rect.h:261
void Draw(const Color &描画色) const override
描画する.
Definition: ShapeDraw.h:33
double x
起点座標
Definition: Rect.h:25
bool Hit(const Line *line) const override
衝突判定.
Definition: Rect.h:168
double GetMinX() const
頂点AとBのX座標の小さい値を取得.
Definition: Line.h:220
色を表すクラス.
Definition: Color.h:11
double widthLeft
起点から左側の幅
Definition: Rect.h:28
double GetMinY() const
頂点AとBのY座標の小さい値を取得.
Definition: Line.h:226
static bool LineLine(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
線分の交差判定.
Definition: IShape.h:62
void Rotate(double 回転する角度) override
回転する.
Definition: Rect.h:90
Rect operator+(Point &加算値) const
座標に加算.
Definition: Rect.h:285
void MultiZoom(double X倍率, double Y倍率) override
縦横別で拡大率を掛け算する.
Definition: Rect.h:81
bool Hit(const IShape *shape) const override
衝突判定.
Definition: Rect.h:141
double GetXB() const
頂点BのX座標を取得.
Definition: Line.h:170
double zoomX
図形の拡大率
Definition: IPosition.h:14
double x
座標
Definition: Point.h:25
virtual IShape * Clone(double X座標, double Y座標) const override
同じ形の図形を作る.
Definition: Rect.h:61
bool Hit(const Complex *complex) const override
衝突判定.
Definition: Rect.h:146
double heightUp
起点から上側の幅
Definition: Rect.h:31
double GetYA() const
頂点AのY座標を取得.
Definition: Line.h:164
double GetXA() const
頂点AのX座標を取得.
Definition: Line.h:158
double GetMaxX() const
頂点AとBのX座標の大きい値を取得.
Definition: Line.h:232
円を表す図形クラス.
Definition: Circle.h:20
double GetW() const override
幅を取得.
Definition: Rect.h:107
double zoomY
図形の拡大率
Definition: IPosition.h:15
double GetH() const override
高さを取得.
Definition: Rect.h:112
double GetThickPow() const
太さの二乗を取得.
Definition: Line.h:214
double heightDown
起点から下側の幅
Definition: Rect.h:32