Question

I have a set of 2D points which i need to transform(2D) only and I know the boundaries of the transformed set. I am trying to put the points inside a restricted boundary. I am using R to do this transformation.

First I equate the boundary points of the original and transformed region. I am using scale and rotate matrix. So the equation would be:

newpoint = scale matrix * rotate matrix * original point.

Both scale matrix and rotate matrix are 2x2 matrices.

However when I do the above for my boundary points and get the rotate and scale matrix and then I use these two matrices to get the new bounded points for the set of original points, I am not able to restrict it within the boundary. Can somebody suggest what is going wrong?

In the below code, xnewrange is the boundary of the transformed points and xorigrange is the boundary of the original points and myorigmat is the matrix containing the original points which need to be transformed.

The code of what i have done so far is as below:

xnewrange<-c(-0.2588,4.036885)

ynewrange<-c(-2.653607,4.069070)

xorigrange<-c(-0.6810824,1.3324875)

yorigrange<-c(-1.419355,2.459154)

myorigmat
         [,1]       [,2]
31  1.3324875 -1.4193554
32  0.5755337  0.4543802
33 -0.3365769  1.0730593
34  0.8752970 -1.1013751
35 -0.6810824  0.9655893
36  0.2439643  0.1838974
37 -0.3893538  0.5326981
38  0.2241310  0.7273958
39 -0.1219151  0.2176043
40  0.8737421  2.4591542

coord<-matrix(c(xorigrange[1],xorigrange[2],yorigrange[1],yorigrange[2]),2,2,byrow=T)

trans_coord<-matrix(c(xnewrange[1],xnewrange[2],ynewrange[1],ynewrange[2]),2,2,byrow=T)

costheta<-sum(trans_coord[,1]*coord[,1])/(sqrt(sum((coord[1,1])^2,(coord[2,1])^2)) * sqrt(sum((trans_coord[1,1])^2,(trans_coord[2,1])^2)))               #using dot product

sintheta<-sqrt(1-(costheta^2))

rotate_mat<-matrix(c(costheta,sintheta,(-sintheta),costheta),2,2,byrow=T)

scale_mat<-(trans_coord) %*% solve(rotate_mat %*% coord)

Using the scale_mat and rotate_mat thus obtained to get the new points as follows:

newmat<-matrix(0,10,2)

for(i in 1:10){
newmat[i,]<-scale_mat %*% rotate_mat %*% matrix(c(mymat[i,1],mymat[i,2]),2,1,byrow=T)
}

newmat

But the points in newmat are not bounded within xnewrange and ynewrange.

Was it helpful?

Solution

a<-c(-0.2588,-2.653607)

b<-c(-0.6810824,-1.419355)

costheta<-sum(a*b)/((sqrt(sum((a[1]^2),(a[2]^2))))*(sqrt(sum((b[1]^2),(b[2]^2)))))

sintheta<-sqrt(1-(costheta^2))

m1output_corner<-matrix(c(a[1],a[2]),2,1)

m1input_corner<-matrix(c(b[1],b[2]),2,1)

rotate_mat<-matrix(c(costheta,sintheta,(-sintheta),costheta),2,2,byrow=T)

mint<-rotate_mat %*% m1input_corner

tran<-matrix(c(-mint[1,1],-mint[2,1]),2,1)

m2output_corner<-matrix(c(4.036885,4.069070),2,1)

m2input_corner<-matrix(c(1.332488,2.459154),2,1)

smat<- m2output_corner / ((rotate_mat%*% m2input_corner)+tran)

scalemat<-matrix(c(smat[1,1],0,0,smat[2,1]),2,2,byrow=T)


newmat<-matrix(0,10,2)

for(i in 1:10){

  newmat[i,]<-(scalemat %*% ((rotate_mat %*% matrix(c(mymat[i,1],mymat[i,2]),2,1,byrow=T)) + tran ))

}

newmat
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top