#================================================== # for the hexagonal lattice, find the x-y positions # for the grid #================================================== get.hex.grid <- function(grid.size) { # Create a rectangular grid of the required size # expand.grid creates an array of grid points # corresponding to specifies x and y values. # The first col. changes more rapidly than the second. hex.pos <- expand.grid(1:grid.size[2],1:grid.size[1])-c(1,1) # Swap the cols so the second changes faster. hex.pos <- hex.pos[,c(2,1)] # Shift every other row by 0.5 to create hexagon hex.pos[,1] <- hex.pos[,1]+rep(c(0,.5), (grid.size[2]+1)/2)[1:grid.size[2]] # make distances to all neighboring units equal hex.pos[,2] <- hex.pos[,2]*sqrt(0.75) return(hex.pos) } ################# get.Uhex.grid <- function(grid.size) { # Create a grid of the required size (see get.hex.grid) hex.pos <- expand.grid(1:grid.size[2],1:grid.size[1])-c(1,1) # Swap the rows/cols hex.pos <- hex.pos[,c(2,1)] # Shift every other row by 0.5 to create hexagon # We need multiples of 4 for row shifts of (0,.5,1,.5) m.4 <- (grid.size[2]%/%4 + 1) hex.pos[,1] <- hex.pos[,1]+rep(c(0,.5,1,.5), m.4)[1:grid.size[2]] hex.pos[,2] <- hex.pos[,2]*sqrt(0.75) # make distances to all neighboring units equal return(hex.pos) }