python画漂亮的画

阅读: 评论:0

python画漂亮的画

python画漂亮的画

#如何用python绘制生日蛋糕?

平时在经常在b站看到up主绘制生日蛋糕的精美图片。以前学过c、c++语言,也想着用C去绘制一些生日蛋糕,绘制一些女神喜欢的小礼物,不过实现起来比较麻烦,刚好学了一些python基础,借阅一些资料,看了大神的操作,借用了一些代码,用python绘制个生日蛋糕!

不过有一些缺点:

其实这里需要注意的是这份代码写的比较low,有许多重复的代码,没有用函数封装起来,一点都不优雅。希望大家可以在以后写代码的时候可以注意一下这一点。

这里比较难的点,就是任意曲线的绘制。对于这个问题,我一开始也想了很多办法,也去网上查了很多资料,但是关于这方面的资料好像比较少。当时我在知乎等一些文章上发现一些大佬用本轮法进行图像的绘制,好像是轮子的个数越多,画出来的图像就越精准,其原理就是利用了高等数学下册的傅里叶变换。那时看了许多这种类型的文章,但是由于我们还没有学习傅里叶变换,也就看了一个寂寞。

后来我在看turtle库时,发现了一个goto函数,也就是在画板上以正中心为原点,右边为x轴的正方向,上边为y轴的正方向。于是我就想到了用我们高中学过的参数方程来实现任意曲线的绘制。大家可以尝试着用一下参数方程来绘制自己想要的图形。

先看看绘制的效果:

`import turtle as t

import math as m

import random as r

def drawX(a, i):

angle = m.radians(i)

return a * m.cos(angle)

def drawY(b, i):

angle = m.radians(i)

return b * m.sin(angle)

设置背景颜色,窗口位置以及大小

t.bgcolor("#d3dae8")

t.setup(1000, 800)

t.penup()

<(150, 0)

t.pendown()

1

t.pencolor(“white”)

t.begin_fill()

for i in range(360):

x = drawX(150, i)

y = drawY(60, i)

<(x, y)

t.fillcolor("#fef5f7")

2

t.begin_fill()

for i in range(180):

x = drawX(150, -i)

y = drawY(70, -i)

<(x, y)

for i in range(180, 360):

x = drawX(150, i)

y = drawY(60, i)

<(x, y)

t.fillcolor("#f2d7dd")

3

t.pu()

<(120, 0)

t.pd()

t.begin_fill()

for i in range(360):

x = drawX(120, i)

y = drawY(48, i)

<(x, y)

t.fillcolor("#cbd9f9")

4

t.begin_fill()

t.pencolor("#fee48c")

for i in range(540):

x = drawX(120, i)

y = drawY(48, i) + 70

<(x, y)

<(-120, 0)

t.fillcolor("#cbd9f9")

5

t.pu()

<(120, 70)

t.pd()

t.pencolor("#fff0f3")

t.begin_fill()

for i in range(360):

x = drawX(120, i)

y = drawY(48, i) + 70

<(x, y)

t.fillcolor("#fff0f3")

6

t.pu()

<(110, 70)

t.pd()

t.pencolor("#fff9fb")

t.begin_fill()

for i in range(360):

x = drawX(110, i)

y = drawY(44, i) + 70

<(x, y)

t.fillcolor("#fff9fb")

7

t.pu()

<(120, 0)

t.pd()

t.begin_fill()

t.pencolor("#ffa79d")

for i in range(180):

x = drawX(120, -i)

y = drawY(48, -i) + 10

<(x, y)

<(-120, 0)

for i in range(180, 360):

x = drawX(120, i)

y = drawY(48, i)

<(x, y)

t.fillcolor("#ffa79d")

8

t.pu()

<(120, 70)

t.pd()

t.begin_fill()

t.pensize(4)

t.pencolor("#fff0f3")

for i in range(1800):

x = drawX(120, 0.1 * i)

y = drawY(-18, i) + 10

<(x, y)

<(-120, 70)

t.pensize(1)

for i in range(180, 360):

x = drawX(120, i)

y = drawY(48, i) + 70

<(x, y)

t.fillcolor("#fff0f3")

9

t.pu()

<(80, 70)

t.pd()

t.begin_fill()

t.pencolor("#6f3732")

<(80, 120)

for i in range(180):

x = drawX(80, i)

y = drawY(32, i) + 120

<(x, y)

<(-80, 70)

for i in range(180, 360):

x = drawX(80, i)

y = drawY(32, i) + 70

<(x, y)

t.fillcolor("#6f3732")

10

t.pu()

<(80, 120)

t.pd()

t.pencolor("#ffaaa0")

t.begin_fill()

for i in range(360):

x = drawX(80, i)

y = drawY(32, i) + 120

<(x, y)

t.fillcolor("#ffaaa0")

11

t.pu()

<(70, 120)

t.pd()

t.pencolor("#ffc3be")

t.begin_fill()

for i in range(360):

x = drawX(70, i)

y = drawY(28, i) + 120

<(x, y)

t.fillcolor("#ffc3be")

12

t.pu()

<(80, 120)

t.pd()

t.begin_fill()

t.pensize(3)

t.pencolor("#ffaaa0")

for i in range(1800):

x = drawX(80, 0.1 * i)

y = drawY(-12, i) + 80

<(x, y)

<(-80, 120)

t.pensize(1)

for i in range(180, 360):

x = drawX(80, i)

y = drawY(32, i) + 120

<(x, y)

t.fillcolor("#ffaaa0")

13

t.pu()

<(64, 120)

t.pd()

t.pencolor("#b1c9e9")

t.begin_fill()

for i in range(360):

x = drawX(4, i) + 60

y = drawY(1, i) + 120

<(x, y)

<(64, 170)

for i in range(540):

x = drawX(4, i) + 60

y = drawY(1, i) + 170

<(x, y)

<(56, 120)

t.fillcolor("#b1c9e9")

t.pencolor(“white”)

t.pensize(2)

for i in range(1, 6):

<(64, 120 + 10 * i)

t.pu()

<(56, 120 + 10 * i)

t.pd()

t.pu()

<(60, 170)

t.pd()

<(60, 180)

t.pensize(1)

t.pu()

<(64, 190)

t.pd()

t.pencolor("#f1add1")

t.begin_fill()

for i in range(360):

x = drawX(4, i) + 60

y = drawY(10, i) + 190

<(x, y)

t.fillcolor("#f1add1")

14

t.pu()

<(-56, 120)

t.pd()

t.pencolor("#b1c9e9")

t.begin_fill()

for i in range(360):

x = drawX(4, i) - 60

y = drawY(1, i) + 120

<(x, y)

<(-56, 170)

for i in range(540):

x = drawX(4, i) - 60

y = drawY(1, i) + 170

<(x, y)

<(-64, 120)

t.fillcolor("#b1c9e9")

t.pencolor(“white”)

t.pensize(2)

for i in range(1, 6):

<(-56, 120 + 10 * i)

t.pu()

<(-64, 120 + 10 * i)

t.pd()

t.pu()

<(-60, 170)

t.pd()

<(-60, 180)

t.pensize(1)

t.pu()

<(-56, 190)

t.pd()

t.pencolor("#f1add1")

t.begin_fill()

for i in range(360):

x = drawX(4, i) - 60

y = drawY(10, i) + 190

<(x, y)

t.fillcolor("#f1add1")

15

t.pu()

<(0, 130)

t.pd()

t.pencolor("#b1c9e9")

t.begin_fill()

for i in range(360):

x = drawX(4, i)

y = drawY(1, i) + 130

<(x, y)

<(4, 180)

for i in range(540):

x = drawX(4, i)

y = drawY(1, i) + 180

<(x, y)

<(-4, 130)

t.fillcolor("#b1c9e9")

t.pencolor(“white”)

t.pensize(2)

for i in range(1, 6):

<(4, 130 + 10 * i)

t.pu()

<(-4, 130 + 10 * i)

t.pd()

t.pu()

<(0, 180)

t.pd()

<(0, 190)

t.pensize(1)

t.pu()

<(4, 200)

t.pd()

t.pencolor("#f1add1")

t.begin_fill()

for i in range(360):

x = drawX(4, i)

y = drawY(10, i) + 200

<(x, y)

t.fillcolor("#f1add1")

16

t.pu()

<(30, 110)

t.pd()

t.pencolor("#b1c9e9")

t.begin_fill()

for i in range(360):

x = drawX(4, i) + 30

y = drawY(1, i) + 110

<(x, y)

<(34, 160)

for i in range(540):

x = drawX(4, i) + 30

y = drawY(1, i) + 160

<(x, y)

<(26, 110)

t.fillcolor("#b1c9e9")

t.pencolor(“white”)

t.pensize(2)

for i in range(1, 6):

<(34, 110 + 10 * i)

t.pu()

<(26, 110 + 10 * i)

t.pd()

t.pu()

<(30, 160)

t.pd()

<(30, 170)

t.pensize(1)

t.pu()

<(34, 180)

t.pd()

t.pencolor("#f1add1")

t.begin_fill()

for i in range(360):

x = drawX(4, i) + 30

y = drawY(10, i) + 180

<(x, y)

t.fillcolor("#f1add1")

17

t.pu()

<(-30, 110)

t.pd()

t.pencolor("#b1c9e9")

t.begin_fill()

for i in range(360):

x = drawX(4, i) - 30

y = drawY(1, i) + 110

<(x, y)

<(-26, 160)

for i in range(540):

x = drawX(4, i) - 30

y = drawY(1, i) + 160

<(x, y)

<(-34, 110)

t.fillcolor("#b1c9e9")

t.pencolor(“white”)

t.pensize(2)

for i in range(1, 6):

<(-26, 110 + 10 * i)

t.pu()

<(-34, 110 + 10 * i)

t.pd()

t.pu()

<(-30, 160)

t.pd()

<(-30, 170)

t.pensize(1)

t.pu()

<(-26, 180)

t.pd()

t.pencolor("#f1add1")

t.begin_fill()

for i in range(360):

x = drawX(4, i) - 30

y = drawY(10, i) + 180

<(x, y)

t.fillcolor("#f1add1")

###随机

color = ["#e28cb9", “#805a8c”, “#eaa989”, “#6e90b7”, “#b8b68f”, “#e174b5”, “#cf737c”, “#7c8782”]

for i in range(80):

t.pu()

x = r.randint(-120, 120)

y = r.randint(-25, 30)

<(x, y)

t.pd()

t.dot(r.randint(2, 5), color[r.randint(0, 7)])

for i in range(40):

t.pu()

x = r.randint(-90, 90)

y = r.randint(-35, 10)

<(x, y)

t.pd()

t.dot(r.randint(2, 5), color[r.randint(0, 7)])

for i in range(40):

t.pu()

x = r.randint(-80, 80)

y = r.randint(60, 90)

<(x, y)

t.pd()

t.dot(r.randint(2, 5), color[r.randint(0, 7)])

for i in range(30):

t.pu()

x = r.randint(-50, 50)

y = r.randint(45, 70)

<(x, y)

t.pd()

t.dot(r.randint(2, 5), color[r.randint(0, 7)])

for i in range(50):

t.pu()

x = r.randint(-500, 500)

y = r.randint(120, 300)

<(x, y)

t.pd()

t.dot(r.randint(3, 5), color[r.randint(0, 7)])

t.seth(90)

t.pu()

<(0, 0)

t.fd(210)

t.left(90)

t.fd(170)

t.pd()

t.write(“Happy Birthday”, font=(“Curlz MT”, 50))

t.done()`

本文发布于:2024-01-30 16:54:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660487521476.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:漂亮   python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23