python实现6种炫酷花式画心,拿去送给你的girl friend吧

第一种:3d效果,可以用鼠标旋转拖动
第二种:波浪压缩效果,超炫酷
第三种:模拟鼠标绘制一箭穿心
第四种:函数图像绘制‘LOVE YOU ❤️’
第五种:模拟手动画心
第六种:自定义字符打印出心形

第一种:3d效果,可以用鼠标旋转拖动:

演示效果:

代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
 
def heart_3d(x,y,z):
    return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3
 
def plot_implicit(fn,bbox=(-1.5,1.5)):
    xmin,xmax,ymin,ymax,zmin,zmax = bbox*3
    fig = plt.figure('HEART')
    ax = fig.add_subplot(111,projection = '3d')
    A = np.linspace(xmin,xmax,80)
    B = np.linspace(xmin,xmax,30)
    A1,A2 = np.meshgrid(A,A)
 
    for z in B:
        X,Y = A1,A2
        Z = fn(X,Y,z)
        cest = ax.contour(X,Y,z+Z,[z],zdir='z',colors=('r',))
 
    for y in B:
        X,Z = A1,A2
        Y = fn(X,y,Z)
        cest = ax.contour(X,Y+y,Z,[y],zdir = 'y',colors = ('red',))
 
    for x in B :
        Y,Z=A1,A2
        X = fn(x,Y,Z)
        cest = ax.contour(X+x,Y,Z,[x],zdir = 'x',colors = ('red',))
 
    ax.set_zlim3d(zmin,zmax)
    ax.set_xlim3d(xmin,xmax)
    ax.set_ylim3d(ymin,ymax)
        
    plt.show()
 
if __name__=='__main__':
    plot_implicit(heart_3d)

第二种波浪压缩效果,超炫酷:

演示效果:

代码:


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ln, = ax.plot([], [], '-',color='r', lw=1)
time_template = 'LOVE = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def init():
    ax.set_xlim(-3,3)
    ax.set_ylim(-2, 3)
    return ln,
def update(ii):
    xdata, ydata = [], []
    for i in range(0,183):
        xi=(182-i)/100
        xdata.append(0.01*i-1.82)
        yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
        if type(yi)=='complex':
            yi=np.around(abs(yi),decimals=4)
        yi=np.around(yi, decimals=3)
        ydata.append(yi)
        
    for i in range(0,182):
        xi=i/100
        xdata.append(xi) 
        yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
        if type(yi)=='complex':
            yi=np.around(abs(yi),decimals=4)
        yi=np.around(yi, decimals=3)
        ydata.append(yi)
    ln.set_data(xdata, ydata)
    time_text.set_text(time_template %(ii))
    return ln,
ani = FuncAnimation(fig, update, np.linspace(0, 13.14, 100),init_func=init, interval=100)
ani.save('love.gif', writer='imagemagick', fps=60)
plt.show()

第三种:模拟鼠标绘制一箭穿心:

演示效果:
python实现6种炫酷花式画心,拿去送给你的girl friend吧_第1张图片
代码:

import time
import turtle  # 需安装turtle库

turtle.color('black','red')
turtle.pensize(5)
turtle.begin_fill()
turtle.penup()
turtle.goto(50,50)
turtle.pendown()
turtle.right(45)
turtle.goto(100,0)
turtle.left(90)
turtle.fd(120)
turtle.circle(50,225)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.left(135)
turtle.fd(120)
turtle.circle(50,225)
turtle.seth(90)
turtle.circle(50,225)
turtle.fd(121)
turtle.end_fill()
turtle.left(56)
turtle.penup()
turtle.goto(-210,40)
turtle.pendown()
turtle.goto(0,80)
turtle.penup()
turtle.goto(160,110)
turtle.pendown()
turtle.goto(320,140)
time.sleep(5)

第四种:函数图像绘制 ‘LOVE YOU ❤️’

演示效果:
python实现6种炫酷花式画心,拿去送给你的girl friend吧_第2张图片
代码:

import matplotlib.pyplot as plt
import seaborn
import numpy

l = numpy.arange(0, 4, 0.01)
L = 1.0 / l

theta = numpy.arange(-4, 4, 0.01)
o = 3.0 * numpy.cos(theta)
O = 3.0 * numpy.sin(theta)

v = numpy.arange(-4, 4, 0.01)
V = numpy.abs(-2.0 * v)

e = numpy.arange(-3, 3, 0.01)
E = -1.0 * numpy.abs(numpy.sin(e))

y = numpy.arange(-10, 10, 0.01)
Y = numpy.log2(numpy.abs(y))

u = numpy.arange(-4, 4, 0.01)
U = 2.0 * u ** 2

points = []
for heartY in numpy.linspace(-100, 100, 500):
    for heartX in numpy.linspace(-100, 100, 500):
        if ((heartX*0.03)**2+(heartY*0.03)**2-1)**3-(heartX*0.03)**2*(heartY*0.03)**3 <= 0:
            points.append({"x": heartX, "y": heartY})
 
heart_x = list(map(lambda point: point["x"], points))
heart_y = list(map(lambda point: point["y"], points))

fig = plt.figure(figsize=(13, 7))

ax_L = fig.add_subplot(2, 4, 1)
ax_O = fig.add_subplot(2, 4, 2)
ax_V = fig.add_subplot(2, 4, 3)
ax_E = fig.add_subplot(2, 4, 4)
ax_Y = fig.add_subplot(2, 4, 5)
ax_O_2 = fig.add_subplot(2, 4, 6)
ax_U = fig.add_subplot(2, 4, 7)
ax_heart = fig.add_subplot(2, 4, 8)

plt.plot(colos='tomato')
ax_L.plot(l, L)
ax_O.plot(o, O)
ax_V.plot(v, V)
ax_E.plot(E, e)
ax_Y.plot(y, Y)
ax_Y.axis([-10.0, 10.0, -10.0, 5.0])
ax_O_2.plot(o, O)
ax_U.plot(u, U)
ax_heart.scatter(heart_x, heart_y, s=10, alpha=0.5)

seaborn.set_style('whitegrid')

plt.show()

第五种:模拟手动画心:

演示效果:
python实现6种炫酷花式画心,拿去送给你的girl friend吧_第3张图片

代码:

# -*- coding: utf-8 -*-
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()

第六种:自定义字符打印出心形:

代码:

print('\n'.join([''.join([('ForChange'[(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相关)