我有一个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
矩阵中添加了第三列为“ 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')
答案 0 :(得分:0)
问题本身的答案:
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')