2013-02-27 111 views
1

我想写一个C程序来旋转矩形内的点。旋转矩形内的几个点

在我的程序中,矩形中心是枢轴点,矩形尺寸是320x480。假设矩形的顶点之一位于原点,则支点为(160,240)

现在相对于旋转点(px, py)矩形内的支点(ox, oy),我用下面的公式 -

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox 

p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy 

然而,当我试图通过90度旋转点,所有点被映射到一条直线上。

任何人都可以解决这个问题吗?

theta2=90; 


     theta1=abs(theta2*3.1415926)/180; 

     if(theta2>0) 
     { 
      for(int tc=0;tc<rstruct2->nrows;tc++) 
      { 
       rstruct2->xcol[tc]=round((rstruct2->xcol[tc]-160)*cos(theta1)-sin(theta1)*(rstruct2->ycol[tc]-240)+160); 

       rstruct2->ycol[tc]=round((rstruct2->xcol[tc]-160)*sin(theta1)+cos(theta1)*(rstruct2->ycol[tc]-240)+240); 


      } 
     } 
     else 
     { 
      for(int tc=0;tc<rstruct2->nrows;tc++) 
      { 
       rstruct2->xcol[tc]=round(160+(rstruct2->xcol[tc]-160)*cos(theta1)+(rstruct2->ycol[tc]-240)*sin(theta1)); 

       rstruct2->ycol[tc]=round(240+(-rstruct2->xcol[tc]-160)*sin(theta1)+(rstruct2->ycol[tc]-240)*cos(theta1)); 


      } 
     } 
+1

1.这是C++或C?他们是不同的。我们可以看看你的代码吗? (这很可能会回答1) – BoBTFish 2013-02-27 10:53:00

+0

@BoBTFish它是c – 2013-02-27 11:03:36

+0

好的,我删除了'C++'标记。 – BoBTFish 2013-02-27 11:05:50

回答

4

你Y型旋转使用修改的X值,但你需要使用基本价值 - 用一个临时变量,就像这样:

double x_tmp = round((rstruct2->xcol[tc]-160)*cos(theta1)-sin(theta1)*(rstruct2->ycol[tc]-240)+160); 
double y_tmp = round((rstruct2->xcol[tc]-160)*sin(theta1)+cos(theta1)*(rstruct2->ycol[tc]-240)+240); 

rstruct2->xcol[tc] = x_tmp; 
rstruct2->ycol[tc] = y_tmp;