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