Question

I wrote a quick hack to generate the coordinates of the endpoints of all "cell walls" in an a plain old array of squares on integer coordinates.

dimx <- 4
dimy <- 5
xvert<-rep(1:(dimx+1),each=dimy)
yvert<-1:dimy
yvert<-rep(yvert,times=dimx+1)
vertwall<-cbind(xvert, xvert,yvert,yvert+1)

And similarly for the horizontal walls. It feels like I just reinvented some basic function, so: Faster, Better, Cleaner?

EDIT: consider a grid of cells. The bottom-left cell's two walls of interest have the coordinate x,y pairs (1,1),(1,2) and (1,1),(2,1) . Similar to the definition of crystal unit cells in solid-state physics, that's all that is required, as the next cell "up" has walls (1,2),(1,3) and (1,2),(2,2) and so on. Thus the reason for repeating the "xvert" data in my sample.

Était-ce utile?

La solution

I am not sure to understand what do you try to do ( your column names are duplicated and this is confusing). You can try this for example:

df = expand.grid( yvert= seq_len(dimy),xver= seq_len(dimy))
transform(df,xvert1=xvert,yvert1=yvert+1)

CGW added for completeness' sake: generate both horizontal and vertical walls:

df = expand.grid( xvert= seq_len(dimx),yvert= seq_len(dimy))
transform(df,xvert1=xvert,yvert1=yvert+1) ->dfv
df2 <- expand.grid(yvert= seq_len(dimy), xvert= seq_len(dimx))
transform(df2,yvert1=yvert,xvert1=xvert+1) ->dfh
#  make x,y same order in both arrays
dfh[] <- dfh[,c(2,1,4,3)]

Autres conseils

The expand.grid function creates Cartesian products of arrays, which provides most of what you need to do.

expand.grid(x=1:5,y=1:5)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top