当前位置: 首页 > news >正文

如何用Python把篮球和鸡联系起来

文章目录

    • 画个球
    • 让球转起来

画个球

不管篮球和不和鸡联系起来,都首先得有个球,或者说要有一个球面,用参数方程可以表示为

x=rcos⁡ϕcos⁡θy=rcos⁡ϕsin⁡θz=rsin⁡ϕ\begin{aligned} x &= r\cos\phi\cos\theta\\ y &= r\cos\phi\sin\theta\\ z &= r\sin\phi \end{aligned} xyz=rcosϕcosθ=rcosϕsinθ=rsinϕ

当然,有球还不行,还得有篮球。篮球其实很好画,只要在球上加两个背带就可以了,这一点可以用Python来实现,但考虑到方便,还是用PS直接P了一下,希望最后画出来不是太离谱。

那么现在球有了,还必须得有鸡,所以在百度上找一只。然后把这只鸡映射到球面上。

在这里插入图片描述

接下来就是关键步骤,如何将这个平面卷成一个球?方法也很简单,只需进行颜色映射就行了。

import numpy as np
import matplotlib.pyplot as plt
path = "bracken1.jpg"
img = plt.imread(path)
#img = img[::5, ::5, :]
h, w, c = img.shape
ys, xs = np.indices([h, w])
th = xs/w*np.pi*2
phi = np.pi/2 - ys/h*np.pix = np.cos(phi)*np.cos(th)
y = np.cos(phi)*np.sin(th)
z = np.sin(phi)cs = [tuple(c/255) for c in img.reshape(-1,3)]
ax = plt.subplot(projection='3d')
ax.scatter(x, y, z, marker='.', c=cs)
plt.axis('off')
plt.show()

效果为

在这里插入图片描述

让球转起来

当然需要注意的一个是,这是个球,而不是一个圆,所以下面让这个球转一下。想要让球转动,那就得有一个旋转矩阵,三个方向的旋转矩阵如下表

Rx(θ)R_x(\theta)Rx(θ)Rx(θ)R_x(\theta)Rx(θ)Rx(θ)R_x(\theta)Rx(θ)
[1000Cθ−Sθ0SθCθ]\begin{bmatrix}1&0&0\\0&C_\theta&-S_\theta\\0&S_\theta&C_\theta\\\end{bmatrix}1000CθSθ0SθCθ[Cθ0Sθ010−Sθ0Cθ]\begin{bmatrix}C_\theta&0 &S_\theta\\0&1&0\\-S_\theta&0&C_\theta\\\end{bmatrix}Cθ0Sθ010Sθ0Cθ[CθSθ0−SθCθ0001]\begin{bmatrix}C_\theta &S_\theta&0\\-S_\theta&C_\theta&0\\0&0&1\end{bmatrix}CθSθ0SθCθ0001

由于只需绕Z轴转动,所以代码如下

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animationcos = lambda th : np.cos(np.deg2rad(th))
sin = lambda th : np.sin(np.deg2rad(th))Rz = lambda th : np.array([[cos(th) , -sin(th), 0],[sin(th), cos(th), 0],[0       , 0,       1]])xyz = np.array([x,y,z]).reshape(3,-1)fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(projection='3d')
ax.grid()lines = ax.scatter(x, y, z, marker='.', c=cs)def animate(n):# 按照xyz顺序旋转axis = [2,1,0]shape = xyz.shapelines._offsets3d = Rz(n)@xyzreturn lines,ani = animation.FuncAnimation(fig, animate, range(0, 360, 2), interval=25, blit=True)#plt.show()
ani.save("zyx.gif")

效果如下,还挺有喜感的。

在这里插入图片描述

http://www.lryc.cn/news/26106.html

相关文章:

  • 【RocketMQ】消息的刷盘机制
  • AMBA-AXI(一)burst 传输-INCR/WRAP/Fixed
  • Java知识复习(八)Spring基础
  • WuThreat身份安全云-TVD每日漏洞情报-2023-02-27
  • 上海交大陈海波教授、夏虞斌教授领衔巨作上市:《操作系统:原理与实现》
  • dpi数据接入shell脚
  • Easyrecovery数据恢复软件工作原理及使用介绍教程
  • 【面试题】社招中级前端笔试面试题总结
  • 设备运行状况不能远程手机查看。难道就妥协吗?为何不试试这个办法
  • 重新认识 Java 中的内存映射(mmap)
  • 224. 基本计算器
  • 微信小程序通过 node 连接 mysql——方法,简要原理,及一些常见问题
  • uni-app项目搭建和代码托管
  • win10+python3.6+cuda9+pytorch1.1.0安装
  • 【2023】某python语言程序设计跟学第二周内容
  • spring源码篇——BeanDefinition的注册
  • virtualbox7虚拟机中安装苹果macOS big sur系统详细教程
  • 用spectralayers 简单去一下人声做个伴奏
  • 高峰对话|深度探讨「多云与边缘」
  • 开发手册——一、编程规约_2.常量定义
  • Sandstorm 建设者亮点——2023 年 2 月
  • MyBatis快速入门
  • Mysql的一些提权方式(mysql提权、UDF)
  • 【2023】DevOps、SRE、运维开发面试宝典之Docker相关面试题
  • 圣杯布局的实现方式
  • RecastDemo用法
  • IIC总线式驱动开发(mpu6050)(二)
  • 盘点一下那些远程办公的神仙公司
  • Spring Cloud Alibaba全家桶(四)——微服务调用组件Feign
  • 安装pytorch