给定定向向量计算 XY 坐标
Compute XY coordinate given a directed vector
我有这些数据:
set.seed(1)
df <- data.frame(xstart=rnorm(5),ystart=rnorm(5),
xmax=rnorm(5),ymax=rnorm(5),
length=runif(5,0,1))
其中xstart
和ystart
定义了向量的起始坐标(即此二维space中的一条线有方向),length
定义了其长度。 xmax
和 ymax
定义向量的方向(即 (ymax - ystart)/(xmax - xstart)
是斜率)。
我正在寻找一个函数来计算每个向量的 xend 和 yend 坐标。基本上这可以使用方程来解决:
length^2 = (xend - xstart)^2 + (yend - ystart)^2
yend = beta*(xend - xstart)
其中:
beta = (ymax - ystart)/(xmax - xstart)
这是纯几何学。我建议使用 atan2
代替斜率,因为它是有限定义的(用垂直线替代 Inf
或 -Inf
的斜率)。
angles <- atan2(df$ymax - df$ystart, df$xmax - df$xstart)
df$xend <- df$xstart + df$length * cos(angles)
df$yend <- df$ystart + df$length * sin(angles)
# and to verify the resulting lengths are as-desired
df$len2 <- sqrt( (df$xend - df$xstart)^2 + (df$yend - df$ystart)^2 )
df
# xstart ystart xmax ymax length xend yend len2
# 1 -0.6264538 -0.8204684 1.5117812 -0.04493361 0.8209463 0.1452983 -0.54055500 0.8209463
# 2 0.1836433 0.4874291 0.3898432 -0.01619026 0.6470602 0.4288186 -0.11138308 0.6470602
# 3 -0.8356286 0.7383247 -0.6212406 0.94383621 0.7829328 -0.2704346 1.28011746 0.7829328
# 4 1.5952808 0.5757814 -2.2146999 0.82122120 0.5530363 1.0433885 0.61133438 0.5530363
# 5 0.3295078 -0.3053884 1.1249309 0.59390132 0.5297196 0.6804608 0.09139217 0.5297196
所以,作为一个函数,类似于:
somefunc <- function(xstart, ystart, xmax, ymax, len) {
angles <- atan2(ymax - ystart, xmax - xstart)
xend <- xstart + len * cos(angles)
yend <- ystart + len * sin(angles)
data.frame(xend = xend, yend = yend)
}
其中 returns 具有两个所需列的框架。可以使用 cbind(origdat, somefunc(...))
.
轻松地将它们添加到现有数据中
我有这些数据:
set.seed(1)
df <- data.frame(xstart=rnorm(5),ystart=rnorm(5),
xmax=rnorm(5),ymax=rnorm(5),
length=runif(5,0,1))
其中xstart
和ystart
定义了向量的起始坐标(即此二维space中的一条线有方向),length
定义了其长度。 xmax
和 ymax
定义向量的方向(即 (ymax - ystart)/(xmax - xstart)
是斜率)。
我正在寻找一个函数来计算每个向量的 xend 和 yend 坐标。基本上这可以使用方程来解决:
length^2 = (xend - xstart)^2 + (yend - ystart)^2
yend = beta*(xend - xstart)
其中:
beta = (ymax - ystart)/(xmax - xstart)
这是纯几何学。我建议使用 atan2
代替斜率,因为它是有限定义的(用垂直线替代 Inf
或 -Inf
的斜率)。
angles <- atan2(df$ymax - df$ystart, df$xmax - df$xstart)
df$xend <- df$xstart + df$length * cos(angles)
df$yend <- df$ystart + df$length * sin(angles)
# and to verify the resulting lengths are as-desired
df$len2 <- sqrt( (df$xend - df$xstart)^2 + (df$yend - df$ystart)^2 )
df
# xstart ystart xmax ymax length xend yend len2
# 1 -0.6264538 -0.8204684 1.5117812 -0.04493361 0.8209463 0.1452983 -0.54055500 0.8209463
# 2 0.1836433 0.4874291 0.3898432 -0.01619026 0.6470602 0.4288186 -0.11138308 0.6470602
# 3 -0.8356286 0.7383247 -0.6212406 0.94383621 0.7829328 -0.2704346 1.28011746 0.7829328
# 4 1.5952808 0.5757814 -2.2146999 0.82122120 0.5530363 1.0433885 0.61133438 0.5530363
# 5 0.3295078 -0.3053884 1.1249309 0.59390132 0.5297196 0.6804608 0.09139217 0.5297196
所以,作为一个函数,类似于:
somefunc <- function(xstart, ystart, xmax, ymax, len) {
angles <- atan2(ymax - ystart, xmax - xstart)
xend <- xstart + len * cos(angles)
yend <- ystart + len * sin(angles)
data.frame(xend = xend, yend = yend)
}
其中 returns 具有两个所需列的框架。可以使用 cbind(origdat, somefunc(...))
.