右转弯

时间:2013-12-03 21:13:07

标签: r plot rotational-matrices

我有一个问题,我有一堆长度,想要从原点开始(假装我正面向y轴的正端),我做了一个正确的沿着x轴向前移动length_i的距离。这时我又转了一圈,走了length_i的距离,重复了n次。我可以做到这一点,但我认为有一种更有效的方法,我缺乏数学背景:

## Fake Data
set.seed(11)
dat <- data.frame(id = LETTERS[1:6], lens=sample(2:9, 6), 
    x1=NA, y1=NA, x2=NA, y2=NA)

##   id lens x1 y1 x2 y2
## 1  A    4 NA NA NA NA
## 2  B    2 NA NA NA NA
## 3  C    5 NA NA NA NA
## 4  D    8 NA NA NA NA
## 5  E    6 NA NA NA NA
## 6  F    9 NA NA NA NA

## Add a cycle of 4 column    
dat[, "cycle"] <- rep(1:4, ceiling(nrow(dat)/4))[1:nrow(dat)]

##For loop to use the information from cycle column
for(i in 1:nrow(dat)) {

    ## set x1, y1
    if (i == 1) {
       dat[1, c("x1", "y1")] <- 0
    } else {
       dat[i, c("x1", "y1")] <- dat[(i - 1), c("x2", "y2")]
    }

    col1 <- ifelse(dat[i, "cycle"] %% 2 == 0, "x1", "y1")
    col2 <- ifelse(dat[i, "cycle"] %% 2 == 0, "x2", "y2")
    dat[i, col2] <- dat[i, col1]

    col3 <- ifelse(dat[i, "cycle"] %% 2 != 0, "x2", "y2")
    col4 <- ifelse(dat[i, "cycle"] %% 2 != 0, "x1", "y1")
    mag <- ifelse(dat[i, "cycle"] %in% c(1, 4), 1, -1)
    dat[i, col3] <- dat[i, col4] + (dat[i, "lens"] * mag)

}

这会产生预期效果:

> dat

  id lens x1 y1 x2 y2 cycle
1  A    4  0  0  4  0     1
2  B    2  4  0  4 -2     2
3  C    5  4 -2 -1 -2     3
4  D    8 -1 -2 -1  6     4
5  E    6 -1  6  5  6     1
6  F    9  5  6  5 -3     2

这是一个情节:

library(ggplot2); library(grid)
ggplot(dat, aes(x = x1, y = y1, xend = x2, yend = y2)) + 
    geom_segment(aes(color=id), size=3, arrow = arrow(length = unit(0.5, "cm"))) + 
    ylim(c(-10, 10)) + xlim(c(-10, 10))

这似乎缓慢而笨重。我猜这是比我在for循环中做的更好的方法。什么是一种更有效的方式来保持编程权利?

enter image description here

5 个答案:

答案 0 :(得分:10)

(正如@DWin所建议的)这是一个使用复数的解决方案,它对任何类型的turn都是灵活的,而不仅仅是90度(-pi / 2弧度)的直角。一切都是矢量化的:

set.seed(11)
dat <- data.frame(id = LETTERS[1:6], lens = sample(2:9, 6),
                                     turn = -pi/2)

dat <- within(dat, { facing   <- pi/2 + cumsum(turn)
                     move     <- lens * exp(1i * facing)
                     position <- cumsum(move)
                     x2       <- Re(position)
                     y2       <- Im(position)
                     x1       <- c(0, head(x2, -1))
                     y1       <- c(0, head(y2, -1))
                   })

dat[c("id", "lens", "x1", "y1", "x2", "y2")]
#   id lens x1 y1 x2 y2
# 1  A    4  0  0  4  0
# 2  B    2  4  0  4 -2
# 3  C    5  4 -2 -1 -2
# 4  D    8 -1 -2 -1  6
# 5  E    6 -1  6  5  6
# 6  F    9  5  6  5 -3

turn变量应该与lens一起被视为输入。现在所有转弯都是-pi/2弧度,但你可以将它们中的每一个设置为你想要的任何一个。所有其他变量都是输出。


现在玩得很开心:

trace.path <- function(lens, turn) {
  facing   <- pi/2 + cumsum(turn)
  move     <- lens * exp(1i * facing)
  position <- cumsum(move)
  x        <- c(0, Re(position))
  y        <- c(0, Im(position))

  plot.new()
  plot.window(range(x), range(y))
  lines(x, y)
}

trace.path(lens = seq(0, 1,  length.out = 200),
           turn = rep(pi/2 * (-1 + 1/200), 200))

enter image description here

