teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

d

2019/12/10 10:23

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -46,4 +46,56 @@
46
46
  ax.autoscale()
47
47
  ```
48
48
 
49
- ![イメージ説明](6c2287a9d62119a8a9c7244d3ca6af1b.png)
49
+ ![イメージ説明](6c2287a9d62119a8a9c7244d3ca6af1b.png)
50
+
51
+ ## 追記
52
+
53
+ ベクトル (x, y) に直行するベクトルは (y, -x), (y, x) になります。
54
+ これをノルムで割り、単位ベクトルにして、半径をかけると、円周上の点になります。
55
+
56
+ [直角なベクトルを作り出す – TauStation](http://taustation.com/math-rightangle-vector/)
57
+
58
+ あまりスマートに書けなかったですが、コードにすると以下のようになります。
59
+
60
+ ```python
61
+ import matplotlib.pyplot as plt
62
+ import numpy as np
63
+
64
+ fig, ax = plt.subplots()
65
+ ax.set_aspect("equal")
66
+
67
+ points = np.array([(random.random() + i, random.random() + i) for i in range(10)])
68
+ r = 0.5
69
+
70
+
71
+ def get_points(points):
72
+ sides1, sides2 = [], [] # 両側の点を格納する配列
73
+
74
+ for i in range(len(points) - 1):
75
+ p1, p2 = points[i], points[i + 1]
76
+ v = p2 - p1 # p1 -> p2 のベクトル
77
+
78
+ # v に直交する単位ベクトル
79
+ n = np.array([v[1], -v[0]])
80
+ n /= np.linalg.norm(n)
81
+
82
+ # 直交する単位ベクトル * 半径が円周の点
83
+ if i == 0:
84
+ sides1.append(p1 + n * r)
85
+ sides2.append(p1 - n * r)
86
+ sides1.append(p2 + n * r)
87
+ sides2.append(p2 - n * r)
88
+
89
+ return np.array(sides1 + sides2[::-1])
90
+
91
+
92
+ sides = get_points(points)
93
+ print(sides)
94
+
95
+ ax.fill(sides[:, 0], sides[:, 1], alpha=0.5, ec="k")
96
+ for p in points:
97
+ ax.add_patch(plt.Circle(xy=p, radius=r, fc="none", ec="k", lw=1))
98
+ ax.autoscale()
99
+ ```
100
+
101
+ ![イメージ説明](11724de38d0b6041a78f2d43e054a234.png)

1

d

2019/12/10 10:23

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -21,4 +21,29 @@
21
21
  ax.plot(x, y, ".")
22
22
  ```
23
23
 
24
- ![イメージ説明](2b49cbc01c2b5e3f2dad821da3871b4d.png)
24
+ ![イメージ説明](2b49cbc01c2b5e3f2dad821da3871b4d.png)
25
+
26
+ ## 追記
27
+
28
+ plot() で linewidth を太めに設定して、線を引けばいいのではないでしょうか。
29
+
30
+ ```python
31
+ import matplotlib.pyplot as plt
32
+ import random
33
+
34
+ fig, ax = plt.subplots()
35
+ ax.set_aspect("equal")
36
+
37
+ # ここで与える点と円の半径rからplt.fill(x, y)におけるxとyを求めたいです.
38
+ xs = [random.random() + i for i in range(10)]
39
+ ys = [random.random() + i for i in range(10)]
40
+ r = 0.5
41
+
42
+ for p in zip(xs, ys):
43
+ ax.add_patch(plt.Circle(xy=p, radius=r, fc="none", ec="k", lw=1))
44
+ ax.plot(xs, ys, lw=r * 50, alpha=0.3)
45
+
46
+ ax.autoscale()
47
+ ```
48
+
49
+ ![イメージ説明](6c2287a9d62119a8a9c7244d3ca6af1b.png)