【发布时间】:2013-03-17 16:54:52
【问题描述】:
我有多个矩阵,其中填充了组成图形的 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
}
这种方法失败了,因为它不够灵活,无法始终计算所需的角度,结果是图形旋转了错误的角度和/或方向错误。
提前感谢您的阅读,希望你们中的一些人可以对此提出一些新的想法
编辑:可在此处找到重现此内容的数据
不确定如何提供您要求的数据,如果您指定您喜欢的方式,我很乐意以其他方式提供它
【问题讨论】:
-
制作更小的示例数据来演示问题,并将
dput的结果粘贴到此处。 -
有时间我会尽量减少,基本上你也可以在2D空间中为A和B随机选择两个点,这应该足以重现基本问题。跨度>
-
这是绕 A 点旋转,还是绕原点旋转?
-
补充说,旋转应该围绕起点A点
-
你应该在假设计算中使用“+”而不是“-”