だから私がしなければならないのは、重複していないか、正方形(0,0)から(1,1)の外側に50の円を作成することです。 :1x1四角形のPythonサークルが重複していないか、正方形の外にありません
fig = plt.figure()
ax = fig.add_axes([.1, .1, .8, .8], axisbg = 'Violet', aspect = 'equal')
clist = []
def overlap(c1, c2):
distance = ((c1.center[0] - c2.center[0])**2 + (c1.center[0] - c2.center[0])**2)**0.5
return distance < c1.radius + c2.radius
for i in range(1000):
x = np.random.rand(1)
y = np.random.rand(1)
c = Circle(xy=(x, y), radius = 0.05, fc = 'Blue', ec = 'DarkBlue', lw = 1.5)
if x - 0.05 >= 0.0:
if x + 0.05 <= 1.0:
if y - 0.05 >= 0.0:
if y + 0.05 <= 1.0:
if len(clist) == 0:
clist.append(c)
ax.add_patch(c)
else:
for j in range(len(clist)):
if overlap(c, clist[j]) == True:
break
if j == len(clist) - 1:
if overlap(c, clist) == False:
clist.append(c)
ax.add_atch(c)
if len(clist) == 50:
break
ax.autoscale()
私は次のようになるだろう私のリストを印刷したい場合:
[<matplotlib.patches.Circle object at 0x07E5F3D0>]
私は私のコードを実行するときにこのエラーが発生します。
AttributeError Traceback (most recent call last)
<ipython-input-35-48e3a2854e8e> in <module>()
24 break
25 if j == len(clist) - 1:
---> 26 if overlap(c, clist) == False:
27 clist.append(c)
28 ax.add_atch(c)
<ipython-input-35-48e3a2854e8e> in overlap(c1, c2)
4
5 def overlap(c1, c2):
----> 6 distance = ((c1.center[0] - c2.center[0])**2 + (c1.center[0] - c2.center[0])**2)**0.5
7 return distance < c1.radius + c2.radius
8
AttributeError: 'list' object has no attribute 'center'
それで、間違いはサークルがリストに正しく格納されていることですが、私はこのように他の人に見てきました。私は間違って何をしていますか?
あなたはどのバージョンのpythonを使用していますか?あなたは 'python-2.7'タグと' python-3.x'タグを持っています。 –
私はPython 3.5を使用しています –
ここで 'numpy'を使用していますか?なぜこの仕事のために 'np.random.rand'を呼び出すのか分かりません。 'numpy'を使い続ければ、コードを修正してx座標とy座標を同時に取得する必要があります。また、[0,1]に乱数を設定しても、範囲の長さを掛けてそれを拡大し、開始点(半径)に追加して右に収まるようにすることができますボックス。これにより、有効な位置を確認するために4つのif文が削除されます。 –