SDXFrameWork 0.12 SDXFrameWork

- Main Page
- Related Pages
- Classes

All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Line.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 namespace SDX
8 {
9 class Complex;
10 class Point;
11 class Line;
12 class Circle;
13 class Rect;
14
15 class Camera;
16 class Color;
17
20 class Line : public IShape
21 {
22 private:
23 double x;
24 double y;
25 double xA;
26 double yA;
27 double xB;
28 double yB;
29 double thick;
30
31 double angle;
32
33 double lengthA;
34 double lengthB;
35
36 double width;
37 double height;
38 double thickHarf;
39 double thickPow;
40 double minX;
41 double minY;
42 double maxX;
43 double maxY;
44
45 void CulLine()
46 {
47 this->xA = x + cos(angle) * lengthA;
48 this->yA = y + sin(angle) * lengthA;
49 this->xB = x - cos(angle) * lengthB;
50 this->yB = y - sin(angle) * lengthB;
51 this->width = abs(this->xB - this->xA);
52 this->height = abs(this->yB - this->yA);
53 this->thickHarf = this->thick / 2;
54 this->thickPow = this->thick * this->thick / 4;
55 this->minX = std::min(xA, xB);
56 this->maxX = std::max(xA, xB);
57 this->minY = std::min(yA, yB);
58 this->maxY = std::max(yA, yB);
59 }
60 public:
62 Line(double X中心座標, double Y中心座標, double 角度, double 長さ, double 太さ) :
63 x(X中心座標),
64 y(Y中心座標),
65 angle(角度),
66 lengthA(長さ / 2),
67 lengthB(長さ / 2),
68 thick(太さ)
69 {
70 this->CulLine();
71 }
72
74 Line(double X中心座標, double Y中心座標, double 角度, double 前方長さ, double 後方長さ, double 太さ) :
75 x(X中心座標),
76 y(Y中心座標),
77 angle(角度),
78 lengthA(前方長さ),
79 lengthB(後方長さ),
80 thick(太さ)
81 {
82 this->CulLine();
83 }
84
85 virtual IShape* Clone(double X座標, double Y座標) const override
86 {
87 auto shape = new Line(X座標, Y座標, this->angle, this->lengthA, this->thick);
88 shape->zoomX = this->zoomX;
89 shape->zoomY = this->zoomY;
90 return shape;
91 }
92
93 double GetX() const override
94 {
95 return x;
96 }
97
98 double GetY() const override
99 {
100 return y;
101 }
102
103 double GetW() const override
104 {
105 return int(xA - xB);
106 }
107
108 double GetH() const override
109 {
110 return int(yA - yB);
111 }
112
113 void SetPos(double X座標, double Y座標) override
114 {
115 this->x = X座標;
116 this->y = Y座標;
117 this->CulLine();
118 }
119
120 void MultiZoom(double X倍率, double Y倍率) override
121 {
122 this->lengthA *= X倍率;
123 this->lengthB *= Y倍率;
124 this->thick *= X倍率;
125
126 this->CulLine();
127
128 zoomX *= X倍率;
129 zoomY *= Y倍率;
130 }
131
132 void Rotate(double 回転する角度) override
133 {
134 this->angle += 回転する角度;
135 this->CulLine();
136 }
137
138 void Move(double X移動量, double Y移動量) override
139 {
140 this->x += X移動量;
141 this->y += Y移動量;
142 this->xA += X移動量;
143 this->yA += Y移動量;
144 this->xB += X移動量;
145 this->yB += Y移動量;
146
147 this->minX += X移動量;
148 this->minY += Y移動量;
149 this->maxX += X移動量;
150 this->maxY += Y移動量;
151 }
152
153 void Draw(const Color &描画色) const override;
154
156 double GetXA() const
157 {
158 return xA;
159 }
160
162 double GetYA() const
163 {
164 return yA;
165 }
166
168 double GetXB() const
169 {
170 return xB;
171 }
172
174 double GetYB() const
175 {
176 return yB;
177 }
178
180 double GetThick() const
181 {
182 return thick;
183 }
184
186 void SetThick(double 太さ)
187 {
188 thick = 太さ;
189 thickHarf = int(thick / 2);
190 thickPow = int(thick * thick / 4);
191 }
192
194 double GetAngle() const override
195 {
196 return this->angle;
197 }
198
200 double GetLength() const
201 {
202 return this->lengthA + this->lengthB;
203 }
204
206 double GetThickHarf() const
207 {
208 return this->thickHarf;
209 }
210
212 double GetThickPow() const
213 {
214 return this->thickPow;
215 }
216
218 double GetMinX() const
219 {
220 return minX;
221 }
222
224 double GetMinY() const
225 {
226 return minY;
227 }
228
230 double GetMaxX() const
231 {
232 return maxX;
233 }
234
236 double GetMaxY() const
237 {
238 return maxY;
239 }
240
241 bool Hit(const IShape *shape) const override
242 {
243 return shape->Hit(this);
244 }
245 bool Hit(const Complex *complex) const override
246 {
247 for (auto it : complex->shapes)
248 {
249 if (it->Hit(this)) return true;
250 }
251 return false;
252 }
253 bool Hit(const Point *point) const override
254 {
255 return LinePoint(point->GetX(), point->GetY(), this->GetThick());
256 }
257 bool Hit(const Line *line) const override
258 {
259 //四角形でラフチェック
260 if (RectRect(this->GetXA() - this->GetThickHarf(), this->GetYA() - this->GetThickHarf(), this->GetXB() - line->GetThickHarf(), this->GetYB() - line->GetThickHarf(),
261 line->GetXA() - this->GetThickHarf(), line->GetYA() - this->GetThickHarf(), line->GetXB() - line->GetThickHarf(), line->GetYB() - line->GetThickHarf()))
262 {
263 return true;
264 }
265 //交差チェック
266 if (LineLine(this->GetXA(), this->GetYA(), this->GetXB(), this->GetYB(), line->GetXA(), line->GetYA(), line->GetXB(), line->GetYB()))
267 {
268 return true;
269 }
270 //線と端のチェック
271 //線が点になっているかで分ける
272 if (this->GetLength() != 0)
273 {
274 if (this->LinePoint(line->GetXA(), line->GetYA(), this->GetThickHarf() + line->GetThickHarf())) return true;
275 if (this->LinePoint(line->GetXB(), line->GetYB(), this->GetThickHarf() + line->GetThickHarf())) return true;
276 }
277 if (line->GetLength() != 0)
278 {
279 if (line->LinePoint(this->GetXA(), this->GetYA(), this->GetThickHarf() + line->GetThickHarf())) return true;
280 if (line->LinePoint(this->GetXB(), this->GetYB(), this->GetThickHarf() + line->GetThickHarf())) return true;
281 }
282
283 return false;
284 }
285 bool Hit(const Rect *rect) const override;
286 bool Hit(const Circle *circle) const override;
287
289 bool LinePoint(double px, double py, double range) const
290 {
291 //点同士の距離
292 const double dx = GetXB() - GetXA();
293 const double dy = GetYB() - GetYA();
294
295 const double a = dx * dx + dy * dy;
296 const double b = dx *(GetXA() - px) + dy *(GetYA() - py);
297
298 //点の場合
299 if (a == 0)
300 {
301 if (
302 (GetXA() - px) *(GetXA() - px) +
303 (GetYA() - py) *(GetYA() - py)
304 < range * range) return true;
305 return false;
306 }
307
308 const double t = -b / a;
309
310 const double tx = GetXA() + dx*t;
311 const double ty = GetYA() + dy*t;
312
313 //線分上の点が範囲内かチェック
314 if (tx < minX || tx > maxX || ty < minY || ty > maxY)
315 {
316 if (
317 (xA - px) * (xA - px) + (yA - py) * (yA - py) > range * range &&
318 (xB - px) * (xB - px) + (yB - py) * (yB - py) > range * range
319 )
320 {
321 return false;
322 }
323 }
324
325 const double d = (px - tx)*(px - tx) + (py - ty) * (py - ty);
326
327 return (d < range * range);
328 }
329 };
330 }
SDX::Line::GetAngle
double GetAngle() const override
角度を取得.
Definition: Line.h:194
SDX::Line::GetThickHarf
double GetThickHarf() const
太さの半分を取得.
Definition: Line.h:206
SDX::Line::GetYB
double GetYB() const
頂点BのY座標を取得.
Definition: Line.h:174
SDX::Rect
矩形を表す図形クラス.
Definition: Rect.h:22
SDX::Line
太さのある線を表す図形クラス.
Definition: Line.h:20
SDX::Line::GetThick
double GetThick() const
太さを取得.
Definition: Line.h:180
SDX::Line::Hit
bool Hit(const Line *line) const override
衝突判定.
Definition: Line.h:257
SDX::Line::SetThick
void SetThick(double 太さ)
太さを設定.
Definition: Line.h:186
SDX::Line::Line
Line(double X中心座標, double Y中心座標, double 角度, double 前方長さ, double 後方長さ, double 太さ)
コンストラクタ.
Definition: Line.h:74
SDX::Line::GetMaxY
double GetMaxY() const
頂点AとBのY座標の大きい値を取得.
Definition: Line.h:236
SDX::Complex::shapes
std::vector< IShape * > shapes
保持するShape
Definition: Complex.h:23
SDX::Point::GetY
double GetY() const override
Y座標を取得.
Definition: Point.h:68
SDX::Line::GetLength
double GetLength() const
長さを取得.
Definition: Line.h:200
SDX::Point
点を表す図形クラス.
Definition: Point.h:22
SDX::Line::Rotate
void Rotate(double 回転する角度) override
回転する.
Definition: Line.h:132
SDX::IShape
衝突判定可能な図形の抽象クラス.
Definition: IShape.h:21
SDX::IShape::Hit
virtual bool Hit(const IShape *iShape) const =0
衝突判定.
SDX::Complex
複合図形を表すクラス.
Definition: Complex.h:20
SDX::Line::Draw
void Draw(const Color &描画色) const override
描画する.
Definition: ShapeDraw.h:23
SDX::Line::SetPos
void SetPos(double X座標, double Y座標) override
指定座標に移動.
Definition: Line.h:113
SDX::Line::GetMinX
double GetMinX() const
頂点AとBのX座標の小さい値を取得.
Definition: Line.h:218
SDX::Color
色を表すクラス.
Definition: Color.h:11
SDX::Line::GetX
double GetX() const override
X座標を取得.
Definition: Line.h:93
SDX::Line::GetH
double GetH() const override
高さを取得.
Definition: Line.h:108
SDX::Line::Hit
bool Hit(const Complex *complex) const override
衝突判定.
Definition: Line.h:245
SDX::Line::Hit
bool Hit(const Point *point) const override
衝突判定.
Definition: Line.h:253
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::Line::GetW
double GetW() const override
幅を取得.
Definition: Line.h:103
SDX::Line::Line
Line(double X中心座標, double Y中心座標, double 角度, double 長さ, double 太さ)
コンストラクタ.
Definition: Line.h:62
SDX::Line::GetXB
double GetXB() const
頂点BのX座標を取得.
Definition: Line.h:168
SDX::IPosition::zoomX
double zoomX
図形の拡大率
Definition: IPosition.h:14
SDX::Line::MultiZoom
void MultiZoom(double X倍率, double Y倍率) override
縦横別で拡大率を掛け算する.
Definition: Line.h:120
SDX::Line::LinePoint
bool LinePoint(double px, double py, double range) const
線と点の当たり判定.
Definition: Line.h:289
SDX::Line::Clone
virtual IShape * Clone(double X座標, double Y座標) const override
同じ形の図形を作る.
Definition: Line.h:85
SDX::Line::GetYA
double GetYA() const
頂点AのY座標を取得.
Definition: Line.h:162
SDX::IShape::RectRect
static bool RectRect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
矩形の交差判定.
Definition: IShape.h:25
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::IPosition::zoomY
double zoomY
図形の拡大率
Definition: IPosition.h:15
SDX::Point::GetX
double GetX() const override
X座標を取得.
Definition: Point.h:63
SDX::Line::GetThickPow
double GetThickPow() const
太さの二乗を取得.
Definition: Line.h:212
SDX::Line::GetY
double GetY() const override
Y座標を取得.
Definition: Line.h:98
SDX::Line::Hit
bool Hit(const IShape *shape) const override
衝突判定.
Definition: Line.h:241
SDX::Line::Move
void Move(double X移動量, double Y移動量) override
相対座標で移動.
Definition: Line.h:138