dee*_*mel 4 r graph rotation matrix
我有多个矩阵填充了2D空间中构成图形的多个点的x和y坐标.矩阵看起来像这样
x1 x2 x3 x4 ...
y1 y2 y3 y4 ...
可能的图形看起来像这样

我想要做的是围绕A点旋转图形,使A点和B点之间的线平行于X轴.
我的想法是将AB线视为右三角形的斜角,计算α(A点的角度)并使用旋转矩阵旋转该图形的矩阵.
到目前为止我所做的是以下内容
#df is the subset of my data that describes the graph we're handling right now,
#df has 2 or more rows
beginx=df[1,]$xcord #get the x coordinate of point A
beginy=df[1,]$ycord #get the y coordinate of point A
endx=df[nrow(df)-1,]$xcord #get the x coordinate of point B
endy=df[nrow(df)-1,]$ycord #get the y coordinate of point B
xnow=df$xcord
ynow=df$ycord
xdif=abs(beginx-endx)
ydif=abs(beginy-endy)
if((xdif != 0) & (ydif!=0)){
direct=sqrt(abs((xdif^2)-(ydif^2))) #calculate the length of the hypothenuse
sinang=abs(beginy-endy)/direct
angle=1/sin(sinang)
if(beginy>endy){
angle=angle
}else{
angle=360-angle
}
rotmat=rot(angle) # use the function rot(angle) to get the rotation matrix for
# the calculated angle
A = matrix(c(xnow,ynow),nrow=2,byrow = TRUE) # matrix containing the graph coords
admat=rotmat%*%A #multiply the matrix with the rotation matrix
}
Run Code Online (Sandbox Code Playgroud)
这种方法失败,因为它不够灵活,无法始终计算所需的角度,结果是图形以错误的角度和/或错误的方向旋转.
在此先感谢阅读,希望你们中的一些人可以为此带来一些新鲜的想法
编辑:可以在此处找到要重现的数据
不知道如何提供您要求的数据,如果您指定的方式,我很乐意以其他方式提供
Rol*_*and 11
像这样?
#read in X and Y as vectors
M <- cbind(X,Y)
#plot data
plot(M[,1],M[,2],xlim=c(0,1200),ylim=c(0,1200))
#calculate rotation angle
alpha <- -atan((M[1,2]-tail(M,1)[,2])/(M[1,1]-tail(M,1)[,1]))
#rotation matrix
rotm <- matrix(c(cos(alpha),sin(alpha),-sin(alpha),cos(alpha)),ncol=2)
#shift, rotate, shift back
M2 <- t(rotm %*% (
t(M)-c(M[1,1],M[1,2])
)+c(M[1,1],M[1,2]))
#plot
plot(M2[,1],M2[,2],xlim=c(0,1200),ylim=c(0,1200))
Run Code Online (Sandbox Code Playgroud)

我将细分转换以使其更容易理解.然而,它只是基本的线性代数.
plot(M,xlim=c(-300,1200),ylim=c(-300,1200))
#shift points, so that turning point is (0,0)
M2.1 <- t(t(M)-c(M[1,1],M[1,2]))
points(M2.1,col="blue")
#rotate
M2.2 <- t(rotm %*% (t(M2.1)))
points(M2.2,col="green")
#shift back
M2.3 <- t(t(M2.2)+c(M[1,1],M[1,2]))
points(M2.3,col="red")
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2868 次 |
| 最近记录: |