当前位置:网站首页>Hundred lines of code launch red hearts, why programmers lose their girlfriends!
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
2022-08-05 09:08:00 【AI Technology Base Camp】

Qixi is a traditional Chinese folk festival,不同时代、People from different regions give this festival different meanings.在漫长的演变过程中,Tanabata becomes the day when the Cowherd and Weaver Girl meet.Because of this beautiful love legend,Qixi Festival is regarded as the most romantic festival in China、festival of love.Against the backdrop of the west wind,Qixi has become Chinese again“情人节”.
作者 | 许向武
责编 | 张红月
Since it's Valentine's Day,Romantic programmers always like to come up with tricks,Add some color to a stressful and dull life.今天给各位带来的是3DAnimated Cupid's Sword of Love:A golden cupid love sword stabs a pulsating red heart in slow motion,The hearts of the host will can't control to fall in love withTAfirst person seen.
我猜,The Cowherd and the Weaver Girl must have fallen in love after being shot by Cupid's sword of love,毕竟,They all live in heaven——in western culture,木星(Jupiter)It's Cupid,金星(Venus)Is Venus.
Before Cupid's appearance The Sword of Love,先用matplotlib绘制一个2Dhearts warm up.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi/2, np.pi/2, 1000)
y1 = np.power(np.cos(x),0.5) * np.cos(200*x) + np.power(np.absolute(x),0.5) - 0.7
y2 = np.power(4-np.power(x, 2), 0.01)
plt.plot(x, y1*y2, c='r')
plt.show()
Heart shape formed by up and down curves
If you zoom in a bit,You will find that the red heart above is actually a curve that fluctuates continuously up and down.red heart below,is a true closed heart,It's just made up of four curves connected end to end.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0, 2, 300)
x2 = np.linspace(0, -2, 300)
y11 = (np.power(x1, 2/3) + np.power(np.power(x1, 4/3)-4*np.power(x1, 2)+4, 0.5))/2 - 0.12
y12 = (np.power(x1, 2/3) - np.power(np.power(x1, 4/3)-4*np.power(x1, 2)+4, 0.5))/2
y21 = (np.power(-x2, 2/3) + np.power(np.power(-x2, 4/3)-4*np.power(-x2, 2)+4, 0.5))/2 - 0.12
y22 = (np.power(-x2, 2/3) - np.power(np.power(-x2, 4/3)-4*np.power(-x2, 2)+4, 0.5))/2
plt.plot(x1, y11, c='r')
plt.plot(x1, y12, c='r')
plt.plot(x2, y21, c='r')
plt.plot(x2, y22, c='r')
plt.show()
Heart shape composed of four curves connected end to end
那么,Can you draw a heart shape with one stroke??当然可以,极坐标方程 r = Arccos(sinθ)just a heart,It's just not perfect.
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2*np.pi, 1000)
x = np.arccos(np.sin(theta)) * np.cos(theta)
y = np.arccos(np.sin(theta)) * np.sin(theta)
plt.plot(x, y, c='r')
plt.show()
心形线
是时候展示3Dred heart.运行下面的代码,除了需要numpy模块,还需要安装wxgl模块——基于PyOpenGL的三维数据绘图工具包.
pip install wxgl关于wxglModule for more details,Please click to play for ten minutes3D绘图:WxGL完全手册,here is the Chinese document.
下面是3Dversion of red heart drawing code:
import numpy as np
import wxgl.glplot as glt
a = np.linspace(0, 2*np.pi, 500)
b = np.linspace(0.5*np.pi, -0.5*np.pi, 500)
lons, lats = np.meshgrid(a, b)
w = np.sqrt(np.abs(a - np.pi)) * 2
x = 2 * np.cos(lats) * np.sin(lons) * w
y = -2 * np.cos(lats) * np.cos(lons) * w
z = 2 * np.sin(lats)
glt.mesh(x, y, z, color='crimson') # crimson - 绯红
glt.show()是不是很简单?wxglThe style of use is almost the same asmatplotlib完全一致.拖动鼠标,就可以看到这个3DThe version of the hearts of around before and after.

