将xyz给出的平面转换为R中的xy坐标(3D到2D)

时间:2021-03-10 13:34:58

My problem is simple! How can I transform points xyz coordinates (all belong to a single plane) to only xy coordinates. I can not find any R function or R solution.

我的问题很简单!如何将点xyz坐标(都属于单个平面)转换为仅xy坐标。我找不到任何R功能或R解决方案。

将xyz给出的平面转换为R中的xy坐标(3D到2D)

Source data:

# cube with plain
library(scatterplot3d)
my.plain <- data.frame(ID = c("A","B","C","D","E","F","G","H"),
                        x = c(1,1,1,2,2,2,3,3),
                        y = c(1,1,1,2,2,2,3,3),
                        z = c(1,2,3,1,2,3,1,2))

scatterplot3d(my.plain$x, my.plain$y, my.plain$z,
              xlim = c(0,3), ylim = c(0,3), zlim = c(0,3))

How can I get a data.frame of points, where point A is [0,0], whereas distance between A and D is sqrt(2)?

如何得到一个点数据帧,其中A点是[0,0],而A和D之间的距离是sqrt(2)?

1 个解决方案

#1


4  

So what you have right now is coordinates in 3D of coplanar points (you can indeed verify that your points are coplanar by computing the rank of the matrix my.plain[, c("x", "y", "z")], which is 2).

所以你现在拥有的是共面点3D的坐标(你可以通过计算矩阵的等级my.plain [,c(“x”,“y”,“z”)来确认你的点是共面的] ,这是2)。

You want your new "frame" to be defined by point A as origin and vectors (A->B)/|A->B|^2 and (A->D)/|A->D|^2.

您希望新的“框架”由点A定义为原点和矢量(A-> B)/ | A-> B | ^ 2和(A-> D)/ | A-> D | ^ 2。

To convert your xyz coordinates into coordinates in the new "frame", you need to multiply the former coordinates, shifted by the coordinates of A, by the matrix of transformation from the old frame into the new one.

要将xyz坐标转换为新“框架”中的坐标,您需要将以前坐标(乘以A的坐标)乘以从旧框架到新框架的转换矩阵。

So, in R code, this gives:

所以,在R代码中,这给出了:

# Get a matrix out of your data.frame
row.names(my.plain) <- my.plain$ID
my.plain <- as.matrix(my.plain[, -1])

# compute the matrix of transformation
require(Matrix)
AB <- (my.plain["B", ] - my.plain["A", ])
AD <- (my.plain["D", ] - my.plain["A", ])
tr_mat <- cbind(AD/norm(AD, "2"), AB/norm(AB, "2"))

# compute the new coordinates
my.plain.2D <- (my.plain - my.plain["A", ]) %*% tr_mat

# plot the 2D data
plot(my.plain.2D, pch=19, las=1, xlab="x", ylab="y")

# and the plot with the letters, the polygon and the color:
plot(my.plain.2D, pch=3, las=1, xlab="x", ylab="y")
polygon(my.plain.2D[c("A", "B", "C", "F", "H", "G", "D"), ], col="magenta")
points(my.plain.2D, pch=3, lwd=2)
text(my.plain.2D[, 1], my.plain.2D[, 2], row.names(my.plain.2D), pos=2, font=2)

将xyz给出的平面转换为R中的xy坐标(3D到2D)

#1


4  

So what you have right now is coordinates in 3D of coplanar points (you can indeed verify that your points are coplanar by computing the rank of the matrix my.plain[, c("x", "y", "z")], which is 2).

所以你现在拥有的是共面点3D的坐标(你可以通过计算矩阵的等级my.plain [,c(“x”,“y”,“z”)来确认你的点是共面的] ,这是2)。

You want your new "frame" to be defined by point A as origin and vectors (A->B)/|A->B|^2 and (A->D)/|A->D|^2.

您希望新的“框架”由点A定义为原点和矢量(A-> B)/ | A-> B | ^ 2和(A-> D)/ | A-> D | ^ 2。

To convert your xyz coordinates into coordinates in the new "frame", you need to multiply the former coordinates, shifted by the coordinates of A, by the matrix of transformation from the old frame into the new one.

要将xyz坐标转换为新“框架”中的坐标,您需要将以前坐标(乘以A的坐标)乘以从旧框架到新框架的转换矩阵。

So, in R code, this gives:

所以,在R代码中,这给出了:

# Get a matrix out of your data.frame
row.names(my.plain) <- my.plain$ID
my.plain <- as.matrix(my.plain[, -1])

# compute the matrix of transformation
require(Matrix)
AB <- (my.plain["B", ] - my.plain["A", ])
AD <- (my.plain["D", ] - my.plain["A", ])
tr_mat <- cbind(AD/norm(AD, "2"), AB/norm(AB, "2"))

# compute the new coordinates
my.plain.2D <- (my.plain - my.plain["A", ]) %*% tr_mat

# plot the 2D data
plot(my.plain.2D, pch=19, las=1, xlab="x", ylab="y")

# and the plot with the letters, the polygon and the color:
plot(my.plain.2D, pch=3, las=1, xlab="x", ylab="y")
polygon(my.plain.2D[c("A", "B", "C", "F", "H", "G", "D"), ], col="magenta")
points(my.plain.2D, pch=3, lwd=2)
text(my.plain.2D[, 1], my.plain.2D[, 2], row.names(my.plain.2D), pos=2, font=2)

将xyz给出的平面转换为R中的xy坐标(3D到2D)