各位,怎么用python画12个花瓣?

from turtle import *
circle(40,step = 12)
done


回答:

楼主的问题中已经定义好了turtle库,所以我在此基础上补充完善。思路是先定义一个花瓣

def draw_petal():

for i in range(2):

circle(40, 60)

left(120)

然后循环生成12个

# 循环调用画花瓣的函数,共绘制12个花瓣

for i in range(12):

draw_petal()

left(30)

done()


回答:

12 个随机颜色的花瓣,大小、位置、弧度等随机生成。

python">import turtle

import random

# 初始化

turtle.screensize(800, 600)

turtle.bgcolor("white")

turtle.speed(10)

turtle.hideturtle()

# 设置变量和函数

colors = ["red", "orange", "yellow", "green", "blue", "purple"]

petals = 12

radius = 100

amplitude = 20

period = 90

def draw_petal(petal_color):

turtle.color(petal_color)

turtle.begin_fill()

for angle in range(0, 360, 5):

x = radius * (1 + amplitude / 100.0 *

abs(math.sin(angle * math.pi / period)))

y = x * math.sin(angle * math.pi / 180)

turtle.goto(x, y)

turtle.end_fill()

# 开始绘制花朵

for petal in range(petals):

# 随机选择颜色并设置初始位置

color = random.choice(colors)

x = random.randint(-300, 300)

y = random.randint(-200, 200)

turtle.penup()

turtle.goto(x, y)

turtle.pendown()

# 开始绘制花瓣

size = random.randint(20, 60)

turtle.pensize(size / 10)

draw_petal(color)

# 显示完毕

turtle.done()


回答:

import numpy as np

import matplotlib.pyplot as plt

theta = np.linspace(0, 2.*np.pi, 1000)

r = np.cos(6*theta)

fig = plt.figure()

ax = fig.add_subplot(111, polar=True)

ax.plot(theta, r)

ax.set_yticklabels([])

plt.show()

各位,怎么用python画12个花瓣?

import numpy as np

import matplotlib.pyplot as plt

def draw_petal(ax, radius, petal_num):

theta = np.linspace(0, 2.*np.pi, 1000)

r = radius * np.cos(petal_num*theta)

ax.plot(theta, r)

fig = plt.figure()

ax = fig.add_subplot(111, polar=True)

ax.set_yticklabels([])

for i in range(5, 0, -1):

draw_petal(ax, i/5.0, i*4)

plt.show()

各位,怎么用python画12个花瓣?

以上是 各位,怎么用python画12个花瓣? 的全部内容, 来源链接: utcz.com/p/938951.html

回到顶部