(我尝试在此处复制图表:http://en.wikipedia.org/wiki/Turtle_graphics

我也让你试试这些:

trace.path(lens = seq(1, 10, length.out = 1000),
           turn = rep(2 * pi / 10, 1000))

trace.path(lens = seq(0, 1,  length.out = 500),
           turn = seq(0, pi, length.out = 500))

trace.path(lens = seq(0, 1,  length.out = 600) * c(1, -1),
           turn = seq(0, 8*pi, length.out = 600) * seq(-1, 1, length.out = 200))

随意添加你的!

答案 1 :(得分:8)

这是另一种使用复数的方法。您可以乘以-1i在复平面中“向右”旋转矢量。下面的代码使第一次遍历进入正X(Re() - al轴)并且每次后续遍历都将旋转到“右”

imVecs <- lengths*c(0-1i)^(0:3)
imVecs
# [1]  9+0i  0-5i -9+0i  0+9i  8+0i  0-5i -8+0i  0+7i  8+0i  0-1i -5+0i  0+3i  4+0i  0-7i -4+0i  0+2i
#[17]  3+0i  0-7i -5+0i  0+8i

cumsum(imVecs)
# [1] 9+0i 9-5i 0-5i 0+4i 8+4i 8-1i 0-1i 0+6i 8+6i 8+5i 3+5i 3+8i 7+8i 7+1i 3+1i 3+3i 6+3i 6-4i 1-4i
#[20] 1+4i
plot(cumsum(imVecs))
lines(cumsum(imVecs))

enter image description here

这是使用复平面旋转向右转45度的方法:

> sqrt(-1i)
[1] 0.7071068-0.7071068i
> imVecs <- lengths*sqrt(0-1i)^(0:7)
Warning message:
In lengths * sqrt(0 - (0+1i))^(0:7) :
  longer object length is not a multiple of shorter object length
> plot(cumsum(imVecs))
> lines(cumsum(imVecs))

情节:

enter image description here

答案 2 :(得分:5)

这不是一个漂亮的情节,但我已经将它包括在内,以表明这种“矢量化”坐标计算能够产生正确的结果,这些结果不应该太难以适应您的需求:

xx <- c(1,0,-1,0)
yy <- c(0,-1,0,1)

coords <- suppressWarnings(cbind(x = cumsum(c(0,xx*dat$lens)), 
                                 y = cumsum(c(0,yy*dat$lens))))
plot(coords, type="l", xlim=c(-10,10), ylim=c(-10,10))

enter image description here

答案 3 :(得分:3)

在距离和方位方面考虑这个可能是有用的。距离由dat$lens给出,而方位是相对于某个任意参考线(例如,x轴)的运动角度。然后,在每一步,

x.new = x.old + distance * cos(bearing)
y.new = y.old + distance * sin(bearing)
bearing = bearing + increment

这里,由于我们从原点开始并沿+ x方向移动,(x,y)=(0,0)并且轴承从0度开始。右转是轴承增量-90度(-pi / 2弧度)。所以在R代码中,使用dat的定义:

x <-0
y <- 0
bearing <- 0
for (i in 1:nrow(dat)){
  dat[i,c(3,4)] <- c(x,y)
  length <- dat[i,2]
  x <- x + length * cos(bearing)
  y <- y + length * sin(bearing)
  dat[i,c(5,6)] <- c(x,y)
  bearing <- bearing - pi/2
}

这会产生你所拥有的东西,并且有一个优点,你可以非常简单地更新它,以左转,或45度转,或其他什么。您甚至可以将bearing.increment列添加到dat以创建随机游走。

答案 4 :(得分:1)

与Josh的解决方案非常相似:

lengths <- sample(1:10, 20, repl=TRUE)
x=cumsum(lengths*c(1,0,-1,0))
y=cumsum(lengths*c(0,1,0,-1))
cbind(x,y)
      x  y
 [1,] 9  0
 [2,] 9  5
 [3,] 0  5
 [4,] 0 -4
 [5,] 8 -4
 [6,] 8  1
 [7,] 0  1
 [8,] 0 -6
 [9,] 8 -6
[10,] 8 -5
[11,] 3 -5
[12,] 3 -8
[13,] 7 -8
[14,] 7 -1
[15,] 3 -1
[16,] 3 -3
[17,] 6 -3
[18,] 6  4
[19,] 1  4
[20,] 1 -4

基础图形:

plot(cbind(x,y))
arrows(cbind(x,y)[-20,1],cbind(x,y)[-20,2], cbind(x,y)[-1,1], cbind(x,y)[-1,2] )

enter image description here

这确实突出了Josh和我的解决方案都“走错路”的事实,所以你需要改变我们的“过渡矩阵”上的标志。而且我们可能应该从(0,0)开始,但你应该没有麻烦地根据你的需要调整它。

相关问题