[R] Find a rectangle of maximal area

Hans W Borchers hwborchers at googlemail.com
Sun Mar 21 12:12:17 CET 2010


For an application in image processing -- using R for statistical purposes -- I
need to solve the following task:

Given n (e.g. n = 100 or 200) points in the unit square, more or less randomly
distributed. Find a rectangle of maximal area within the square that does not
contain any of these points in its interior.

If a, b are height and width of the rectangel, other constraints may have to be
imposed such as  a, b <= 0.5  and/or  0.5 <= a/b <= 2.0 . The rectangle is
allowed to touch the border of the square.

For each new image the points will be identified by the application, like all
stars of a certain brightness on an astronomical picture. So the task will have
to be performed several times.

I assume this problem is computationally hard. I would like to find a solution
that is reasonably fast for  n = 100..200  points. Exhaustive search along the
x, y coordinates of the points will not be fast enough.

I know this request is not about R syntax and does not contain 'repro-code'. But
perhaps a somehow similar problem has been solved before.

Thanks in advance for any suggestions,
Hans Werner

P.S.: Example "Is this rectangle of maximal area?"
----
n <- 100; set.seed(832)
x <- runif(n); y <- runif(n)
plot(c(0,1), c(0,1), type="n", axes=FALSE, asp=1,
     xlab="", ylab="", main="Rectangle Problem", sub="")
rect(0,0,1,1, col="darkblue")
xl<-100; yu<-43; xr<-40; yo<-3
area <- (x[xr]-x[xl])*(y[yo]-y[yu])
rect(x[xl], y[yu], x[xr], y[yo],
     lty=2, col="darkblue", border="red")
text((x[xl]+x[xr])/2, (y[yu]+y[yo])/2,
     paste("area =", round(area, 4)), cex=0.75, col="red")
points(x, y, pch=20, col="white")
----



More information about the R-help mailing list