我需要在三维空间中绘制一个简单的动画箭头向量

2 投票
3 回答
1540 浏览
提问于 2025-04-17 13:01

我想用安卓手机的传感器数据来显示一个单独的磁场向量,也就是在空间中的一个点,带有从原点发出的箭头。我用Python写了一个简单的服务器来获取手机的传感器数据,并希望实时绘制接收到的数据。我在寻找一个简单的解决方案,但一直找不到。

我查看了matplotlib、blender和visual python,但都没有找到一个足够简单的解决方案,能够直接拿到三个坐标并绘制出来。接收到的数据就是一个包含三个点的向量。原点的箭头并不是特别重要,我只想能在三维空间中可视化这个移动的点。

另外,如果需要的话,我可以用Java重写服务器,并使用Java的绘图库。我只需要一些建议和简短的代码示例来实现这个目标。

3 个回答

1

如果你有选择C++的话,真的应该看看VTK这个东西。

http://www.vtk.org/

它非常强大,可以用来显示三维向量场,而且使用起来也挺简单的。

2

VPython 非常简单:

pointer = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=1)

只需要把 axis=(1,2,3) 改成你向量中的三个点就可以了。更多信息可以在 这里 找到。

3

你可以用VPython非常简单地绘制场景:

from visual import *
import math

def make_grid(unit, n):
    nunit = unit * n
    f = frame()
    for i in xrange(n+1):
        if i%5==0: 
            color = (1,1,1)
        else:
            color = (0.5, 0.5, 0.5)

        curve(pos=[(0,i*unit,0), (nunit, i*unit, 0)],color=color,frame=f)
        curve(pos=[(i*unit,0,0), (i*unit, nunit, 0)],color=color,frame=f)
    return f

arrow(pos=(0,0,0), axis=(5,0,0), color=(1,0,0), shaftwidth=0.1)    
arrow(pos=(0,0,0), axis=(0,5,0), color=(0,1,0), shaftwidth=0.1)    
arrow(pos=(0,0,0), axis=(0,0,5), color=(0,0,1), shaftwidth=0.1)    
grid_xy = make_grid(0.5, 10)
grid_xz = make_grid(0.5, 10)
grid_xz.rotate(angle=pi/2, axis=(1,0,0), origin=(0,0,0))
grid_yz = make_grid(0.5, 10)
grid_yz.rotate(angle=-pi/2, axis=(0,1,0), origin=(0,0,0))
sphere(radius=0.3)

obj = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=0.3)
th = 0
while True:
    rate(20)
    obj.axis = (3*math.cos(th), 3*math.sin(th), 2)
    th += 0.04

在这里输入图片描述

撰写回答