用户:
大头小强查看:0 回复:1 评论:0 创建时间:2022-04-15T10:30:53
【作品展示】

【作品介绍】
画心脏线的代码
【作品源代码】
from turtle import setup, Pen, done, tracer, bgcolor, title
from math import sin, cos
class Heart():
def __init__(self, width, height):
self.heart = Pen()
self.H1 = -(width - 11) // 2 - 4
self.V1 = -(height - 11) // 2 + 4
self.H2, self.V2 = -self.H1, -self.V1
if width % 2 == 0:
self.H1 += 1
self.H2 += 1
self.h1, self.h2 = -3.5, 3.5
self.v1, self.v2 = -3.5, 3.5
self.a1 = (self.H1 - self.H2) / (self.h1 - self.h2)
self.b1 = (self.h1 * self.H2 - self.h2 * self.H1) / (self.h1 - self.h2)
self.a2 = (self.V1 - self.V2) / (self.v1 - self.v2)
self.b2 = (self.v1 * self.V2 - self.v2 * self.V1) / (self.v1 - self.v2)
self.heart.speed(3)
self.heart.pencolor('#00FFFF')
self.heart.color('#0000FF', 'sky blue')
self.heart.pensize(1)
self.drawFn(self.myFunc, 0.5 * 3.1415926, 2.5 * 3.1415926, 0.05)
self.heart.goto(30, 180)
self.heart.write('心脏线与ρ=1-sin(θ)的图像', font=('宋体', 18, 'bold'))
self.heart.ht()
done()
def myFunc(self, θ):
ρ = 1 - sin(θ)
x = ρ * cos(θ)
y = ρ * sin(θ)
return x, y
def drawFn(self, fn, lower, upper, step):
θ = lower
x, y = fn(θ)
self.heart.goto(self.tran(x, y))
self.heart.pd()
self.heart.begin_fill()
while θ < upper:
θ += step
x, y = fn(θ)
self.heart.goto(self.tran(x, y))
self.heart.pu()
self.heart.end_fill()
def tran(self, x, y):
return self.a1 * x + self.b1, self.a2 * y + self.b2
class TurtleCoordinate():
def __init__(self, width, height, step, color, speed):
self.width = width
self.height = height
self.coordinate = Pen()
self.coordinate.color(color)
self.coordinate.ht()
setup(self.width, self.height)
self.step = step
self.speed = speed
self.w = self.width // 2
self.h = self.height // 2
self.show()
def drawLine(self, x1, y1, x2, y2):
self.coordinate.pu()
self.coordinate.goto(x1, y1)
self.coordinate.pd()
self.coordinate.goto(x2, y2)
self.coordinate.pu()
def labelPoint(self, x1, y1, label):
self.coordinate.pu()
self.coordinate.goto(x1, y1)
self.coordinate.pd()
self.coordinate.write(label)
self.coordinate.pu()
def labelGridPoint(self, x, y, isVertical, text):
if isVertical:
self.drawLine(x, 2, x, 0)
self.labelPoint(x-5, y-15, text)
else:
self.drawLine(2, y, 0, y)
self.labelPoint(x-22, y-8, text)
def show(self):
self.coordinate.speed(self.speed)
self.drawLine(-self.w, 0, self.w, 0)
self.drawLine(0, -self.h, 0, self.h)
for x in range(-self.w, self.w, self.step):
self.labelGridPoint(x, 0, True, x)
self.drawLine(self.w-10, 10, self.w, 0)
self.drawLine(self.w-10, -10, self.w, 0)
self.labelPoint(self.w, 10, "x")
for y in range(-self.h, self.h, self.step):
if y != 0:
self.labelGridPoint(0, y, False, y)
self.drawLine(-10, self.h-10, 0, self.h)
self.drawLine(10, self.h-10, 0, self.h)
self.labelPoint(-10, self.h, "y")
self.coordinate.speed(1)
def hide(self):
self.coordinate.clear()
def keep():
while True:
pass
title('心脏线')
bgcolor("#000000")
tc = TurtleCoordinate(1200, 800, 100, '#0000FF', 5)
hp = Heart(1200, 800)
keep()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn