求問怎樣用python/python turtle畫「心」呢?

剛剛起步python,覺得python turtle真是萌神一般的存在呀~ 試著畫了顆「心」,不過覺得方法實在是太笨了&> &< 想求問各位老小夥伴,還有什麼方法可以python/python turtle畫「心」呢?


from turtle import *
def curvemove():
for i in range(200):
right(1)
forward(1)
color("red","pink")
begin_fill()
left(140)
forward(111.65)
curvemove()
left(120)
curvemove()
forward(111.65)
end_fill()
done()

How can I draw a heart using Python?


2333自學Python一周,剛寫好的。歡迎交流,分享

#heart

from turtle import *
from time import sleep

def go_to(x, y):
up()
goto(x, y)
down()

def big_Circle(size): #函數用於繪製心的大圓
speed(10)
for i in range(150):
forward(size)
right(0.3)

def small_Circle(size): #函數用於繪製心的小圓
speed(10)
for i in range(210):
forward(size)
right(0.786)

def line(size):
speed(1)
forward(51*size)

def heart( x, y, size):
go_to(x, y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()

def arrow():
pensize(10)
setheading(0)
go_to(-400, 0)
left(15)
forward(150)
go_to(339, 178)
forward(150)

def arrowHead():
pensize(1)
speed(5)
color("red", "red")
begin_fill()
left(120)
forward(20)
right(150)
forward(35)
right(120)
forward(35)
right(150)
forward(20)
end_fill()

def main():
pensize(2)
color("red", "pink")
#getscreen().tracer(30, 0) #取消注釋後,快速顯示圖案
heart(200, 0, 1) #畫出第一顆心,前面兩個參數控制心的位置,函數最後一個參數可控制心的大小
setheading(0) #使畫筆的方向朝向x軸正方向
heart(-80, -100, 1.5) #畫出第二顆心
arrow() #畫出穿過兩顆心的直線
arrowHead() #畫出箭的箭頭
go_to(400, -300)
write("author:寶寶可乖了", move=True, align="left", font=("宋體", 30, "normal"))
done()

main()


from turtle import *

pensize(1)

color("black","red")

speed(2)

up()

goto(-12,100)

down()

begin_fill()

left(90)

circle(120,180)

circle(360,70.529)

left(38.942)

circle(360,70.529)

circle(120,180)

end_fill()

up()

goto(-250,-150)

down()


一行代碼:

print"
".join(["".join([("YourLove"[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3&<=0 else" ")for x in range(-30,30)])for y in range(15,-15,-1)])


用python matplotlib畫笛卡爾的心形線

import numpy as np

import pylab as plt

from matplotlib import colors

a = [[] for i in range(1000)]

i = 0

while i &< 1000:

j = 0

while j &< 1000:

x = -1.8 + 0.003*i

y = -1.4 + 0.0028*j

z = y**2 + (-x - (y**2)**0.33333)**2

if z &<= 1:

a[i].append(0.9)

else:

a[i].append(0.0)

j = j + 1

i = i + 1

cmap = colors.ListedColormap(["white", "pink"])

im = plt.imshow(a, cmap = cmap, interpolation="bicubic" )

plt.show()


大家自己畫吧~

import turtle

turtle.hideturtle()
canvas = turtle.getcanvas()
drawing = False
color = "red"
bgcolor = "white"
canvas.config(background = bgcolor)

last_point = (-1, -1)

def draw_point(event):
global last_point

x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
canvas.create_rectangle(x, y, x, y, fill = color)
last_point = (x, y)

def draw_line(event):
global drawing, last_point
if(drawing):
return
drawing = True
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
canvas.create_line(last_point[0], last_point[1], x, y, fill = color)
last_point = (x, y)
drawing = False
def stop_drawing(event):
global last_point
last_point = (-1, -1)
canvas.bind("&", draw_point)
canvas.bind("&", draw_line)
canvas.bind("&", stop_drawing)

turtle.mainloop()


import sys
import math

def frange(start, end, step=1.0):
if step &> 0:
while start &< end: yield start start += step elif step &< 0: while start &> end:
yield start
start += step
else:
raise ValueError("range() step must not be zero")

def f(x, y, z):
a = x*x + 9.0/4*y*y + z*z - 1
return a*a*a - x*x*z*z*z - 9.0/80*y*y*z*z*z

def h(x, z):
for y in frange(1.0, 0.0, -0.001):
if f(x, y, z) &<= 0: return y return 0.0 if __name__ == "__main__": for z in frange(1.5, -1.5, -0.1): for x in frange(-1.5, 1.5, 0.05): v = f(x, 0, z) if v &<= 0: y0 = h(x, z) ny = 0.01 nx = h(x + ny, z) - y0 nz = h(x, z + ny) - y0 nd = 1.0/math.sqrt(nx*nx+ny*ny+nz*nz) d = (nx + ny - nz)*nd*0.5 + 0.5 sys.stdout.write(".:-=+*#%@"[int(d*5)]) else: sys.stdout.write(" ") sys.stdout.write(" ")

-------------------------------------------------------------------------------------------------

import sys

def frange(start, end, step=1.0):
if step &> 0:
while start &< end: yield start start += step elif step &< 0: while start &> end:
yield start
start += step
else:
raise ValueError("frange() step must not be zero")

if __name__ == "__main__":
for y in frange(1.5, -1.5, -0.1):
for x in frange(-1.5, 1.5, 0.05):
z = x*x + y*y - 1
f = z*z*z - x*x*y*y*y
if f &<= 0: sys.stdout.write("*") else: sys.stdout.write(" ") sys.stdout.write(" ")

就醬


Python 3.x 畫心程序 情人節拿去不謝

1、打開Python 3.5 Shell。

2、選擇左上角的「File」、點擊「New File」

3、在白板中粘貼以下代碼

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0,2*np.pi, 0.1)
x = 16*np.sin(t)**3
y = 13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)-np.cos(4*t)
plt.plot(x,y,color = "red")
plt.show()