3D红心
接下来,This code encapsulates the generation of red heart data with a function,Then call the custom model transformation function,This red heart is beating.
import numpy as np
import wxgl.glplot as glt
def red_heart(r, slices=100, thick=2.0, shift=(0,0,0)):
"""Returns the vertex data of hearts"""
a = np.linspace(0, 2*np.pi, slices)
b = np.linspace(0.5*np.pi, -0.5*np.pi, slices)
lons, lats = np.meshgrid(a, b)
w = np.sqrt(np.abs(a - np.pi)) * thick
x = r * np.cos(lats) * np.sin(lons) * w + shift[0]
y = -r * np.cos(lats) * np.cos(lons) * w + shift[1]
z = r * np.sin(lats) + shift[2]
return x, y, z
def heart_beat(t):
"""心跳函数"""
t %= 1000
if t < 300:
scale = 1 + t/3000
elif t > 700:
scale = 1 + (1000-t)/3000
else:
scale = 1.1
return (scale,)
glt.mesh(*red_heart(2), color='crimson', transform=heart_beat)
glt.show()Click the play button on the left sidebar,Will according to this red heart60次/heart rate beats in seconds.熟悉了wxgl的使用,It's not hard to read the complete code of Cupid's arrow of love below.
import numpy as np
import wxgl
import wxgl.glplot as glt
def red_heart(r, slices=100, thick=2.0, shift=(0,0,0)):
"""Returns the vertex data of hearts"""
a = np.linspace(0, 2*np.pi, slices)
b = np.linspace(0.5*np.pi, -0.5*np.pi, slices)
lons, lats = np.meshgrid(a, b)
w = np.sqrt(np.abs(a - np.pi)) * thick
x = r * np.cos(lats) * np.sin(lons) * w + shift[0]
y = -r * np.cos(lats) * np.cos(lons) * w + shift[1]
z = r * np.sin(lats) + shift[2]
return x, y, z
def heart_beat(t):
"""心跳函数"""
t %= 1000
if t < 300:
scale = 1 + t/3000
elif t > 700:
scale = 1 + (1000-t)/3000
else:
scale = 1.1
return (scale,)
def arrow_fly(t):
"""Cupid's Arrow Flight Function"""
t %= 4000
if t > 2000:
return ((0,-2,40-t/100),)
else:
return ((0,-2,0),)
def heart_fly(t):
"""Cupid's Arrow Flight Function"""
t %= 4000
if t > 2000:
return ((0,0,1,90), (0,-2,40-t/100))
else:
return ((0,0,1,90), (0,-2,0))
glt.figure(azim=50, elev=16, style='gray') # set initial azimuth、高度角、背景色
glt.mesh(*red_heart(2), color='crimson', transform=heart_beat) # Drawing beating hearts
x, y, z = red_heart(0.2, thick=3.0, shift=(0,-8,0)) # Generate Heart Arrow Vertex Data
glt.mesh(x, -z, y, color='crimson', transform=heart_fly) # draw heart arrow
light = wxgl.SunLight(roughness=0, metalness=0, shininess=0.5) # Arrow shaft lights
glt.cylinder((0,0,-8), (0,0,9), 0.1, color='goldenrod', transform=arrow_fly, light=light) # draw arrow shaft
vs = [(-1,1,11), (1,-1,11), (1,-1,6), (-1,1,6),(-1,-1,11), (1,1,11), (1,1,6), (-1,-1,6)] # Nock Vertex Data
texture = wxgl.Texture(r'res\feather.png') # Arrow tail feather texture
texcoord = [(0,1), (0,0), (1,0), (1,1),(0,1), (0,0), (1,0), (1,1)] # nock texture coordinates
light = wxgl.BaseLight() # Arrow tail lights
glt.quad(vs, texture=texture, texcoord=texcoord, transform=arrow_fly, light=light) # draw nocks
glt.show()wxglThe toolbar provides recordinggif和MP4文件的功能,下面的gif就是使用wxglBuilt-in recording function.

The texture of the feather is used in the code,Please download the pictures,and save to the location specified by the code.


不止七夕,让《新程序员》Accompany you every day and night!
Where to subscribe on Tanabata Day《新程序员》的朋友
都将获得CSDNA complimentary custom mug!
扫描下方二维码立即订阅

往期回顾
分享
点收藏
点点赞
点在看边栏推荐
- C语言-数组
- Rotation of the displayed value on the button
- egg框架
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- 16种香饭做法全攻略
- 阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
- Is there a problem with writing this?How to synchronize data in sql-client
- Controller-----controller
- 让程序员崩溃的N个瞬间(非程序员误入)
- NC20164 :最大数MAXNUMBER [线段树]
猜你喜欢
随机推荐
画法几何及工程制图考试卷A卷
Does flink cdc support synchronization from oracle dg library?
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
Overall design and implementation of Kubernetes-based microservice project
随时牵手 不要随意分手[转帖]
基于 Kubernetes 的微服务项目整体设计与实现
CCVR基于分类器校准缓解异构联邦学习
MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
JS syntax usage
十一道家常小菜详细攻略[图文并茂]
Hbuilder 学习使用中的一些记录
阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
【Excel实战】--图表联动demo_001
The toss of MM before going to the street (interesting)
Constellation ideal lover
DPU — 功能特性 — 存储系统的硬件卸载
love is a sad song
好资料汇总
openpyxl to manipulate Excel files









