简体   繁体   English

绘制旋转矩形

[英]Drawing rotated Rectangle

So I'm writting a function that takes a point and rotates it around another point at a certain angle, so when I draw a rectangle, I want that rectangle to be rotated as well,but it's distorted.I'm also drawing two colored rectangles, one without rotation and one rotated by the angle provided, like in this picture:所以我写了一个函数,它接受一个点并以某个角度围绕另一个点旋转,所以当我画一个矩形时,我希望这个矩形也被旋转,但它被扭曲了。我还画了两个彩色矩形,一个没有旋转,一个按提供的角度旋转,如下图所示:

图片

The purple outline should look like the red one.紫色轮廓应该看起来像红色轮廓。 This is the code for the rotation这是旋转的代码


 double d2r(double d)
        {
            return (d / 180.0) * ((double)M_PI);
        }

 double sind(double x) 
        {

            return std::sin(d2r(x));
        }


 double cosd(double x) 
        {
            return std::cos(d2r(x));
        }


std::pair<double, double> rotate_around(double x, double y, double o_x, double o_y, double angle)
{


    x = o_x + (x - o_x) * cosd(angle) - (y - o_y) * sind(angle);
    y = o_y + (y - o_y) * cosd(angle) + (x - o_x) * sind(angle);

    return std::pair<double, double>(x, y);

}

The function rotate_around is called with the following parameters (original x, original y , middle x of the rectangle, middle y of the rectangle, angle of the rectangle)使用以下参数调用函数 rotate_around(原始 x、原始 y、矩形的中间 x、矩形的中间 y、矩形的角度)

Can someone please tell me what am I doing wrong here?有人可以告诉我我在这里做错了什么吗?

x-= o_x; y-= o_y;
angle= d2r(angle);
double c= std::cos(angle), s= std::sin(angle);

return std::pair<double, double>(o_x + x * c - y * s, o_y + y * c + x * s);

will work.将工作。

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

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