 # Math problem when comparing vector (3d) angles [Solved]

I’m comparing two 3d Vectors using Dot Product, but I keep getting strange results. I compare the yellow Vector3d (n), a face normal, with the “cube-direction” Vector3d a. The Box is planar (XY) and therefore it regards only the Planar (XY) angle of vector a. But when I try calculate the angle between the two vectors I don’t get expected results(n is not parallel to n (0 degree) where it should be parallel):

In the code below the variable `m_dirToDelete` is the vector “a” pictured above :

``````if ( m_dirToDelete.Length > 0 )
{
// Test the face normal direction angle against the m_dirToDelete direction.
// Remove the MeshFace if less than x° degree tolerance (+/- 1°).
//
Vector3d n = normal;
Vector3d a = m_dirToDelete;

// Project both vectors down to XY plane (planarize)
//
a.Z = 0;
n.Z = 0;

// Get the Dot product
//
a.Unitize();
n.Unitize();

double dot = a.X * n.X + a.Y * n.Y; // + a.Z * n.Z;  0-values are meaningless.
double deg = Math.Acos(dot) * (180/Math.PI);

// Record  MeshFace index, if the vectors are within angle tolerance of each other
//
if ( deg < 1 && deg > -1 )
{
//face index to be deleted
}
}
``````

So, something is wrong with the math, since I know that there are several yellow faces (n) having the same (planar XY) direction as the red arrow (a).

What am I doing wrong?

// Rolf

Hi Rolf, double check that the facenormals actually are perpendicular to the face. I just noticed that if I weld a rhino mesh box then the vertice vectors aren’t pointing out correctly at the average angles. So resetting the vectors might help you out.

Hi @RIL,

You might consider using Vector3d.VectorAngle.

– Dale

Arghhh, I found out that the “direction” vector (a) wasn’t exactly parallel with the box.

In the GH definition I had assumed that I could rotate the Box with the angle of this vector (in the XY plane) without first “planarizing” the vector. Not planarizing the vector before using it to rotate the Box resulted in a angle deviation (~5 deg) between the box and the vector, a deviation which exceeded the angle tolerance I used in my code (+/- 1 deg).

So, my math was correct in the code while the input vector (a) didn’t actually have the direction I thought it had (i.e. the box face normal, a planarized “a”, being congruent to the yellow face normal (n)).

@dale , yes, I had used VectorAngle in my code before, but due to the deviation, VectorAngle function suffered from the same problem. I also have other use for some of the var iables being used in this lower level math.

Yup, there it was, i.e. the Box and the vector “a” was off about 5 degrees. Thanks! // Rolf

Edit: The “fix” (in the light red area) that did all the difference (feeding a 3D rotation of the Box):