今晚上回来有时间的话把它改成动图画点
拟合圆的效果:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import random as rd
r = 6.0
a, b = (0., 0.)
Ary=[]
Aryd=[]
def getPoint():
R=int(r)
st=[0,R]
Ary.append(st)
plt.scatter(0,R,color='b')
plt.scatter(0,-R,color='b')
plt.scatter(-R,0,color='b')
plt.scatter(R,0,color='b')
for i in range(0,R-1):
H=[st[0]+1,st[1]]
L=[st[0]+1,st[1]-1]
DH=H[0]*H[0]+H[1]*H[1]-R*R
DL=L[0]*L[0]+L[1]*L[1]-R*R
di=DH+DL
Aryd.append(di)
if(di>0):
H=L
st=H
Ary.append(st)
DrawPoint(st[0],st[1],'b')
def DrawPoint(x,y,cr):
plt.scatter(x,y,color=cr)
plt.scatter(-x,y,color=cr)
plt.scatter(-x,-y,color=cr)
plt.scatter(x,-y,color=cr)
plt.scatter(y,x,color=cr)
plt.scatter(-y,x,color=cr)
plt.scatter(-y,-x,color=cr)
plt.scatter(y,-x,color=cr)
def setAxis():
lent=range(-15,15,1)
plt.xticks(lent)
plt.yticks(lent)
plt.plot([-18,18],[0,0],'k')
plt.plot([0,0],[-18,18],'k')
def drawCle():
theta = np.arange(0, 2*np.pi, 0.01)
x = a + r * np.cos(theta)
y = b + r * np.sin(theta)
plt.plot(x,y,'r')
plt.axis('equal')
if __name__=="__main__":
r=int(rd.uniform(3,15)+0.5)
setAxis()
getPoint()
drawCle()
print(Ary)
print(Aryd)
plt.show()