Skip to content

Random Rectangles

2 messages · David Arnold, Jim Lemon

#
On 01/10/2013 07:37 AM, David Arnold wrote:
Hi David,
There are a number of ways to generate "random" rectangles, for instance:

# each row specifies the number of rows and columns of squares
rr.df<-data.frame(nrow=sample(1:12,100,TRUE,prob=12:1),
  ncol=sample(1:12,100,TRUE,prob=12:1))

Then just plot the resulting rectangles:

sqrect<-function(x0,y0,x1,y1) {
  nx<-x1-x0-1
  ny<-y1-y0-1
  for(x in 0:nx) {
   for(y in 0:ny)
    rect(x0+x,y0+y,x0+x+1,y0+y+1)
  }
}

rrPlot<-function(rrdf,div=1.3) {
  nrect<-dim(rrdf)[1]
  plotspace<-nrect/div
  plot(c(1,plotspace),c(1,plotspace),type="n",
   axes=FALSE,xlab="",ylab="",main="Random Rectangles")
  xpos<-ypos<-maxypos<-1
  for(rectangle in 1:nrect) {
   if(xpos+rrdf[rectangle,1] > plotspace) {
    xpos<-1
    ypos<-maxypos
    maxypos<-1
   }
   sqrect(xpos,ypos,xpos+rrdf[rectangle,1],
    ypos+rrdf[rectangle,2])
   xpos<-xpos+rrdf[rectangle,1]+1
   if(ypos+rrdf[rectangle,2] > maxypos)
    maxypos<-ypos+rrdf[rectangle,2]+2
  }
}

The example above does not do any sophisticated placing of the 
rectangles, but more importantly, shows that there are probably unstated 
constraints on the "randomness" of the rectangles.

Jim