使用Python3绘制心形,你可以选择多种方法,这里提供两种常见的方法:
方法一:使用Turtle Graphics
from turtle import *
from time import sleep
def go_to(x, y):
up()
goto(x, y)
down()
def big_Circle(size):
speed(1)
for i in range(150):
forward(size)
right(0.3)
def small_Circle(size):
speed(1)
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(1)
color('red')
调用函数绘制心形
heart(0, 0, 100)
调用函数绘制箭头
arrow()
结束绘制
done()
方法二:使用matplotlib和numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
生成爱心点
x_lim = np.linspace(-10, 10, 150)
y_lim = np.linspace(-10, 10, 150)
z_lim = np.linspace(-10, 10, 150)
X_points = []
Y_points = []
Z_points = []
for x in x_lim:
for y in y_lim:
for z in z_lim:
if (x2 + (9/4)*y2 + z2 - 1)3 - (9/80)*y2*z3 - x2*z3 <= 0:
X_points.append(x)
Y_points.append(y)
Z_points.append(z)
绘制爱心
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X_points, Y_points, Z_points, c='red')
plt.show()
以上代码分别展示了如何使用Turtle Graphics和matplotlib库来绘制心形。你可以根据自己的喜好和需求选择合适的方法进行尝试。