简体   繁体   English

旋转矩形内的几个点

[英]Rotating Several points inside rectangle

I want to write a C program to rotate the points inside a rectangle. 我想编写一个C程序来旋转矩形内的点。

In my program, the rectangle center is the pivot point and the rectangle dimensions are 320x480 . 在我的程序中,矩形中心是枢轴点,矩形尺寸是320x480 Assuming one of the vertices of the rectangle are at the origin, the pivot point is (160,240) . 假设矩形的一个顶点位于原点,则枢轴点为(160,240)

Now to rotate the points (px, py) inside the rectangle with respect to the pivot (ox, oy) , I am using the following formulas - 现在旋转矩形内的点(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

However, when I try to rotate the points by 90 degrees, all the points are mapped onto a straight line. 但是,当我尝试将点旋转90度时,所有点都映射到一条直线上。

Can any one solve this problem? 任何人都可以解决这个问题吗?

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


            }
        }

Your y-rotation uses modified x-value, but you need to use the base value - use a temporary variable, like this: 您的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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM