所以我要编写一个函数,该函数需要一个点,并以某个角度围绕另一个点旋转它,所以当我绘制一个矩形时,我也希望该矩形也旋转,但它会变形。绘制两个彩色矩形,一个不旋转,另一个按所提供的角度旋转,如下图所示:
紫色轮廓应看起来像红色轮廓。 这是旋转代码
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);
}
使用以下参数调用函数rotate_around (原始x,原始y,矩形的中间x,矩形的中间y,矩形的角度)
有人可以告诉我我在做什么错吗?
答案 0 :(得分:0)
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);
将起作用。