# R code to find the smallest bounding box for a specified orientation

How can I devise an R-code that can derive a rectangular bounding box (with 90-degree angle on each corner) for a set of points with arbitrary orientation? e.g. if the 2 longer edges have a 45-degree angular direction, or 15-degree, or any angle. It seems to have been solved on the paper below, but I cannot read their mathematical notation.

http://www.cccg.ca/proceedings/2004/29.pdf

It is partially solved on the link below, however, it looks only for the "minimum area rectangle" and not the minimum bounding rectangle for a specified orientation.

Finding minimum-area-rectangle for given points?

• Maybe this will help? stackoverflow.com/questions/41833933/…
– GBG
Oct 17, 2017 at 16:56
• No, the orientation of the bounding box on that thread is always vertical/horizontal, it means its pair of parallel sides are always 90 degrees and 0 degrees respectively. My question is, find the smallest bounding box if the orientation of one of the parallel sides is say 45 degrees, 10 degrees, etc. Thanks for your response. Oct 17, 2017 at 23:43
• do you mean minimum oriented bounding box? QGIS has an implementation but that doesn't answer the implementation in R... but it might clarify / be a starting point? Oct 18, 2017 at 19:06
• @Steven Kay, I think this is what I am looking for, but I'm not familiar in Python coding in QGIS, but I'm willing to study it if needed. Can you please illustrate how to implement it? I suggest that you post your answer, but not in the comment section so you can earn the bounty. Thank you very much! Oct 18, 2017 at 22:25
• The solution is already provided by @whuber in gis.stackexchange.com/questions/22895/… The rectangle is being rotated, isnt that what you want??
– BERA
Oct 19, 2017 at 9:21

I guess you need Principal Component Analysis (PCA), read this publication:

On the Bounding Boxes Obtained by Principal Component Analysis

Look at this as well:

https://www.rdocumentation.org/packages/shotGroups/versions/0.1/topics/getMinBBox

• I'm not merely looking for the minimum bounding box, I need a solution wherein I can specify first the orientation of the bounding box, then get the minimum bounding. If I cannot control the orientation of the bounding box, then it won't answer my query here. Thanks for your help though. Oct 21, 2017 at 0:22
• Then rotate (constrain: alpha parameter) the points with a rotation matrix. Use the usual boundary box: rdocumentation.org/packages/spatstat/versions/1.6-1/topics/… And rotate back the box. Or, simpler: 1. Rotate the points. 2. Create corners of boundary box (4 points) with the combinations of xmin, xmax, ymin, ymax. 3. Rotate back the corner points, and create the box with convexhull for example. Oct 21, 2017 at 11:22
• I also thought about that before but rotating the points back and forth would be cumbersome when iterated on thousands of data. The orientedMinimumBounding shared by @Steven Kay may do the trick only if I am able to implement it Oct 22, 2017 at 8:00
• You needed an R solution: orientedMinimumBoundingBox (QGIS) Returns the oriented minimum bounding box for the geometry, which is the smallest (by area) rotated rectangle which fully encompasses the geometry. getMinBBox (R) Calculates the vertices of the minimum-area, possibly oriented bounding box given a set of 2D-coordinates. - They are the same. - Look at my 2. link, that's the answer for you question. Oct 22, 2017 at 10:59
• Both algorithm return the same properties by the way: angle, width, height Oct 22, 2017 at 11:20

Then rotate (constrain: alpha parameter) the points with a rotation matrix.

Use the usual boundary box: https://www.rdocumentation.org/packages/spatstat/versions/1.6-1/topics/bounding.box

And rotate back the box.

Or, simpler:

1. Rotate the points.
2. Create corners of boundary box (4 points) with the combinations of xmin, xmax, ymin, ymax.
3. Rotate back the corner points, and create the box with convexhull for example.