Python图形复刻——绘制母亲节花束

各位小伙伴,好久不见,今天学习用Python绘制花束。

有一种爱,不求回报,有一种情,无私奉献,这就是母爱。祝天下妈妈节日快乐,幸福永远!


图形展示:

代码展示:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap as lsc
from scipy.spatial.transform import Rotation as R
# @author : slandarer

# 生成花朵数据
t1 = np.array(range(25))/24
t2 = np.arange(0, 575.5, 0.5)/575*20*np.pi + 4*np.pi
[xr, tr] = np.meshgrid(t1, t2)
pr = (np.pi/2)*np.exp(-tr/(8*np.pi))
ur = 1 - (1 - np.mod(3.6*tr, 2*np.pi)/np.pi)**4/2 + np.sin(15*tr)/150 + np.sin(15*tr)/150
yr = 2*(xr**2 - xr)**2*np.sin(pr)
rr = ur*(xr*np.sin(pr) + yr*np.cos(pr))
hr = ur*(xr*np.cos(pr) - yr*np.sin(pr))

tb = np.resize(np.linspace(0, 2, 151), (1,151))
rb = np.resize(np.linspace(0, 1, 101), (101,1)) @ ((abs((1-np.mod(tb*5,2))))/2 + .3)/2.5
xb = rb*np.cos(tb*np.pi)
yb = rb*np.sin(tb*np.pi)
hb = np.power(-np.cos(rb*1.2*np.pi)+1, .2)

cL = np.array([[.33,.33,.69], [.68,.42,.63], [.78,.42,.57], [.96,.73,.44]])
cL = np.array([[.02,.04,.39], [.02,.06,.69], [.01,.26,.99], [.17,.69,1]])
cMpr = lsc.from_list('slandarer', cL)
cMpb = lsc.from_list('slandarer', cL*.4 + .6)

# 绕轴旋转数据点
def rT(X, Y, Z, T):
    SZ = X.shape
    XYZ = np.hstack((X.reshape(-1, 1), Y.reshape(-1, 1), Z.reshape(-1, 1)))
    RMat = R.from_euler('xyz', T, degrees = True); XYZ = RMat.apply(XYZ)
    return XYZ[:,0].reshape(SZ), XYZ[:,1].reshape(SZ), XYZ[:,2].reshape(SZ)

# 贝塞尔函数插值生成花杆并绘制
def dS(X, Y, Z):
    MN = np.where(Z == np.min(Z)); M = MN[0][0]; N = MN[1][0]
    x1 = X[M, N]; y1 = Y[M, N]; z1 = Z[M, N] + .03
    x = np.array([x1, 0, (x1*np.cos(np.pi/3) - y1*np.sin(np.pi/3))/3]).reshape((3,1))
    y = np.array([y1, 0, (y1*np.cos(np.pi/3) + x1*np.sin(np.pi/3))/3]).reshape((3,1))
    z = np.array([z1, -.7, -1.5]).reshape((3,1))
    P = np.hstack((x,y,z)).T
    t = (np.array(range(50)) + 1)/50
    c1 = np.array([1, 2, 1]).reshape(3,1)
    c2 = np.power(t, np.array(range(3)).reshape(3,1))
    c3 = np.power(1 - t, np.array(range(2, -1, -1)).reshape(3,1))
    P = (P @ (c1*c2*c3))
    ax.plot(P[0], P[1], P[2], color = '#800080')


# 创建figure窗口及axis坐标区域
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制花束
ax.plot_surface(rr*np.cos(tr), rr*np.sin(tr), hr + .35, rstride = 1, cstride = 1,
                    facecolors = cMpr(hr), antialiased = True, shade = False)
U, V, W = rT(rr*np.cos(tr), rr*np.sin(tr), hr + .35, [180/8, 0, 0]); V = V - .4
for i in range(5):
    U, V, W = rT(U, V, W, [0, 0, 72])
    ax.plot_surface(U, V, W - .1, rstride = 1, cstride = 1,
                        facecolors = cMpr(hr), antialiased = True, shade = False)
    dS(U, V, W - .1)

u1, v1, w1=rT(xb, yb, hb/2.5 + .32, [180/9, 0, 0])
v1 = v1 - 1.35
u2, v2, w2 = rT(u1, v1, w1, [0, 0, 36])
u3, v3, w3 = rT(u1, v1, w1, [0, 0, 24])
u4, v4, w4 = rT(u3, v3, w3, [0, 0, 24])
for i in range(5):
    u1, v1, w1 = rT(u1, v1, w1, [0, 0, 72])
    u2, v2, w2 = rT(u2, v2, w2, [0, 0, 72])
    u3, v3, w3 = rT(u3, v3, w3, [0, 0, 72])
    u4, v4, w4 = rT(u4, v4, w4, [0, 0, 72])
    ax.plot_surface(u1, v1, w1, rstride = 1, cstride = 1,
                        facecolors = cMpb(hb), antialiased = True, shade = False)
    ax.plot_surface(u2, v2, w2, rstride = 1, cstride = 1,
                        facecolors = cMpb(hb), antialiased = True, shade = False)
    ax.plot_surface(u3, v3, w3, rstride = 1, cstride = 1,
                        facecolors = cMpb(hb), antialiased = True, shade = False)
    ax.plot_surface(u4, v4, w4, rstride = 1, cstride = 1,
                        facecolors = cMpb(hb), antialiased = True, shade = False)
    dS(u1, v1, w1)
    dS(u2, v2, w2)
    dS(u3, v3, w3)
    dS(u4, v4, w4)

ax.set_position((-.215, -.3, 1.43, 1.43))
ax.set_box_aspect((1, 1, .8))
ax.view_init(elev = 50, azim = 2)
ax.axis('off')
plt.show()

参考文献:

作者slandarer

网址:https://mp.weixin.qq.com/s/a3QukoBQhoK46aY8ZinBrQ

相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou3 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
安静读书3 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024065 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康5 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式