SDXFrameWork 0.12 SDXFrameWork

- Main Page
- Related Pages
- Classes

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;
26 double y;
27
28 double widthLeft;
29 double widthRight;
30
31 double heightUp;
32 double heightDown;
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≒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
274 operator SDL_Rect() const
275 {
276 return{ (int)GetLeft(), (int)GetTop(), (int)GetW(), (int)GetH() };
277 }
278
280 Rect operator +(Point &加算値)
281 {
282 return {this->x + 加算値.x,this->y + 加算値.y ,widthRight , heightDown , widthLeft , heightUp};
283 }
284 };
285 }
SDX::Rect::GetX
double GetX() const override
X座標を取得.
Definition: Rect.h:97
SDX::Line::GetThickHarf
double GetThickHarf() const
太さの半分を取得.
Definition: Line.h:206
SDX::Rect::y
double y
起点座標
Definition: Rect.h:26
SDX::Rect::Rect
Rect(T1 X座標, T2 Y座標, T3 横幅A, T4 高さA, T3 横幅B=0, T4 高さB=0)
座標と大きさを指定.
Definition: Rect.h:52
SDX::Line::GetYB
double GetYB() const
頂点BのY座標を取得.
Definition: Line.h:174
SDX::Rect
矩形を表す図形クラス.
Definition: Rect.h:22
SDX::Point::y
double y
座標
Definition: Point.h:26
SDX::Line
太さのある線を表す図形クラス.
Definition: Line.h:20
SDX::Rect::GetBottom
double GetBottom() const
下端のY座標を取得.
Definition: Rect.h:136
SDX::Rect::widthRight
double widthRight
起点から右側の幅
Definition: Rect.h:29
SDX::Rect::GetTop
double GetTop() const
上端のY座標を取得.
Definition: Rect.h:124
SDX::Rect::GetY
double GetY() const override
Y座標を取得.
Definition: Rect.h:102
SDX::Rect::GetRight
double GetRight() const
右端のX座標を取得.
Definition: Rect.h:130
SDX::Line::GetMaxY
double GetMaxY() const
頂点AとBのY座標の大きい値を取得.
Definition: Line.h:236
SDX::Rect::SetPos
void SetPos(double X座標, double Y座標) override
指定座標に移動.
Definition: Rect.h:69
SDX::Rect::GetLeft
double GetLeft() const
左端のX座標を取得.
Definition: Rect.h:118
SDX::Complex::shapes
std::vector< IShape * > shapes
保持するShape
Definition: Complex.h:23
SDX::Rect::Rect
Rect(double X座標, double Y座標, double 横幅A, double 高さA, double 横幅B=0, double 高さB=0)
座標と大きさを指定.
Definition: Rect.h:39
SDX::Point
点を表す図形クラス.
Definition: Point.h:22
SDX::IShape
衝突判定可能な図形の抽象クラス.
Definition: IShape.h:21
SDX::IShape::Hit
virtual bool Hit(const IShape *iShape) const =0
衝突判定.
SDX::Complex
複合図形を表すクラス.
Definition: Complex.h:20
SDX::Rect::Hit
bool Hit(const Point *point) const override
衝突判定.
Definition: Rect.h:154
SDX::Rect::GetPoint
Point GetPoint() const
Pointを取得.
Definition: Rect.h:268
SDX::Rect::Move
void Move(double X移動量, double Y移動量) override
相対座標で移動.
Definition: Rect.h:75
SDX::Rect::Hit
bool Hit(const Rect *rect) const override
衝突判定.
Definition: Rect.h:261
SDX::Rect::Draw
void Draw(const Color &描画色) const override
描画する.
Definition: ShapeDraw.h:33
SDX::Rect::x
double x
起点座標
Definition: Rect.h:25
SDX::Rect::Hit
bool Hit(const Line *line) const override
衝突判定.
Definition: Rect.h:168
SDX::Line::GetMinX
double GetMinX() const
頂点AとBのX座標の小さい値を取得.
Definition: Line.h:218
SDX::Color
色を表すクラス.
Definition: Color.h:11
SDX::Rect::widthLeft
double widthLeft
起点から左側の幅
Definition: Rect.h:28
SDX::Line::GetMinY
double GetMinY() const
頂点AとBのY座標の小さい値を取得.
Definition: Line.h:224
SDX::IShape::LineLine
static bool LineLine(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
線分の交差判定.
Definition: IShape.h:62
SDX::Rect::Rotate
void Rotate(double 回転する角度) override
回転する.
Definition: Rect.h:90
SDX::Rect::MultiZoom
void MultiZoom(double X倍率, double Y倍率) override
縦横別で拡大率を掛け算する.
Definition: Rect.h:81
SDX::Rect::Hit
bool Hit(const IShape *shape) const override
衝突判定.
Definition: Rect.h:141
SDX::Line::GetXB
double GetXB() const
頂点BのX座標を取得.
Definition: Line.h:168
SDX::IPosition::zoomX
double zoomX
図形の拡大率
Definition: IPosition.h:14
SDX::Point::x
double x
座標
Definition: Point.h:25
SDX::Rect::Clone
virtual IShape * Clone(double X座標, double Y座標) const override
同じ形の図形を作る.
Definition: Rect.h:61
SDX::Rect::Hit
bool Hit(const Complex *complex) const override
衝突判定.
Definition: Rect.h:146
SDX::Rect::heightUp
double heightUp
起点から上側の幅
Definition: Rect.h:31
SDX::Line::GetYA
double GetYA() const
頂点AのY座標を取得.
Definition: Line.h:162
SDX::Line::GetXA
double GetXA() const
頂点AのX座標を取得.
Definition: Line.h:156
SDX::Line::GetMaxX
double GetMaxX() const
頂点AとBのX座標の大きい値を取得.
Definition: Line.h:230
SDX::Circle
円を表す図形クラス.
Definition: Circle.h:20
SDX::Rect::GetW
double GetW() const override
幅を取得.
Definition: Rect.h:107
SDX::IPosition::zoomY
double zoomY
図形の拡大率
Definition: IPosition.h:15
SDX::Rect::operator+
Rect operator+(Point &加算値)
座標に加算.
Definition: Rect.h:280
SDX::Rect::GetH
double GetH() const override
高さを取得.
Definition: Rect.h:112
SDX::Line::GetThickPow
double GetThickPow() const
太さの二乗を取得.
Definition: Line.h:212
SDX::Rect::heightDown
double heightDown
起点から下側の幅
Definition: Rect.h:32