当前位置:网站首页>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!
扫描下方二维码立即订阅
往期回顾
分享
点收藏
点点赞
点在看
边栏推荐
- HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
- express hot-reload
- 请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
- SQL语句查询字段内重复内容,并按重复次数加序号
- mySQL数据库初始化失败,有谁可以指导一下吗
- The difference between beautiful MM and ordinary MM
- 阿里云存储的数据库是怎么自动加快加载速度的呢www.cxsdkt.cn怎么设置案例?
- Chapter 12 Bayesian Networks
- 苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
- ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
猜你喜欢
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
Creo 9.0 基准特征:基准轴
DTcloud 装饰器
【LeetCode】623. Add a row to the binary tree
只有一台交换机,如何实现主从自动切换之nqa
链表中的数字相加----链表专题
使用HBuilder离线本地打包ipa教程
What is a good movie to watch on Qixi Festival?Crawl movie ratings and save to csv file
复现一次循环和两次循环
mySQL数据库初始化失败,有谁可以指导一下吗
随机推荐
sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
16 kinds of fragrant rice recipes
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
DPU — 功能特性 — 存储系统的硬件卸载
Creo 9.0 基准特征:基准点
好资料汇总
代码审计—PHP
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
thinkPHP5 realizes clicks (data increment/decrement)
接口全周期的生产力利器Apifox
Xcode 12 ld: symbol(s) not found for architecture armv64
【Excel实战】--图表联动demo_001
The toss of MM before going to the street (interesting)
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
How to make pictures clear in ps, self-study ps software photoshop2022, simple and fast use ps to make photos clearer and more textured
并发之CAS
请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f