-1

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()
AMC
  • 2,642
  • 7
  • 13
  • 35
  • 1
    [Formatting help](https://stackoverflow.com/editing-help)... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Jul 06 '20 at 18:31
  • Related - [Angles between two n-dimensional vectors in Python](https://stackoverflow.com/questions/2827393/angles-between-two-n-dimensional-vectors-in-python) – wwii Jul 06 '20 at 18:50

1 Answers1

0

arccos is returning the angle in radians, not degrees.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18
  • how do i return the angle in degrees? – Erico123 Jul 06 '20 at 18:32
  • @Erico123 180 * result / math.pi ? Pretty much every trig function in a programming language will deal with radians. You should get used to working with them, or at least converting them when necessary. There's also math.degrees() and math.radians to do the conversions. – Max Jul 06 '20 at 18:47