Check if a polygon is a convex or not ?

Monkey Forums/Monkey Programming/Check if a polygon is a convex or not ?

GC-Martijn(Posted 2011) [#1]
H!

Is there a function (i'm not good in polygon math) to check if a
Polygon:Float:[] is a convex ?

I did found some C++ code but I can't translate it to monkey :S
Maybe someone have a monkey function for this ?

Thanks very much if you will share it here :)

Greetings,
GCMartijn


Yahfree(Posted 2011) [#2]
I don't know many algorithms on this subject(so there may be a faster way).. but just from thinking about it:

Wouldn't a polygon be convex if each point is OUTSIDE a polygon formed by the other points?

So do a point-in-polygon for each point against a polygon formed by the other points.. if one returns true(is in the polygon) then it's concave, otherwise it's convex.


hardcoal(Posted 2011) [#3]
i will add math functions in the future for this kind of issues


muddy_shoes(Posted 2011) [#4]
If you'd care to provide some code that provides polygons to test then it shouldn't take much to write the function.


FlameDuck(Posted 2011) [#5]
Wouldn't a polygon be convex if each point is OUTSIDE a polygon formed by the other points?
No. A polygon is convex, if all of its interior angles (that is the angles measured on the inside of the polygon) are less than Pi (or 180 degrees). So, go through each vertex, calculate its interior angle, and if you get one that's more than Pi you have a concave polygon, if you don't you have a convex one. Simple. You can do this while you're drawing it so you don't have to iterate though it just to know.


GC-Martijn(Posted 2011) [#6]
@FlameDuck,

I already have the polygon's I need it more as a tool to check if everything is a convex.

So for exmple I have:
Poly:Float = [257.0,398.0, 146.0,398.0, 146.0, 302.0,257.0,302.0]

What is then the math to check if that is a convex or not ?
I can't do this myself.

@hardcoal
:) If you post the math (helper) functions please mail me.


Difference(Posted 2011) [#7]
Hi GCMartijn

I made a function for you here:

http://www.monkeycoder.co.nz/Community/posts.php?topic=1659


GC-Martijn(Posted 2011) [#8]
@Difference
Thanks very much, I will check it when I'm at home !