Home > Mobile >  How to recursively divide a square into different parts?
How to recursively divide a square into different parts?

Time:08-24

I have a data.frame containing the coordinates of a square.

square = data.frame(xmin = c(0),
                xmax = c(1),
                ymin = c(0),
                ymax = c(1))

Using ggplot2, I can plot the square:

library(ggplot2)

ggplot(square, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax))  
        geom_rect()  
        coord_fixed()

which produces:

enter image description here

Now, I want to have a function, that takes a data.frame containing the four coordinates of a square and divides this square into 4 rectangles, giving the first 3 rectangles a separate colour and leaving the last one uncoloured:

enter image description here

Then, I want this function to divide the last/uncoloured rectangle into 4 squares, giving the first 3 squares a separate colours (but the ordering of the colours should match with the ones in rectangles) and leaving the last square uncoloured:

enter image description here

This would be the end of the first iteration. But, I want to use this function iteratively and recursively.

For example, if the iteration number is defined to be 2, I want this function to take the coordinates of the last/uncoloured square and repeat the processes explained above.

So, if we repeat this whole function 2 times, we would end up with similar figures:

enter image description here

enter image description here

I want to repeat this process many times. Also, I would prefer this recursive function to produce a data.frame so that I can use ggplot2 to visualise it later.

What I have tried so far is a lot of hardcoding the coordinates to make the rectangles and squares. Obviously it is a big problem as I want to repeat this process many times. It would take significant amount of time to manually hardcode the plot.

CodePudding user response:

The following code initiates a first dataframe with the row rule and a column rule.
The second part of the code applies the same formula splitting each time each part of the square.

### Initiating magnificent colors
Colors <- c("#00a5da", "#2bec00", "#6b00ec", "#fff3b9")

### Initiating first dataframe
dfCols <- data.frame(xmin = seq(0, 0.75, 0.25),
                     xmax = seq(0.25, 1, 0.25),
                     ymin = rep(0, 4),
                     ymax = rep(1, 4))
dfRows <- data.frame(xmin = rep(0.75, 4),
                     xmax = rep(1, 4),
                     ymin = seq(0, 0.75, 0.25),
                     ymax = seq(0.25, 1, 0.25))
dfBoth <- rbind(dfCols, dfRows)

### Looping
for(i in 1:4){
dfCols <- data.frame(xmin = dfCols$xmin[4]   dfRows$ymax[1]*0:3/4, 
                         xmax = dfCols$xmin[4]   dfRows$ymax[1]*1:4/4, 
                         ymin = rep(dfRows$ymin[1], 4), 
                         ymax = rep(dfRows$ymax[1], 4))
dfRows <- data.frame(xmin = rep(dfCols$xmin[4], 4), 
                         xmax = rep(dfCols$xmax[4], 4), 
                         ymin = dfRows$ymax[1] - dfRows$ymax[1]*4:1/4,
                         ymax = dfRows$ymax[1] - dfRows$ymax[1]*3:0/4)
dfBoth <- rbind(dfBoth, dfCols, dfRows)
}

### Plotting
ggplot(dfBoth, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax))  
  geom_rect()   
  coord_fixed()   
  annotate("rect", xmin=dfBoth$xmin, xmax=dfBoth$xmax, ymin=dfBoth$ymin, ymax=dfBoth$ymax, fill=rep(Colors, nrow(dfBoth)/4), alpha=1)

enter image description here

  • Related