Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The required code in python 3.6
- import math
- #calculates the dotproduct of two vectors
- def dotproduct(v1,v2):
- sum = 0;
- for i,j in zip(v1,v2):
- sum = sum + (i*j)
- return sum
- #calculates magnitude of a vector
- def magnitude(v):
- return math.sqrt(dotproduct(v,v))
- #returns angle between two vector in degrees
- def angleBetweenVector(v1,v2):
- angleInRadians = math.acos(dotproduct(v1,v2)/ (magnitude(v1)*magnitude(v2)))
- angleInDegrees = angleInRadians *180 / math.pi
- return angleInDegrees
- #take user inputs
- arr = input("3D coordinates of obeserver: ")
- vo = [int(num) for num in arr.split()]
- arr = input("3D coordinates of point 1 : ")
- v1 = [int(num) for num in arr.split()]
- arr = input("3D coordinates of point 2 : ")
- v2 = [int(num) for num in arr.split()]
- #calculate displacement vector
- vo1 = [i-j for i,j in zip(v1,vo)]
- vo2 = [i-j for i,j in zip(v2,vo)]
- #output
- print("The magnitude of displacement vector(point 1 - observer) is : " + str(magnitude(vo1)))
- print("The magnitude of displacement vector(point 2 - observer) is : " + str(magnitude(vo2)))
- print("The dot product between vectors is : " + str(dotproduct(vo1,vo2)))
- print("The angle between vectors in degrees is : " + str(angleBetweenVector(vo1,vo2)))
- for indentation refer screenshot attached
- https://media.cheggcdn.com/media%2Fd38%2Fd386a700-5ad6-4b73-8f05-656eb0d80d93%2Fphpicxyrw.png
- https://media.cheggcdn.com/media%2F4b2%2F4b291b61-d360-4ab5-b240-f58a33c9a727%2Fphp5Xm9k8.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement