This is my first post on this Q&A site!
In the following, I will present you some code I wrote and on which I got my head stuck on. It is about computing the inner angle between the two vectors. I perhaps know there have been a lot of questions revolving around this topic but I came here with the hope of maybe getting some individual to look at my code and tell me, why it doesn't print out the anticipated answer.
If I for example use vector a = [-8 , -13, -9]
and vector b = [0, 0, 1]
, I should be getting 30° as a result.
This, however, is not the case. I suspect that my error has to do with the wrong implementation of the arccos
function:
import math
import numpy as np
def converter():
print("-+-+-+-+-+- Winkelberechnung zwischen Gerade und Gerade -+-+-+-+-+-")
rx1 = float(input("X- Wert (Richtungsvektor #1): "))
ry1 = float(input("Y- Wert (Richtungsvektor #1): "))
rz1 = float(input("Z- Wert (Richtungsvektor #1): "))
rx2 = float(input("X- Wert (Richtungsvektor #2): "))
ry2 = float(input("Y- Wert (Richtungsvektor #2): "))
rz2 = float(input("Z- Wert (Richtungsvektor #2): "))
rich1 = [rx1, ry1, rz1]
rich2 = [rx2, ry2, rz2]
# Winkel Berechnung: Skalarprodukt durch Beträge - davon den arccos?
skala = rich1[0] * rich2[0] + rich1[1] * rich2[1] + rich1[2] * rich2[2]
b_von_r1 = math.sqrt(rich1[0]**2 + rich1[1]**2 + rich1[2]**2) # winkel_test
b_von_r2 = math.sqrt(rich2[0]**2 + rich2[1]**2 + rich2[2]**2)
angle = np.arccos(skala / (b_von_r1 * b_von_r2))
if angle > 90:
angle = 180 - angle
else:
pass
print(angle)
converter()