简单实现python画圆功能

作者:ijbuhv 时间:2021-06-06 02:39:36 

本文实例为大家分享了python实现画圆功能的具体代码,供大家参考,具体内容如下

简单实现python画圆功能


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.patches as mpatches

fig = plt.figure(figsize = (16,8))
ax = fig.gca()
ax.set_xlim(-5,18)
ax.set_ylim(-5,8)
alpha = np.linspace(-np.pi,np.pi,30)  
t = np.linspace(-np.pi,np.pi,100)
def ppp(nc , na , c , sx , sy):
beta = alpha[na]
gam = alpha[na+1]
x = [] ; y = [] ;
r = 0.5*nc
x.append(r*np.cos(beta)) ; y.append(r*np.sin(beta)) ;
tt = np.linspace(beta,gam,10)
for i in range(len(tt)):
 x.append(r*np.cos(tt[i]))
 y.append(r*np.sin(tt[i]))
r-=0.5
x.append(r*np.cos(gam)) ; y.append(r*np.sin(gam))
tt = np.linspace(gam,beta,10)
for i in range(len(tt)):
 x.append(r*np.cos(tt[i])) ; y.append(r*np.sin(tt[i]))
x = np.array(x) ; y = np.array(y)
verts = list(zip(x+sx,y+sy))
poly = Polygon(verts , facecolor=c , edgecolor=c)
ax.add_patch(poly)

#ppp(2,0,'r',0,0)
def get_coordiate(x , y):
beta = (alpha[y]+alpha[y+1])/2
pr = 0.5*(x+x-1)/2
sx = pr*np.cos(beta) ; sy = pr*np.sin(beta)
return sx,sy
def pp_arrow(x,y):
print x , y
sx , sy = get_coordiate(x, y)
for i in range(-1,2):
 for j in range(-1,2):
  if i == 0 and j == 0:
   continue
  ex , ey = get_coordiate(x+i, y+j)
  ax.arrow(sx,sy,ex-sx,ey-sy,head_width=0.05,head_length=0.1,fc='r',ec='r')

def pxy(cx,cy):
for i in range(10):
 plt.plot(0.5*i*np.cos(t)+cx,0.5*i*np.sin(t)+cy)  
for i in range(len(alpha)):
 plt.plot((0.5*np.cos(alpha)+cx,4.5*np.cos(alpha)+cx),(0.5*np.sin(alpha)+cy,4.5*np.sin(alpha)+cy))

cx = 13 ; cy =0
pxy(0,0)
pxy(13,0)

mr = np.zeros((10,30))
mb = np.zeros((10,30))

for i in range(3,9):
for j in range(1,27):
 ttt = np.random.random()
 if ttt >= 0.98 and ttt <= 1:
  ppp(i,j,'r',0,0)
  mr[i][j] = 1
  continue
 if ttt < 0.8:
  continue
 else:
  ppp(i,j,'b',0,0)
  mb[i][j] = 1

for i in range(10):
for j in range(30):
 if mr[i][j] == 0:
  continue
 pp_arrow(i, j)
 ma = 0 ;posx = 1 ; posy = 1
 for k in range(-1,2):
  for s in range(-1,2):
   mb[i+k][j+s]=0
   ttt = np.random.random()
   if ttt > ma:
    ma = ttt
    posx = k ; posy = s
 ppp(i+posx , j+posy , 'r' , 13 , 0)
 ppp(i,j,'g',13,0)

for i in range(10):
for j in range(30):
 if mb[i][j] == 0:
  continue
 ppp(i , j , 'b' , 13 , 0)

arrow = mpatches.Arrow(5, 0
     ,3,0,width = 1,color = 'r')
ax.add_patch(arrow)
ax.text(6.2,0.5,'after one\nunit time',ha='center',va='center',color='y')

xx = [4,4,4]
yy = [7.5,6.5,5.5]
c = ['b' , 'r' , 'g']
s = ['debris','satellite' , 'the location of satellite one unit time ago']
for i in range(3):
print xx[i] , yy[i]
rect = mpatches.Rectangle((xx[i],yy[i]),0.5,0.3,ec = c[i],fc=c[i] )
ax.text(xx[i]+0.7,yy[i],s[i])
ax.add_patch(rect)
plt.axis('off')
plt.show()

来源:http://blog.csdn.net/cq_pf/article/details/50629768

标签:python,画圆
0
投稿

猜你喜欢

  • 使用Python进行中文繁简转换的实现代码

    2021-05-04 09:19:03
  • 一起来学习Python的列表

    2022-08-04 06:06:23
  • Python ValueError: invalid literal for int() with base 10 实用解决方法

    2023-06-18 17:00:40
  • XML轻松学习手册(3)XML的术语

    2008-09-05 17:17:00
  • Python时间和字符串转换操作实例分析

    2023-04-15 22:58:08
  • OpenCV+python实现膨胀和腐蚀的示例

    2021-06-12 10:35:00
  • Python中常见的反爬机制及其破解方法总结

    2023-03-05 19:38:42
  • asp FSO组件操作详解

    2009-03-20 18:32:00
  • Python基于Matplotlib库简单绘制折线图的方法示例

    2021-10-26 15:01:12
  • python 函数定位参数+关键字参数+inspect模块

    2023-07-05 23:04:37
  • python django model联合主键的例子

    2023-10-09 19:18:17
  • python OpenCV学习笔记实现二维直方图

    2021-08-17 11:23:49
  • Python爬取你好李焕英豆瓣短评生成词云的示例代码

    2021-04-06 12:13:21
  • 速记Python布尔值

    2022-04-23 10:02:17
  • python日志模块logbook使用方法

    2021-10-02 02:25:50
  • Python3 使用pip安装git并获取Yahoo金融数据的操作

    2023-11-17 21:56:48
  • 一个asp正则替换的方法

    2008-11-25 14:05:00
  • python 统计list中各个元素出现的次数的几种方法

    2022-12-09 10:04:01
  • OpenCV实现常见的四种图像几何变换

    2022-11-08 11:20:50
  • Python编程基础之输入与输出

    2021-03-03 10:06:07
  • asp之家 网络编程 m.aspxhome.com