4

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Samra
  • 875
  • 1
  • 9
  • 15
  • 1
    Only number of points ? or do you have the co-ordinates also? – Naveen Aug 19 '09 at 05: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 06: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 06: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 06:10
  • Did you even try to do this yourself? – chrips Nov 06 '18 at 14:16
  • related question https://stackoverflow.com/questions/34479435/fit-rectangle-around-points – Hamed Mar 09 '22 at 12:21

5 Answers5

9

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().

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • I am unable to find the this method . Can u give me the precise for this thanks – Samra Aug 19 '09 at 07:45
  • It's here http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#WMkTvnMDpLM/cv/src/cvrotcalipers.cpp&q=cvMinAreaRect2 – ZZ Coder Aug 19 '09 at 11:10
5

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.

p00ya
  • 3,659
  • 19
  • 17
  • I think it will help me .. more precisely i have to calculate the width and length of polygon !!! – Samra Aug 19 '09 at 06: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 07:33
1

Follow the following algorithm

  1. Rotate the polygon onto the XY plane
  2. Pick 1 edge and align this edge with the X axis(use arctan). Use the min/max x,y to find the bounding rectangle. Compute Area and store in list
  3. Do the same for remaining edges in clipped polygon.
  4. Pick the rectangle with the minimum Area.
  5. Rotate Bounding Rectangle back for Coplanar reverse rotation of Step 1 and step 2

for more detail check link Minimum-Area-Rectangle

Temp
  • 51
  • 6
1

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

cyber-monk
  • 5,470
  • 6
  • 33
  • 42
-1

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.

Mark Bessey
  • 19,598
  • 4
  • 47
  • 69
  • thanks mark for ur comment Yes i need the rectanle at any angle. Could you plz elaborate optimization process – Samra Aug 19 '09 at 06: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 05:25