实战:基于 GitHub awesome-math 空间几何资源实现三维向量运算可视化

三维向量运算可视化实现

1. 核心数学原理

空间几何中三维向量的关键运算:

  • 向量加法:$\vec{a} + \vec{b} = (a_x+b_x, a_y+b_y, a_z+b_z)$
  • 点积运算:$\vec{a} \cdot \vec{b} = a_xb_x + a_yb_y + a_zb_z$
  • 叉积运算:$\vec{a} \times \vec{b} = (a_yb_z - a_zb_y, a_zb_x - a_xb_z, a_xb_y - a_yb_x)$
  • 标量投影:$\text{proj}_{\vec{b}} \vec{a} = \frac{\vec{a} \cdot \vec{b}}{|\vec{b}|}$
2. 可视化实现方案
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

class Vector3D:
    def __init__(self, x, y, z):
        self.coords = np.array([x, y, z])
    
    def __add__(self, other):
        return Vector3D(*(self.coords + other.coords))
    
    def dot(self, other):
        return np.dot(self.coords, other.coords)
    
    def cross(self, other):
        return Vector3D(*np.cross(self.coords, other.coords))
    
    def visualize(self, ax, color='r', label=''):
        ax.quiver(0, 0, 0, *self.coords, 
                 color=color, arrow_length_ratio=0.1, label=label)

# 创建可视化场景
def plot_vectors(vectors, operations=None):
    fig = plt.figure(figsize=(10, 8))
    ax = fig.add_subplot(111, projection='3d')
    
    # 绘制坐标轴
    ax.quiver(0, 0, 0, 5, 0, 0, color='k', alpha=0.3)
    ax.quiver(0, 0, 0, 0, 5, 0, color='k', alpha=0.3)
    ax.quiver(0, 0, 0, 0, 0, 5, color='k', alpha=0.3)
    
    # 绘制输入向量
    colors = ['r', 'g', 'b']
    for i, vec in enumerate(vectors):
        vec.visualize(ax, color=colors[i], label=f'$\\vec{{v_{i+1}}}$')
    
    # 绘制运算结果
    if operations:
        for op_name, result in operations.items():
            result.visualize(ax, color='m', label=op_name)
    
    ax.set_xlim([-3, 3])
    ax.set_ylim([-3, 3])
    ax.set_zlim([-3, 3])
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.legend()
    plt.show()

# 示例使用
if __name__ == "__main__":
    v1 = Vector3D(1, 2, 1)
    v2 = Vector3D(-1, 1, 2)
    
    operations = {
        'Sum $\\vec{v_1} + \\vec{v_2}$': v1 + v2,
        'Cross $\\vec{v_1} \\times \\vec{v_2}$': v1.cross(v2)
    }
    
    plot_vectors([v1, v2], operations)

3. 可视化效果说明

执行代码将显示:

  1. 红色箭头:向量 $\vec{v_1} = (1,2,1)$
  2. 绿色箭头:向量 $\vec{v_2} = (-1,1,2)$
  3. 品红色箭头:
    • 向量和:$\vec{v_1} + \vec{v_2} = (0,3,3)$
    • 叉积结果:$\vec{v_1} \times \vec{v_2} = (3,-3,3)$
4. 扩展应用
  1. 动态交互:集成滑块控件实时调整向量分量
  2. 投影可视化:添加标量投影的平面表示
  3. 坐标系变换:实现基变换后的向量表示
  4. 物理应用:模拟力矩 $\vec{\tau} = \vec{r} \times \vec{F}$

提示:完整项目需结合awesome-math中空间几何的数学证明资源,确保运算的数学严谨性。可视化代码可扩展为Web应用使用Plotly.js实现浏览器端交互。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值