4、選擇「File」、點擊「Save」,命名為「dannysgift.py」,保存

5、選擇「Run」,點擊「Run Module」

6、顯示結果


1.用CAD或者GeoGebra建模,取數據。

2.from turtle import Turtle

代碼如下:

from turtle import Turtle
from math import *

def draw_heart(radius1,extent,deg_set1):
for i in range(4):
if radius1[i]&>0:
radius1[i] = sqrt(radius1[i])
else:
radius1[i] = -sqrt(-radius1[i])
p.setheading(deg_set1[i])
p.circle(radius1[i]*40,extent[i])

if __name__ == "__main__":
radius1 =[139.53,4366.02,-10.8,-11.2]
extent = [17.61,3.43,77.93,157.63]
deg_set1 = [124.24,142.06,140.09,68.19]

radius2 =[-139.53,-4366.02,10.8,11.2]
deg_set2 = [55.76,37.94,39.91,111.81]

p = Turtle()
p.penup()
p.goto(0,-200)
p.pendown()
p.color("red")
p.pensize(2)
p.speed(10)

draw_heart(radius1,extent,deg_set1)
p.penup()
p.goto(0,-200)
p.pendown()
draw_heart(radius2,extent,deg_set2)
p.hideturtle()


自學兩天,正好學用turtle畫畫,腦洞一開就想要畫心,畫了很多次都是首尾不接的狀態,而且內部的取值是根據固定大小,嘗試著拼湊出來的參數。

看了下知乎和百度上的其他答案,是比較高級的畫法,暫時還沒學,就用簡單的畫法了。

花了半天時間研究和修改,完成了自己的執念(一定要首尾相接),並且可以自由設置心的大小,都能由程序自動放大縮小而不改變形狀。

和題主分享一下:

代碼:

import turtle

import math

def drawHeart(rad):

turtle.circle(rad, 225)

turtle.fd(rad*math.tan(math.pi/8*3))

turtle.circle(0, 90)

turtle.fd(rad*math.tan(math.pi/8*3))

turtle.circle(rad, 225)

def main():

turtle.setup(500, 500, 0, 0)

turtle.seth(90)

pythonsize = 30

turtle.pensize(pythonsize)

turtle.pencolor("red")

drawHeart(40)

main()

希望能夠有所幫助。願向大家學習,共同進步!


畫出來的丑,略有其形

import turtle

turtle.fillcolor("pink")

turtle.begin_fill()

turtle.left(90)

turtle.circle(50,180)

turtle.left(180)

turtle.circle(50,180)

turtle.left(45)

turtle.forward(100*2**0.5)

turtle.left (90)

turtle.forward(100*2**0.5)

turtle.end_fill ()


我東拼西湊也做一個,體驗一下。

# -*- coding: utf-8 -*-

# file:plot.py

# matplotlib 繪圖演示
# 參考資料:http://blog.csdn.net/jenyzhang/article/details/52046372
# 心形線:http://www.sohu.com/a/54786554_372453

import numpy as np
import matplotlib.pyplot as plt
import math

# 設置X,Y軸的數值
# 一種心形線的表達方式
# x = 16*(sin(t))**3
# y = 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)

t=np.linspace(0,2*math.pi,3000)
x=16*(np.sin(t))**3
y=13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)-np.cos(4*t)

# 創建繪圖對象
plt.figure(figsize=(8,8))

# 在當前繪圖對象進行繪圖(兩個參數是x,y軸的數據)
plt.plot(x,y,color="red",linew=2)

# 關閉坐標軸(也沒有四邊外框,僅有圖像)
plt.axis("off")

# 關閉坐標刻度(但還有四條邊框)
#plt.xticks([])
#plt.yticks([])

# 填充紅色
plt.fill(x,y,"red")

# 提示:要先保存,再顯示。順序反了,會只得到一張空白圖片
# 保存圖片
# .png文件能實現背景透明
#plt.savefig("plot.png",dpi=300,bbox_inches="tight", transparent=True)
plt.savefig("plot.jpg",dpi=300)

# 顯示圖
plt.show()

PNG背景透明效果的,也做了一個。


from pylab import*
t=linspace(0,2*pi,100)
x=sin(2*t) + 2*sin(t)
y=-cos(2*t)-2*cos(t)
fill(x,y,"r")
show()


推薦閱讀:

Python的return如何理解?
Python這麼熱,會不會很快出現市場飽和,比如五年內?
if __name__ == __main__ 如何正確理解?
怎樣用 Windows 入門Python?
Python 有哪些入門學習方法和值得推薦的經典教材?

TAG:Python | 計算機 | Python入門 |