我的问题很简单!如何将点 xyz 坐标(均属于单个平面)转换为 xy 坐标。 我找不到任何R功能或R解决方案。
来源数据:
# 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))
如何得到一个点数据帧,其中A点是[0,0],而A和D之间的距离是sqrt(2)?
答案 0 :(得分:4)
所以你现在拥有的是共面点3D的坐标(你可以通过计算矩阵my.plain[, c("x", "y", "z")]
的等级来确认你的点是共面的,这是2)。
你想要你的新框架&#34;由A点定义为原点和向量(A->B)/|A->B|^2
和(A->D)/|A->D|^2
。
要将xyz坐标转换为新的&#34;帧&#34;中的坐标,您需要将前坐标(乘以A的坐标)乘以从旧框架到新框架的转换矩阵
所以,在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)