-3

I am trying to generate a vector between the origin (0,0,0) to a specific point. How do I turn the math into code?

Edit:

Sorry about the unclarity.

Let's say that I have point o (0,0,0) and point p (3,6,8) that sit in a 3d coordinate system.

How can I calculate the vector from point o in the direction of point p?

4
  • 2
    By writing a formula... Jul 10, 2018 at 18:55
  • The question as it is is too broad. Please show some contex, e.g. in the shape of a program which defines the point data in a suitable data structure and e.g. is able to output the result (even if it is hard coded to the obviously wrong (1;2;3)).
    – Yunnosch
    Jul 10, 2018 at 18:58
  • At least show the math. A vector from the origin to any point in 3D can be represented by 3 coordinates.
    – pault
    Jul 10, 2018 at 19:01
  • Just what do you mean by "point" and "vector" in python? There is no standard meaning for those--there are multiple implementations. Please show example input, expected output, and some of your own attempts at a solution. Jul 10, 2018 at 19:01

2 Answers 2

4

Like others I am a bit unclear. From what I understand all you want is a vector that will take you from one point to another point. This is just basic vector addition and is very simple. I made so it works for any dimension not just the third.

def multiDimenDist(point1,point2):
   #find the difference between the two points, its really the same as below
   deltaVals = [point2[dimension]-point1[dimension] for dimension in range(len(point1))]
   runningSquared = 0
   #because the pythagarom theorm works for any dimension we can just use that
   for coOrd in deltaVals:
       runningSquared += coOrd**2
   return runningSquared**(1/2)
def findVec(point1,point2,unitSphere = False):
  #setting unitSphere to True will make the vector scaled down to a sphere with a radius one, instead of it's orginal length
  finalVector = [0 for coOrd in point1]
  for dimension, coOrd in enumerate(point1):
      #finding total differnce for that co-ordinate(x,y,z...)
      deltaCoOrd = point2[dimension]-coOrd
      #adding total difference
      finalVector[dimension] = deltaCoOrd
  if unitSphere:
      totalDist = multiDimenDist(point1,point2)
      unitVector =[]
      for dimen in finalVector:
          unitVector.append( dimen/totalDist)
      return unitVector
  else:
      return finalVector

EDIT, forgot to include usage:

 vector = findVec([0,0,0],[3,6,8])# you can also set unitCircle to True
 >>[3,6,8]
1

Your question is not clear but here is an example how to plot some 3D vectors

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

vectors=np.array( [ [0,0,1,1,-2,0], [0,0,2,1,1,0],[0,0,3,2,1,0],[0,0,4,0.5,0.7,0]]) 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for vector in vectors:
    v = np.array([vector[3],vector[4],vector[5]])
    vlength=np.linalg.norm(v)
    ax.quiver(vector[0],vector[1],vector[2],vector[3],vector[4],vector[5],
              pivot='tail',length=vlength,arrow_length_ratio=0.3/vlength)
ax.set_xlim([-4,4])
ax.set_ylim([-4,4])
ax.set_zlim([0,4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.