如何使用矩阵乘法将原点从(0,0)转换为(x,y)?

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

我有一个2乘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')

enter image description here

题。如何正确地使矩阵乘法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')
r matrix rotation transform matrix-multiplication
1个回答
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')

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.