2013-09-19 4 views
-1

Я работаю над проектом с векторами, но теперь я хочу рассчитать и построить результирующий вектор векторов, которые имеют большее значение, например, между красным и синим. Это код:Как построить результирующий вектор

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
from itertools import product, combinations 
from numpy import sin, cos 
from matplotlib.patches import Rectangle, Circle, PathPatch 
import mpl_toolkits.mplot3d.art3d as art3d 


fig = plt.figure() 
ax = fig.gca(projection='3d') 
ax.set_aspect("auto") 
ax.set_autoscale_on(True) 


#dibujar cubo 
r = [-1, 1] 
for s, e in combinations(np.array(list(product(r,r,r))), 2): 
    if np.sum(np.abs(s-e)) == r[1]-r[0]: 
     ax.plot3D(*zip(s,e), color="b") 

#dibujar punto 
ax.scatter([0],[0],[0],color="g",s=100) 

#dibujar vector 
from matplotlib.patches import FancyArrowPatch 
from mpl_toolkits.mplot3d import proj3d 

class Arrow3D(FancyArrowPatch): 
    def __init__(self, xs, ys, zs, *args, **kwargs): 
     FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs) 
     self._verts3d = xs, ys, zs 

    def draw(self, renderer): 
     xs3d, ys3d, zs3d = self._verts3d 
     xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M) 
     self.set_positions((xs[0],ys[0]),(xs[1],ys[1])) 
     FancyArrowPatch.draw(self, renderer) 
print "ingrese coordenada inicial" 
#m=float(raw_input()) 
a = Arrow3D([0,0],[0,1],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="k") 
b = Arrow3D([0,-1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r") 
c = Arrow3D([0,0],[0,0],[0,1], mutation_scale=20, lw=1, arrowstyle="-|>", color="b") 
d = Arrow3D([0,0],[0,0],[0,-1], mutation_scale=20, lw=1, arrowstyle="-|>", color="g") 
e = Arrow3D([0,1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="c") 
f = Arrow3D([0,0],[0,-0.5],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="m") 

ax.add_artist(a) 
ax.add_artist(b) 
ax.add_artist(c) 
ax.add_artist(d) 
ax.add_artist(e) 
ax.add_artist(f) 
plt.show() 

Помогла ли вам это сделать? Спасибо!

+0

Я не понимаю вашего вопроса. С чем вы столкнулись? – tacaswell

+0

Мне нужно построить результат между стрелками 'b' и 'c'. –

+0

ОК, так в чем проблема с вашим кодом? – tacaswell

ответ

0

Ваш вопрос содержит ответ, как у вас есть класс, который будет рисовать стрелки между произвольными точками в 3-пространстве

from itertools import cycle 
a = [0, 0, 1] 
b = [1, 0, 1] 
orig = [0, 0, 0] 
for (_b, _e), _c in zip([[orig, a], [orig, b], [a, b]], cycle(['m', 'r', 'g', 'b'])): 
    xs, ys, zs = zip(_b, _e) 
    res = Arrow3D(xs, ys, zs, mutation_scale=20, lw=1, arrowstyle="-|>", color=_c) 
    ax.add_artist(res) 
+0

Отлично! Но почему это меняет цвета стрелок, поэтому я пытался нарисовать результат желтым, но я не мог! : S –

+0

изменить 'g' -> 'y' – tacaswell

+0

@ DiegoMartínezGiardini Это не имеет для меня никакого смысла. – tacaswell

Смежные вопросы