python画奥特曼代码_程序员式优雅表白,教你用python代码画爱心

还能用python代码画爱心?还有这种操作?这是什么原理?

不相信python代码可以画爱心?先来一张效果图来看看效果吧!

PyCharm pro Mac-PyCharm pro for Mac( Python IDE 开发软件)- Mac下载​www.macdown.com
deeed8414a5033368a84ab1440d06ccf.png

0d118ec46ee62119a4bb641585bbb869.gif

用python代码画爱心的思路是怎样的?

1、怎么画心形曲线

2、怎么填满心形曲线

3、怎么用 python 画出爱心

接下来看好了,满满的都是干货,不收藏将是遗憾哦!

第一:怎么画心形曲线

这个最难的部分,前人已将大树栽好,我们只要记住公式,即可在大树下乘凉哦!

766b2b70555da27048836745f88881cf.png

今天我们选用中上的那个图形,一起来看看吧!

第二:怎样填满爱心曲线?

简单说:函数=0 是一根线, 那线的左边跟右边就是大于 0 跟小于 0.

取x, y = 0 , 发现 函数 <=0 是成立的,先试试!

第三:怎么用 python 画出爱心?

我们先测试一下公式:

499fc7b5a70c71882a25e49c83f714fe.png

, 把公式放到 python 中, 借助 sympy 这个库,确认这个公式是否可行。

from sympy import var, plot_implicit

var('x y ')
plot_implicit((x**2+(-y)**2-1)**3+x**2*(-y)**3)

效果如下图所示:(好像还不错哦)。

3f2fe157eca20c97a9b101034a3668d6.png

我们把一行画心的代码解析一下,是这样:

l2=[]
# 将纵向每个字符当作 y 坐标的刻度
for y in range(15,-15,-1):
 l3 = []
 # 将横向每个字符当作 x 坐标的刻度
 for x in range(-30,30):
 # 如果 x,y 点在心形内,则将一个字符加入到行,否则加入空字符
 l3.append((' I love U'[(x-y)%9]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' '))
 l2.append(''.join(l3))
l1 ='n'.join(l2) 
for i in l1:
 print("033[91m"+i,end="",flush=True) 

下面这段代码,是不是跟我们用的公式很像, 只不过做了 x y 轴的缩放:

((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0

下面这段代码的意思是:从 ' I love U' 里挑字符出来拼行. 为毛 x-y ? 只为了斜一点, 但要求是保证它的增速为 1 . 你用 x 也行 但画出来就是耿直的 I love U了。

' I love U'[(x-y)%9]

我们只需要将字符当作坐标刻度。去填满

a471411a99cd07ee6eb90b70e807a67c.png

即可!

最后就是 print 控制颜色:

033[91m

基本就这样,有兴趣的同学可以将画心的函数都试一下.理论上都可以画出来。 只不过有一些是极坐标,要做一些转换才行。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用Python Turtle模块奥特曼代码: ```python import turtle # 设置笔大小和速度 turtle.pensize(5) turtle.speed(10) # 奥特曼的身体 turtle.color('red', 'red') turtle.begin_fill() turtle.left(45) turtle.forward(150) turtle.circle(90, 180) turtle.right(90) turtle.circle(90, 180) turtle.forward(150) turtle.end_fill() # 奥特曼的头部 turtle.color('white', 'white') turtle.begin_fill() turtle.right(135) turtle.forward(150) turtle.right(90) turtle.circle(-90, 180) turtle.left(90) turtle.circle(-90, 180) turtle.forward(150) turtle.end_fill() # 奥特曼的眼睛 turtle.penup() turtle.goto(-35, 80) turtle.pendown() turtle.color('black', 'white') turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(-35, 70) turtle.pendown() turtle.color('black', 'black') turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.penup() turtle.goto(35, 80) turtle.pendown() turtle.color('black', 'white') turtle.begin_fill() turtle.circle(25) turtle.end_fill() turtle.penup() turtle.goto(35, 70) turtle.pendown() turtle.color('black', 'black') turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 奥特曼的口 turtle.penup() turtle.goto(-50, 20) turtle.pendown() turtle.color('black', 'black') turtle.begin_fill() turtle.left(45) turtle.forward(100) turtle.right(90) turtle.circle(-50, 180) turtle.right(90) turtle.forward(100) turtle.end_fill() # 完成 turtle.done() ``` 运行代码后,会弹出一个窗口展示出的奥特曼图案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值