4

グラフを構成する2D空間の複数の点のx座標とy座標で満たされた複数の行列があります。行列は次のようになります

x1 x2 x3 x4 ...
y1 y2 y3y4..。

可能なグラフは次のようになります

ここに画像の説明を入力してください

私がやりたいのは、点Aと点Bの間の線がX軸に平行になるように、点Aを中心にグラフを回転させることです。

私の考えは、直線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
}

このアプローチは、必要な角度を常に計算するのに十分な柔軟性がないため失敗し、その結果、グラフが間違った角度および/または間違った方向に回転します。

読んでくれてありがとう、そしてうまくいけば、あなたの何人かはこれにいくつかの新鮮なアイデアをもたらすことができます

編集:これを再現するためのデータはここにあります

X座標

Y座標

要求したデータの提供方法がわからない場合は、希望する方法を指定していただければ、別の方法で喜んで提供します。

4

2 に答える 2

13

このような?

#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))

ここに画像の説明を入力してください

編集:

わかりやすくするために、変換を分解します。ただし、これは基本的な線形代数にすぎません。

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")

ここに画像の説明を入力してください

于 2013-03-17T18:20:35.350 に答える
3

データフレームの代わりに、データが(を介して)マトリックスとして提供されているように見えますas.matrix

この答えはローランドの答えと非常に似ていますが、物事をより多くのステップに分割し、角度がの倍数である場合にいくつかの特殊なケースの処理がありpi/2ます。

#sample data
set.seed(1) #for consistency of random-generated data
d <- matrix(c(sort(runif(50)),sort(runif(50))),ncol=2)

#rotation about point A
rotA <- function(d) {
d.offset <- apply(d,2,function(z) z - z[1]) #offset data
  endpoint <- d.offset[nrow(d.offset),] #gets difference
  rot <- function(angle) matrix(
    c(cos(angle),-sin(angle),sin(angle),cos(angle)),nrow=2) #CCW rotation matrix
  if(endpoint[2] == 0) {
    return(d) #if y-diff is 0, then no action required
  } else if (endpoint[1] == 0) { 
    rad <- pi/2 #if x-diff is 0, then rotate by a right angle
  } else {rad <- atan(endpoint[2]/endpoint[1])}
  d.offset.rotate <- d.offset %*% rot(-rad) #rotation
  d.rotate <- sapply(1:2,function(z) d.offset.rotate[,z] + d[1,z]) #undo offset
  d.rotate
}

#results and plotting to check visually
d.rotate <- rotA(d)
plot(d.rotate)
abline(h=d[1,2])
于 2013-03-17T18:34:50.050 に答える