python中用turtle画一个圆形,python绘图turtle画圆

本文介绍了使用Pythonturtle库绘制圆形的基本步骤,并比较了turtle法、OpenCV法、pygame法和matplotlib法在实现圆形或扇形图形上的应用,适合初学者了解不同技术在图形绘制中的运用。

大家好,本文将围绕用pythonturtle画简单的图案展开说明,python中用turtle画一个圆形是一个很多人都想弄明白的事情,想搞清楚python绘图turtle画圆需要先了解以下几个事情。

1 说明:

=====

1.1 是问答中的我的一个回答。

1.1 因为问答中没有代码块的,所以我改为这里写文章,然后链接过去不学c语言能不能学python

1.2 4种方法:turtle法、OpenCV法、pygame法和matplotlib法。

?url=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0921%2Faeffab8bj00qgzlfj0014c000hs00bvc.jpg&thumbnail=650x2147483647&quality=80&type=jpg

2 turtle法:

=======

2.1 代码:

#第1步:导入模块import turtle as t#第2步:初始化设置#窗口大小t.setup(800,800)#画笔颜色t.pencolor("green")#画笔粗细t.pensize(2.5)#第3步:开始绘画#r=300t.fd(300)#向左转向90==本来方向是水平向右t.left(90)#这时候方向就是垂直向上#画圆r=300,120°t.circle(300,120)#转角t.left(90)#走300=r,回到原点(圆点)t.fd(300)t.done()

2.2 效果图:

6dd554dag00qgzlfk006ic001o000zec.gif

3 OpenCV法:

==========

3.1 代码:注释版

#cv2就是OpenCV#第1步:导入模块import numpy as npimport cv2#第2步:Create a black image#窗口或者空的图像的大小pic_w=pic_h=800#获取中心点坐标centerxy=int(pic_w/2)#产生一个空的图像img = np.zeros((pic_w,pic_h,3), np.uint8)#第3步:画椭圆来画扇形#cv2.ellipse(img, center, axes, angle, startAngle, endAngle, #color[, thickness[, lineType[, shift]]])#重要参数:'''img: 图片center:椭圆中心(x,y) 比如==(256,256)axes:x/y轴的长度 比如==(100,50)angle:angle--椭圆的旋转角度==0#角度是以顺时针方向计算的startAngle:startAngle--椭圆的起始角度==90endAngle:endAngle--椭圆的结束角度==180color: 颜色'''#坐标点(centerxy,centerxy)代表窗口的中心点#270°开始,顺时针,180°停止img = cv2.ellipse(img,(centerxy,centerxy),(100,50),0,270,180,(0,255,0),-1)cv2.imshow('image',img)cv2.waitKey(0)cv2.destroyAllWindows()

3.2 效果图:

?url=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0921%2F5217432dj00qgzlfj0005c000c400bmc.jpg&thumbnail=650x2147483647&quality=80&type=jpg

4 pygame法:

==========

4.1 代码:

#注意pygame画扇形是通过画弧形来的,一般弧形宽度为1#如果宽度等于半径,就是扇形#第1步:导入模块import pygame,sys,mathfrom pygame.locals import *#第2步:初始化pygame.init()#窗口大小设置screen = pygame.display.set_mode((600,500))#窗口标题名pygame.display.set_caption("sector扇形")#第3步:循环while True: #退出设置,pygame必须在循环中一开始就设立 for event in pygame.event.get(): if event.type in (KEYDOWN,QUIT): sys.exit() #背景颜色填充255,255,255==白色 screen.fill((0,0,0)) #黑色 color = 0,0,255 #蓝色 start_angle = math.radians(0) end_angle = math.radians(80) #注意width一般为1,那就是弧形,当等于r=radius就是扇形 width = 200 x = 300 y = 250 radius = 200 #位置公式,喜欢这样,看得懂 position = x - radius, y - radius, radius*2, radius*2 #利用画弧形函数 pygame.draw.arc(screen,color,position,start_angle,end_angle,width) pygame.display.update()

4.2 图:

?url=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0921%2F59886240j00qgzlfj000fc000f400dic.jpg&thumbnail=650x2147483647&quality=80&type=jpg

5 matplotlib法:

==========

5.1 代码:

#利用饼图画扇形,不显示其他#第1步:导入模块import matplotlib.pyplot as plt#第2步:定义画布fig = plt.figure(figsize=(8,6), dpi=80)#第3步:数据datax=[10,20,40,20,10]#突出第3部分explode = [0,0,0.1,0,0]#显示突出第3部分,其余均为w=白色colorx=['w','w','r','w','w']#第4步:作图,设置标签,突出显示块,显示百分比#plt.pie(frac, labels=labels, explode=explode, autopct="%.1f%%",colors=colorx)#不显示其他部分plt.pie(datax,explode=explode,colors=colorx)#标题名plt.title("扇形图")#图片展示plt.show()

5.2 图:

?url=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0921%2Fcfd4bc43j00qgzlfj000ac000hs00dlc.jpg&thumbnail=650x2147483647&quality=80&type=jpg

喜欢不?关注我不迷路。

Python中,我们可以使用turtle模块来创建简单的图形,包括绘制小象侧面。不过,由于turtle模块并不直接内置大象形状的函数,我们通常需要通过组合圆形、矩形等基本形状来模拟。下面是一个简化的步骤示例: 首先,导入turtle库并创建一个Turtle对象: ```python import turtle # 创建一个turtle对象 my_turtle = turtle.Turtle() ``` 然后,你可以设置一些变量,比如象耳朵的位置,身体的颜色等: ```python ear_size = 50 body_color = 'gray' ``` 接着,用turtle的`begin_fill()`开始填充颜色,并出小象的身体部分,可以成椭圆代表头部和两个梯形代表身体两侧: ```python # 绘制头部和身体 my_turtle.begin_fill() my_turtle.color(body_color) my_turtle.penup() my_turtle.goto(-ear_size, ear_size * 1.5) # 上方位置 my_turtle.pendown() my_turtle.circle(ear_size * 0.8) # 圆形头部 my_turtle.right(90) my_turtle.forward(ear_size * 2) # 身体宽度 my_turtle.left(90) my_turtle.end_fill() my_turtle.penup() my_turtle.goto(-ear_size * 2, -ear_size) # 左侧位置 my_turtle.pendown() my_turtle.forward(ear_size * 4) # 身体长度 my_turtle.left(90) my_turtle.forward(ear_size) # 右耳位置 my_turtle.right(90) my_turtle.end_fill() ``` 为了完成象耳朵,可以分别两个小三角形: ```python # 绘制左耳朵 my_turtle.penup() my_turtle.goto(-ear_size * 0.7, ear_size * 1.2) my_turtle.pendown() my_turtle.forward(ear_size * 0.6) my_turtle.left(135) my_turtle.forward(ear_size * 0.6) my_turtle.right(135) my_turtle.forward(ear_size * 0.6) # 绘制右耳朵 my_turtle.penup() my_turtle.goto(ear_size * 0.7, ear_size * 1.2) my_turtle.pendown() my_turtle.forward(ear_size * 0.6) my_turtle.right(135) my_turtle.forward(ear_size * 0.6) my_turtle.left(135) my_turtle.forward(ear_size * 0.6) ``` 最后别忘了关闭填充: ```python my_turtle.done() # 显示窗口并结束绘图 ``` 这只是一个基础的示例,如果想让小象更逼真,你可能还需要调整形状和细节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值