30岁的程序员拥有20岁的浪漫,用Python画3D玫瑰送女朋友
Posted yunyun云芸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了30岁的程序员拥有20岁的浪漫,用Python画3D玫瑰送女朋友相关的知识,希望对你有一定的参考价值。
1.天生丽质
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
# 将相位向后移动了6*pi
[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 20 * np.pi + 4*np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
# 添加边缘扰动
change = np.sin(15*t)/150
# 将t的参数减少,使花瓣的角度变大
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = u * (x * np.cos(p) - y * np.sin(p))
c= cm.get_cmap('Reds')
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,
cmap= c, linewidth=0, antialiased=True)
plt.show()
2.姹紫嫣红
mport numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = u * (x * np.cos(p) - y * np.sin(p))
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,
cmap=cm.gist_rainbow_r, linewidth=0, antialiased=True)
plt.show()
3.粉黛俏佳人
mport numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 30 * np.pi - 4*np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
change = np.sin(20*t)/50
u = 1 - (1 - np.mod(3.3 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
c= cm.get_cmap('magma')
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,
cmap= c, linewidth=0, antialiased=True)
plt.show()
4.桃之夭夭
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
[x, t] = np.meshgrid(np.array(range(25)) / 24.0, np.arange(0, 575.5, 0.5) / 575 * 6 * np.pi - 4*np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
change = np.sin(10*t)/20
u = 1 - (1 - np.mod(5.2 * t, 2 * np.pi) / np.pi) ** 4 / 2 + change
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p)) * 1.5
h = u * (x * np.cos(p) - y * np.sin(p))
c= cm.get_cmap('spring_r')
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1,
cmap= c, linewidth=0, antialiased=True)
plt.show()
5.思路步骤
以姹紫嫣红为例
1.
花的花瓣总是围绕中心轴生长,我们不妨将花看做一个中心旋转3D图。在此基础上建立柱状极坐标系。在极坐标系中,图像上的每个点有三个自由度(r,t,h)(其中t表示theta)
2.
观察花瓣生长规律,我们发现,其外边缘线在一条旋转内缩的曲线上。这条曲线里中心轴的距离r逐渐缩短,距离地平面h逐渐变高。且该曲线大概符合以下公式:
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
r = np.sin(p)
h = np.cos(p)
3.
花瓣状乘类似于水滴状关于中心轴旋转。因此r和h都是关于模式u的函数:
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
r = u * np.sin(p)
h = u * np.cos(p)
4.
为了图像更加真实,添加一个修正项y,使花瓣的形态向下凸,更接近真实生活:
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = u * (x * np.cos(p) - y * np.sin(p))
5.
实际上我们画出来的不是花瓣的平面,而是一个个离散的点(总共25*1150 = 28750个),经过python的plot_surface逼近花瓣的面状:
[x, t] = np.meshgrid(np.array(range(25)) / 24.0,
np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)
x,t是两个自变量,相当于参数方程中两个参数,最终表示为三维坐标系下的某点(rcos(θ),rsin(θ),h),x是在[0,25)上取的25个点,t是-2pi到15pi上取的1150个点,在3D图像中共计28750个点,绘制出的散点图通过python自带的函数连成曲面状。
福利来啦
Python300本有关的电子书籍,技术交流,工具安装包,接单交流,有趣的源码共享,领取获得。
以上是关于30岁的程序员拥有20岁的浪漫,用Python画3D玫瑰送女朋友的主要内容,如果未能解决你的问题,请参考以下文章
30岁的程序员拥有20岁的浪漫,用Python画3D玫瑰送女朋友