How to get Positive and Negative Degrees (Angle Node)?

Hello everyone,
I am using the Angle node to get radians between two vectors. Later I convert them to Degrees, but I only get Positive(+) degrees. I need both Positive (+) and Negative (-) degrees to move ahead. I am attaching my grasshopper file below.



Angle problem.gh (15.4 KB)

The Angle node in RhinoCommon returns the angle between two vectors in radians, and this value is always positive. If you want to get a signed angle in degrees (i.e. an angle that can be positive or negative), you can use the following approach:

double signedAngleInDegrees = angleInDegrees * Vector3d.VectorProduct(vec1, vec2).Z;

The Vector3d.VectorProduct(vec1, vec2) calculates the vector product of the two vectors, which has the direction of the angle of rotation. The Z component is positive for counter-clockwise rotations, and negative for clockwise rotations. So, by multiplying the angle in degrees by the Z component of the cross product, you will get the angle in degrees, with a positive or negative value depending on the rotation direction of the angle.

Alternatively, you could also use the Vector3d.UnsignedAngle method, and then use Math.Sign method of the Math class to get the sign of the angle and multiply it by the angle obtained from the UnsignedAngle Method.

3 Likes

@RANE @farouk.serragedine fwiw I found this to be useful:

def signedVectorAngle(v1, v2, plane):
    """
    Calculates the signed vector angle between two
    vectors v1 and v2.
    """
    # signed angle calculation, see:
    # https://stackoverflow.com/a/33920320
    return math.atan2(Vector3d.CrossProduct(v1, v2) * plane.ZAxis, v1 * v2)
2 Likes

You need to provide a plane (World XZ) so that the component calculates an oriented angle, otherwise it just takes the absolute value. See more ideas in this thread !

I like the cross/dot product approach also, it’s doable with components.

Angle problem.gh (25.2 KB)

@farouk.serragedine @Gijs @magicteddy
The def works thank you…