'efficient way to calculate groups of points on a grid

Say I have an evenly spaced 6 x 6 grid of points:

width      <- 6
N          <- width^2
x_cent     <- rep(seq(from = 0, to = 1, length.out = width), times = width)
y_cent     <- rep(seq(from = 0, to = 1, length.out = width), each = width)
dat <- tibble::as_tibble(cbind(x_cent, y_cent)) %>% 
  dplyr::mutate(unit_id = rep(1:nrow(.)))

I want to calculate/ know all the possible ways to arrange 2 x 4 (e.g. length/width of 2 and 4) groups of points (accounting for a "residual" group of 4 points which does not need to be spatially grouped). Is there an efficient way to calculate or iterate over these groups in R? Note that these groups should not overlap with one another; they should all be spatially distinct.

I have tried to write an algorithm that creates a 2x4 or 4x2 (e.g. either horizontally or vertically placed) group at (0, 0) that then tries to fit the remaining points to groups, but I haven't been able to write enough rules to guide the algorithm to correctly group points together.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source