I've found a problem with the detection of angle types (whether inner or outer wrt the origin of axes), given a set of polylines. I've found dozens of very similar questions, but none of them solved my problem, so I put it here in hope something comes out.
All I have is a set of polylines. I need to find the angles (with a given tolerance close to rectangles), and classify them as inner or outer.
For each polyline I take the vertices 3 by 3, and I am able to recognize whether the central one is or is not an angle, and measure its value as a number between 0 and 180 degrees.
Now I need to give this angle a direction (let's say a sign, negative if the acute is aiming away from the origin, positive if it aims toward the center), and I thought I'd implement it with one of the 2 methods below, but none of them worked.
1) Just the 'sign of a 2d crossproduct' (i know this is not mathematically correct terminology):
//given 3 contiguous vertices a,b,c
//check if b is a inner (+1) or outer (-1) vertex (0 in other cases)
double cross = ((b.x - a.x)*(c.y - a.y)) - ((b.y - a.y)*(c.x - a.x));
if(cross > 0){
return 1;
} else if (cross < 0) {
return -1;
}
return 0;
But it seems it only works in the bottom left quadrant, in the top right works exactly in the opposite way, while it screws in the others and i can't understand why.
2) Compare the norm of the vertices
if b.norm() < a.norm() && b.norm() < c.norm
then return +1
else return -1
This only works with basic cases, and overall no polylines crossing the axes (embracing the origin). I can check all the cases, but I'd rather avoid that.
Obviously, there are far safer methods, such as checking if the vertex is on the same side of the origin compared to a line passing between 2 neighbours lying on the 2 vectors.. but i need to optimize it as much as possible.