与常规网格相反,如何生成R中由矩形块组成的不规则布局?

时间:2019-06-12 11:34:02

标签: r

我正在尝试生成类似于城市布局的东西,矩形块代表建筑物,空间代表街道。我已经在同事的帮助下开发了规则的建筑物和街道网格。 挑战在于使该布置看起来更随机,或具有另一布置,例如。中心或其他。我尝试通过使用if-else构造以一些不同的角度旋转某些块,但所有块以相同的角度旋转,从而再次形成旋转的规则排列来完成此操作。这是我到目前为止使用的代码。

library(raster)
library(sp)
L=100
W=100
dx=1
x0=0.
y0=0.
z0=0.
LB=14
WB=12
sx=1    #street width in x-direction
sy=1    #street width in y- direction
Y=4
Hb=2   # building height
NB=ceiling(L/(LB+Y+sy))   # number of buildings
Nrow=ceiling(L/dx)
Ncol=ceiling(W/dx)

ymx = y0 + Nrow*dx

M <- matrix(z0, nrow=Nrow, ncol=Ncol, byrow=T)


Nbx= NB    #number of buildings in x-direction
Nby= NB    #number of buildings in y-direction

#rows and cols of buildings
Brow=ceiling(LB/dx)
Bcol=ceiling(WB/dx)

# assign values based on position
#M <- matrix(z0, nrow=Nrow, ncol=Ncol)

#------------TEST------------------------------------------------
# this function returns 1 if the point to be tested is in the polygon and 0 otherwise
# #### if a point is inside a polygon, then the vertices of a ray through the point 
# intersects with the vertices of the polygon an even number of times ########
# x, y are the arrays containing cordinates of points to be tested
# polx and poly are arrays containing cordinates of the polygon

PIP = function(x, y, polx, poly, TOL_PIP){
  n=0
  for(i in 1:length(x)){
    p1x = x[i]
    p1y = y[i]
    if(i < length(x)){
      p2x = x[i+1]
      p2y = y[i+1]
    }else{
      p2x = x[1]
      p2y = y[1]
    }
    if((poly > min(p1y,p2y)) && 
       (poly <= max(p1y, p2y)) && 
       (polx <= max(p1x, p2x))){
      if(p1y != p2y){
        x_inters = p1x + (poly-p1y)*(p2x-p1x)/(p2y-p1y);
        if(p1x==p2x || abs(polx-x_inters) <= TOL_PIP || polx < x_inters){
          n=n+1
        }
      }
    }
  }
  if(n %% 2 == 0){
    return(0)
  }
  return(1)
}

#----------------------Test the function-------------------------------
#reference insertion point

#M <- matrix(z0, nrow=Nrow, ncol=Ncol)


p1x = 0
p1y = 0
p2x = 0
p2y = LB-sy
p3x = WB
p3y = LB-sy
p4x = WB
p4y = 0

# rotate the point
ro = 15
ro = ro*(pi/180)

r1x = p1x*cos(ro) + p1y*sin(ro)
r1y = p1y*cos(ro) - p1x*sin(ro)
r2x = p2x*cos(ro) + p2y*sin(ro)
r2y = p2y*cos(ro) - p2x*sin(ro)
r3x = p3x*cos(ro) + p3y*sin(ro)
r3y = p3y*cos(ro) - p3x*sin(ro)
r4x = p4x*cos(ro) + p4y*sin(ro)
r4y = p4y*cos(ro) - p4x*sin(ro)

x = c(p1x, p2x, p3x, p4x)
y = c(p1y, p2y, p3y, p4y)
#plot(x, y, t='l')

rx = c(r1x, r2x, r3x, r4x)
ry = c(r1y, r2y, r3y, r4y)
#lines(rx, ry, col='blue')

for(nx in 1:Nbx){
  for(ny in 1:Nby){

    # translation takes place at sx* 
    bx = x0 + nx*sx + (WB*cos(ro) + LB*sin(ro))*(nx-1)
    by = ymx - (sy*(ny-1)  + (LB*cos(ro))*ny) - WB*sin(ro)*(ny-1)

    vx = bx-p1x
    vy = by-p1y

    trx = rx+vx
    try = ry+vy

    imin = min(trx)/dx
    imax = max(trx)/dx
    jmin = min(ymx-try)/dx
    jmax = max(ymx-try)/dx

    for(i in imin:imax){
      if(i>=1 && i<=Ncol){
        Ax = x0 + dx*(i-0.5)
        for(j in jmin:jmax){
          if(j>=1 && j<= Nrow){
            Ay = ymx-dx*(j-0.5)
            M[j, i] = M[j, i] + Hb 

          }
        }
      }
    }
  }
}

R1=raster(M, xmn = x0, xmx=(x0+Ncol)*dx, ymn = y0, ymx = ymx)
plot(R1)

0 个答案:

没有答案