问题描述:

Hey guys this is a bit of a homework puzzle I'm working on and my trig isn't too strong so bear with me.

I have a list of three vertexes and I've already figured out how to calculate the internal angle where they meet (I'm using this to test to make sure they have a valid angle so the polygon is a valid polygon).

Currently I pluck out the three vertices in a row, then calculate the edges to the vertex I want the angle on, then get the acos of the vector product at that point:

 double dx21 = one.x - two.x;

double dx31 = three.x - two.x;

double dy21 = one.y - two.y;

double dy31 = three.y - two.y;

double m12 = Math.sqrt(dx21*dx21 + dy21*dy21);

double m13 = Math.sqrt(dx31*dx31 + dy31*dy31);

double theta = Math.acos((dx21*dx31 + dy21*dy31)/ (m12 * m13));

I know nominally I could grab the external angle by subtracting the internal angle from 360 degrees, but this is a sanity check to make sure that the polygon is valid (The vertices are in counter clockwise order).

The note I was given was to make sure the sin of the vector product at the vertexes was positive, but I've been playing on this for a while on paper and am not really having any luck getting this to work.

I know it's mostly a maths question but any advice would be really useful.

网友答案:

Here are a few notes.

First, the external angle as it's usually defined would be 180 degrees - the interior angle, or pi - the interior angle, depending on your units. See here. A quick sanity check would be to add the external angles together to be sure you get a circle.

Second, you may know this but I should point out that Math.acos returns an angle in radians, not degrees.

Third, note that the cosine can't distinguish between a convex and a concave angle, which means that your interior angle method is only valid for convex polygons. You might be better off working with the cross product, which will give you the sine of the angle.

Finally, getting to your question, here's a hint: You look at the vectors from p2->p1 and p2->p3; when I read the problem, I pictured each the vectors from p1->p2 and p2->p3. Either one is a perfectly valid way to solve the problem, but think about how the angles between these pairs of vectors are different, and how that relates to your problem.

相关阅读:
Top