当前位置:网站首页>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!
扫描下方二维码立即订阅
往期回顾
分享
点收藏
点点赞
点在看
边栏推荐
猜你喜欢
pytorch余弦退火学习率CosineAnnealingLR的使用
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
Code Audit - PHP
Weekly Report 2022-8-4
为什么我推荐使用智能化async?
使用HBuilder离线本地打包ipa教程
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
Creo 9.0 基准特征:基准轴
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
随机推荐
spark集群部署(第三弹)
在colab里怎样读取google drive数据
画法几何及工程制图考试卷A卷
egg框架
XSS靶机通关以及XSS介绍
The Secrets of the Six-Year Team Leader | The Eight Most Important Soft Skills of Programmers
请问大佬们 ,使用 Flink SQL CDC 是不是做不到两个数据库的实时同步啊
控制器-----controller
15.1.1、md—md的基础语法,快速的写文本备忘录
让程序员崩溃的N个瞬间(非程序员误入)
程序员的七种武器
IT研发/开发流程规范效能的思考总结
汇编语言(8)x86内联汇编
16 kinds of fragrant rice recipes
Going to book tickets tomorrow, ready to go home~~
Controller-----controller
Code Audit - PHP
Linux导出数据库数据到硬盘
【 a daily topic 】 1403. The increasing order of the sequence, boy
DTcloud 装饰器