Search code examples
3dgeometry

How do you compute the angle between two edges in a polygon in 3D space?


I want to compute the angle between two edges in a face in 3D space.

I thought I could used the solution in this question: Signed angle between two 3D vectors with same origin within the same plane

However, when I try it out on rectangle with all 90 degree angles I get one angle that's 90 degrees and three that's 270.

I have very little knowledge of math related to geometry, which is why I struggle with this and clearly made the wrong assumption.

So how do you calculate the angle between two edges in a face?

What I tried:

def self.full_angle_between( vector1, vector2, normal )
  angle = vector1.angle_between( vector2 )
  direction = normal % ( vector1 * vector2 )
  angle = 360.degrees - angle if direction < 0.0
  angle
end

normal is the face normal vector1 and vector2 both origins from the same vertex and each points towards the next vertex in either direction

vector1.angle_between( vector2 ) refer to a method in the SketchUp Rubu API: http://code.google.com/apis/sketchup/docs/ourdoc/vector3d.html#angle_between It return an angle between 0-180 in radians.

360.degrees yields the degrees in radians. Also an SketchUp API method.

When I iterate over all the vertices in a rectangle I get three angles reported as 270 degrees. Why?


Solution

  • I would say your problem lies in this line:

    direction = normal % ( vector1 * vector2 )
    

    It's not wrong in itself, but you might be passing in the wrong values.

    If you're not fully up to speed on vector maths, the thing you may be missing is that the direction of the vector given by the cross product vector1 * vector2 depends on the order of the operands. If you swap them around you get the same vector pointing in the opposite direction.

    So when you're iterating over the rectangle vertices, if you get the "next" and "previous" vertices mixed up you will get the bogus 270 degree results you describe.