I have a need to calculate the minimum area rectangle (smallest possible rectangle) around the polygon.

The only input i have is the number of points in polygon.

I have the co-ordinates of the points also.

link|improve this question

0% accept rate
1  
Only number of points ? or do you have the co-ordinates also? – Naveen Aug 19 '09 at 5:59
is it a regular shape? – nickf Aug 19 '09 at 5:59
4  
Is the polygon at an arbitrary orientation or does the rectangle have to be orthogonal to the coordinate system? – Ates Goral Aug 19 '09 at 6:03
you need to have coordinates. or, have the length of a side and a restriction that all the sides have the same length. or you could answer : 4. (number of points in rectangle) hehe. – moogs Aug 19 '09 at 6:04
Yes polygon could be in arbitrary orientation and for rectangle only think it should be the samllest possible in terms of area – Samra Aug 19 '09 at 6:10
feedback

4 Answers

Use the rotating calipers algorithm for a convex polygon, or the convex hull otherwise. You will of course need the coordinates of the points in the polygon, not just the number of points.

link|improve this answer
I think it will help me .. more precisely i have to calculate the width and length of polygon !!! – Samra Aug 19 '09 at 6:12
1  
that's not problem: as you apply the algorithm, you calculate the width and length of a bounding box aligned to each edge in turn. – p00ya Aug 19 '09 at 7:33
feedback

Obviously, you'll need the coordinates of the points to get the answer. If the rectangle is aligned to the X and Y aces, then the solution is trivial. If you want the smallest possible rectangle, at any angle, then you'll need to do some sort of optimization process.

link|improve this answer
thanks mark for ur comment Yes i need the rectanle at any angle. Could you plz elaborate optimization process – Samra Aug 19 '09 at 6:18
Several people have already mentioned the rotating calipers algorithm. That's basically how it's done. You do the equivalent of the basic min/max bounding box, but with the coordinate system rotated to the angle of each of the sides. – Mark Bessey Aug 21 '09 at 5:25
feedback

This is called Minimum Bounding Box, it's most basic algorithm used in OCR packages. You can find an implementation using Rotating Calipers from the OpenCV package. Once you get the source code, check out this file,

cv/src/cvrotcalipers.cpp

The method you need is cvMinAreaRect2().

link|improve this answer
I am unable to find the this method . Can u give me the precise for this thanks – Samra Aug 19 '09 at 7:45
It's here google.com/codesearch/… – ZZ Coder Aug 19 '09 at 11:10
feedback

First do a grahm-scan and get the convex hull of the set of points. Then you can use something like minimum rectangle discussed here

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.