views:

39

answers:

1

Is there a way better than a brute force comparison to get the max and min length diagonals of a polygon? To be more specific, I would like to find the ratio, so I can sort polygon on their "skinniness."

The polygons aren't too large (usually 4-8 faces per polygon), but there's a lot of them. I thought I'd just check with SO to see if there was a better way of doing this.

Thanks in advance

+2  A: 

The polygons aren't too large (usually 4-8 faces per polygon), but there's a lot of them.

I don't know if there's a faster solution than O(n^2), but for n <= 8 it's not gonna matter. If n = 8, you just have to check 20 diagonals (8 * 5 / 2). It's not so big multiplier itself, and any complex algorithm is likely to have a lot of computational overhead (data structures, sophisticated loops and checks).

One thing you can do to speed it up, though, is to throw away square root in the formula of distance between two points. First find min/max of (xi-xj)*(xi-xj) + (yi-yj)*(yi-yj), then apply square root. It's quite expensive operation and doing it 2 times instead of 20 could make a difference.

Nikita Rybak
+1 for skipping the expensive square rooting while finding min/max.
Albin Sunnanbo
Actually, I just realized this was a bad question, because it doesn't solve my problem of classifying polygons as thin or round, since it's very possible that there is a min diagonal smaller than the width :\. Thanks for the answer, but it looks like Im going to have to find another way to do this
Xzhsh
How about the ratio of area to perimeter? You haven't given a metric for "skinniness" but intuitively, it seems that for a given perimeter, a smaller area would be "skinny".
bbudge