2012-10-16 104 views
2

好的,我有一個基本腳本來繪製一個對象的軌跡。我有基本的運動方程來解決物體相對於時間的位置。情節本身是對象軌跡的3D表示。刪除軸範圍外的值?

我已經成功地獲得了軸限制設置,現在我想確保我沒有看到任何超出軸限制的軌跡值。現在,軌跡落在x-y平面之下並繼續向下,在3D陰謀之外......有什麼方法可以防止這種情況發生?

這裏是整個代碼:

import matplotlib as mpl 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 
import matplotlib.pyplot as plt 

mpl.rcParams['legend.fontsize'] = 10 

fig = plt.figure() 
ax = fig.gca(projection='3d') 


### Define Variables ### 
time = (10)  #Time to calculate the equations 
t = np.linspace(0, time, 100) 

g = (9.81)  #Gravity 

vxo = (3)  #initial velocity in the x-direction 
vyo = (1)  #initial velocity in the y-direction 
vzo = (0)  #initial velocity in the z-direction 

xo = (0)  #initial x-position 
yo = (0)  #initial y-position 
zo = (9)  #initial z-position 


### Equations of Motion ### 
x = (xo + (vxo * t)) 
y = (yo + (vyo * t)) 
z = (10 - (.5 * g * (t**2))) 


ax.plot(x, y, z, label=('Trajectory of Soccer Ball ' + str(time))) 
ax.legend() 

### Set axis limits ### 
ax.set_xlim3d(0,10) 
ax.set_ylim3d(0,10) 
ax.set_zlim3d(0,10) 
plt.show() 

回答

0

如果你的目標要超越你的軸的界限;這是因爲方程式告訴它這樣做。在繪製它們之前,您必須先限制結果並過濾。例如:

x = equation1() 
if(x > someLimit) 
    handle error/object bounces by inverting the direction perhaps 
plot(x)