原线两侧的平行线

问题描述 投票:0回答:1

我想在原始折线的两侧绘制平行的折线,相距 x 英尺,覆盖 2 英里。

line<-structure(list(ID = 1, result = structure(list(structure(c(-103.425920946, 
-103.420670944, 31.7545147220001, 31.7406247380001), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg"))), class = c("sfc_LINESTRING", 
"sfc"), precision = 0, bbox = structure(c(xmin = -103.425920946, 
ymin = 31.7406247380001, xmax = -103.420670944, ymax = 31.7545147220001
), class = "bbox"), crs = structure(list(input = "+proj=longlat +datum=NAD27 +no_defs", 
    wkt = "GEOGCRS[\"unknown\",\n    DATUM[\"North American Datum 1927\",\n        ELLIPSOID[\"Clarke 1866\",6378206.4,294.978698213898,\n            LENGTHUNIT[\"metre\",1]],\n        ID[\"EPSG\",6267]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433],\n        ID[\"EPSG\",8901]],\n    CS[ellipsoidal,2],\n        AXIS[\"longitude\",east,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433,\n                ID[\"EPSG\",9122]]],\n        AXIS[\"latitude\",north,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433,\n                ID[\"EPSG\",9122]]]]"), class = "crs"), n_empty = 0L)), row.names = c(NA, 
-1L), class = c("sf", "tbl_df", "tbl", "data.frame"), sf_column = "result", agr = structure(c(ID = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"))

下面是类似问题的链接,但只绘制了一条偏移线。

在一条直线的 R 偏移量中绘制一条平行线

谢谢!

x <-  c(10,5)
y <-  c(1,3)
d <- -0.5   # distance away from the road
I believe i need a for loop here.

# Given a vector (defined by 2 points) and the distance, 
# calculate a new vector that is distance away from the original 
segment.shift <- function(x, y, d){
  
  # calculate vector
  v <- c(x[2] - x[1],y[2] - y[1])
  
  # normalize vector
  v <- v/sqrt((v[1]**2 + v[2]**2))
  
  # perpendicular unit vector
  vnp <- c( -v[2], v[1] )
  
  return(list(x =  c( x[1] + d*vnp[1], x[2] + d*vnp[1]), 
              y =  c( y[1] + d*vnp[2], y[2] + d*vnp[2])))
  
}

plot(x,y, xlim=c(-1,11), ylim=c(-1,11), type="l", main= "Bicycle path" )

# allocate memory for the bike path
xn <- numeric( (length(x) - 1) * 2 )
yn <- numeric( (length(y) - 1) * 2 )

for ( i in 1:(length(x) - 1) ) {
  xs <- c(x[i], x[i+1])
  ys <- c(y[i], y[i+1])
  new.s <- segment.shift( xs, ys, d )
  xn[(i-1)*2+1] <- new.s$x[1] ; xn[(i-1)*2+2] <- new.s$x[2]
  yn[(i-1)*2+1] <- new.s$y[1] ; yn[(i-1)*2+2] <- new.s$y[2]
}

# draw the path
lines(xn, yn, col="brown", lwd =2, lty=1)
r spline r-sf
1个回答
1
投票

只需要一个循环

x <-  c(10,5)
y <-  c(1,3)
distance <- c(-0.5,.5,.75)   # distance away from the road

#key update
for (d in distance) {
  # Given a vector (defined by 2 points) and the distance, 
  # calculate a new vector that is distance away from the original 
  segment.shift <- function(x, y, d){
    
    # calculate vector
    v <- c(x[2] - x[1],y[2] - y[1])
    
    # normalize vector
    v <- v/sqrt((v[1]**2 + v[2]**2))
    
    # perpendicular unit vector
    vnp <- c( -v[2], v[1] )
    
    return(list(x =  c( x[1] + d*vnp[1], x[2] + d*vnp[1]), 
                y =  c( y[1] + d*vnp[2], y[2] + d*vnp[2])))
    
  }
  
  #plot(x,y, xlim=c(-1,11), ylim=c(-1,11), type="l", main= "Bicycle path" )
  
  # allocate memory for the bike path
  xn <- numeric( (length(x) - 1) * 2 )
  yn <- numeric( (length(y) - 1) * 2 )
  
  for ( i in 1:(length(x) - 1) ) {
    xs <- c(x[i], x[i+1])
    ys <- c(y[i], y[i+1])
    new.s <- segment.shift( xs, ys, d )
    xn[(i-1)*2+1] <- new.s$x[1] ; xn[(i-1)*2+2] <- new.s$x[2]
    yn[(i-1)*2+1] <- new.s$y[1] ; yn[(i-1)*2+2] <- new.s$y[2]
  }

  lines(xn, yn, col="brown", lwd =2, lty=1)  
}
© www.soinside.com 2019 - 2024. All rights reserved.