【问题标题】:How to translate the origin from (0,0) to (x,y) with a matrix multiplication?如何使用矩阵乘法将原点从 (0,0) 转换为 (x,y)?
【发布时间】:2019-09-11 01:59:08
【问题描述】:

我有一个 2 x 4 矩阵 coor 定义四个点:(0,0)(a,0)(a, -b)(a-c, -b) 平面上的一条线:

a<-3; b<-1; c<-1
coor <- matrix(0,2,4)
coor <- t(matrix(c(0,0, a,0, a,-b, a-c, -b), nrow=2));

我在coor 矩阵中添加了第 3 列是“1”,以便在乘法中使用该矩阵。

coor <- cbind(coor, 1) 

我需要 1) 将原点从 (0,0) 平移到坐标为 (a-c,-b) 的第四点,以及 2) 在角度 alpha 上旋转线:

# translation matrix
I <- matrix(0,3,3); diag(I) <- 1

I[1, 3] <- -coor[4, 1]
I[2, 3] <- -coor[4, 2]

alpha = -pi/2

# rotation matrix
M <- matrix(c(cos(alpha), sin(alpha), 0,
             -sin(alpha), cos(alpha), 0,
                 0,          0, 1), nrow=3)


coor1 <- matrix()
coor1 <- coor %*% I %*% M %*% solve(I)

两次操作的结果是:

> cbind(coor1[,1], coor1[,2])
            [,1] [,2]
[1,] 0.00000e+00    0
[2,] 1.83691e-16    3
[3,] 1.00000e+00    3
[4,] 1.00000e+00    2

预期结果是:

> coor2 <- matrix(c(2,-1, 2,2, 3,2, 3,1),nrow=2); t(coor2);
     [,1] [,2]
[1,]    2   -1
[2,]    2    2
[3,]    3    2
[4,]    3    1

带有原始点和结果的绘图如下。在图中,我将红线中的给定点分组,其中 a、b、c 是相应线段的长度。

plot(t(coor[1,]), t(coor[2,]), col='red', type= 'l', xlim=c(0,3), ylim=c(-1,3), xlab='x', ylab='y')
points(round(cbind(coor1[,1], coor1[,2]),2), col='green', type= 'l')
points(t(coor2), col='blue', type= 'l')

问题。如何正确地进行矩阵乘法coor %*% I %*% M %*% solve(I) 以便将线的第一个点从(0,0) 移动到(a-c, -b)

编辑。

Combining translation and rotation

完整代码:

a<-3; b<-1; c<-1
coor <- matrix(0,2,4)
coor <- t(matrix(c(0,0, a,0, a,-b, a-c, -b), nrow=2));

coor <- cbind(coor, 1) 

# translation matrix
I <- matrix(0,3,3); diag(I) <- 1

I[1, 3] <- -coor[4, 1]
I[2, 3] <- -coor[4, 2]
alpha = -pi/2

# rotation matrix
M <- matrix(c(cos(alpha), sin(alpha), 0,
             -sin(alpha), cos(alpha), 0,
                 0,          0, 1), nrow=3)


coor1 <- matrix()
coor1 <- coor %*% I %*% M %*% solve(I) 
coor2 <- matrix(c(2,-1, 2,2, 3,2, 3,1),nrow=2)

plot(coor[,1], coor[,2], col='red', type= 'l', xlim=c(-3,6), ylim=c(-2,6), xlab='x', ylab='y')
points(x=coor1[,1], y=coor1[,2], col='green', type= 'l')
points(t(coor2), col='blue', type= 'l')

【问题讨论】:

  • 你能澄清一下吗?一方面,coor 不是 3x3,因此不清楚“在平面中定义向量”是什么意思,因为平面是二维的,并且该矩阵可以指 3 或 4 个“维度”。另一方面,目前还不清楚要问什么。您只是在问矩阵乘法是如何工作的吗?
  • @42,我重新提出了这个问题。
  • 我很困惑。 (a) 惯例是将变换矩阵 M 从左侧应用于向量 xM %*% x = result,但您正在寻求右乘。有什么原因,还是这是一个错误?如果这是故意的,您将(我相信)需要转置传统映射。 (b) 您的图表仅显示翻译,您的“预期结果”似乎与图表匹配,但您将其与翻译和旋转的代码进行比较。你有包括旋转在内的预期结果吗? (c)I是单位矩阵,请换个字母翻译...
  • (d) 你为什么使用I %*% M %*% solve(I)?你说“平移和旋转”,这将是I %*% M,(或者,使用左乘的标准约定,M %*% I %*% x)。在组合中添加solve(I) 表示您希望在旋转后撤消翻译。但是您似乎没有在任何地方的文字中提及这一点,因此令人困惑。
  • 所以,说了这么多,我想你想要的是(使用你定义的对象,但是标准的左乘转换)M %*% I %*% t(coor)。如果您想以coor 开头并右乘,请转置所有内容:coor %*% t(I) %*% t(M)

标签: r matrix rotation transform matrix-multiplication


【解决方案1】:

问题的自己的答案:

a <- 3; b <-1 ; c <- 1

coor <- matrix(0, 2, 4)
coor <- t(matrix(c(0,0, a,0, a,-b, a-c, -b), nrow=2));
coor <- cbind(coor, 1)

alpha = -pi/2

coor1 <- matrix(0, 3, 3)

# new origin (x, y)
x <- coor[4,1]; y <- coor[4,2]

T <- matrix(0, 3, 3)
T <- matrix(c(cos(alpha), sin(alpha),  x,
             -sin(alpha), cos(alpha),  y,
                       0,          0,  1), nrow=3);
coor1 <- coor %*% T

plot(coor[,1], coor[,2], col='red', type= 'l', xlim=c(0,3), ylim=c(-1,2), xlab='x', ylab='y')
points(round(cbind(coor1[,1], coor1[,2]),2), col='green', type= 'l')

【讨论】:

  • 在 SO 中仅代码答案被认为是不够的
猜你喜欢
  • 2017-08-12
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多