I'm implementing an algorithm to dertermine if a polygon is convex. I believe(correct me if I'm wrong) this algorithm here(Loren Pechtel answer) will determine if a polygon, self intersecting or not, is convex. To test this I'm using a crossed rectangle, but i do not know which are the interior angles. I assume Loren is talking about the interior angles but i could be wrong.
Im using this(second line) definition of an interior angle. Does this definition even make sense? Like the interior angles are then arbitrary depending on which interior angle you start with. So will the answer be meaningful? Do interior angles even make sense in the context of self intersecting polygons? Because Lorens algorithm is dependent on interior angles.
I do not think these are strictly the interior angles. Are these the interior angles?
Update I have found something important. While the definition above is somewhat correct for interior angles the sum of the interior angles must be modifed by a factor. The relevant info can be found under Extension to crossed polygons
Update Question Now my question becomes, how do i find k? The wiki says k is the number of total (360°) revolutions one undergoes by walking around the perimeter of the polygon. Im not sure how to find this k. What exactly does it mean by walking around the perimeter? Could someone give me an example using a crossed rectangle.
The wiki gives this formula 180(n - 2k) where n is the number of vertices and k is the number of 360 rotations. In combination with Lorens formula we get 180n - 180(n-2k) == 360 means its convex. This formula actually reveals the fact that the original picture of interior angles I posted are in fact the interior angles of the polygon. or at least according to this formula. and if k is equal to one doesnt that mean the polygon is simple and if not one the polygon is complex for all cases?
Another approach would be to see if the lines intersect. If they do its a concave polygon. I think Ive heard of an algorithm that can do this relatively fast.
This is my implementation to figure out k.
pub fn revolutions(&self) -> f64 {
let mut total = 0f64;
for i in 0..self.vertices.len() {
let j = (i + 1) % self.vertices.len();
let k = (i + 2) % self.vertices.len();
let ii = &self.vertices[i];
let jj = &self.vertices[j];
let kk = &self.vertices[k];
let a = vec2_sub(*jj, *ii);
let b = vec2_sub(*kk, *jj);
total += ((a[0] * b[0] + a[1] * b[1]) / (vec2_len(a) * vec2_len(b))).acos();
}
total / (std::f64::consts::PI * 2f64)
}
and here is a picture of the angles i got when walking around the perimeter of a crossed square. walking around the perimeter