几何形状对象C ++的旋转错误

时间:2020-06-30 06:54:29

标签: c++ rotation scale translate

您好,请参阅下面显示的图像。我的多边形形状对象旋转错误,除了沿位图图像中的轴沿圆周旋转之外。我应该走一条直路。但是,我得到了一条弯曲的道路。我怀疑这是由于我的代码中的旋转功能。在我的main()中,我将首先调用翻译函数, t-> translate({0.0f,50.0f}),就像这样。然后,我将调用rotate:r-> rotate(0.25f),然后按比例尺:s-> scale(0.85f);注意:main()属于测试用例,因此无法永久进行编辑。

所以,我的问题是什么导致多边形对象沿着不同于第二张图片中所示的固定路径的其他路径旋转?无法弄清楚。是否也可能是由于渲染了位图图像?希望有人能够提供帮助。用于平移,缩放和旋转的功能如下。也许有人可以列出为什么会这样的几种可能性。谢谢!

Wrong rotation

Correct rotation

翻译功能

void Point::translate(const Object::VectorType& displacement){
    float nx, ny;
    nx = displacement.x() + _pos.x();
    ny = displacement.y() + _pos.y();
   _pos = Object::PointType(nx, ny);
}

旋转功能

void Polygon::rotate(float angle){
float nx, ny;

for(PointType& vertice : _pvertices){
    float xr = vertice.x() - Point::_pos.x();
    float yr = vertice.y() - Point::_pos.y();
    
    nx = Point::_pos.x() + std::cos(angle) * xr - std::sin(angle) * yr;
    ny = Point::_pos.y() + std::sin(angle) * xr + std::cos(angle) * yr;
    
    vertice = PointType(nx, ny);
}
}

缩放功能

void Polygon::scale(float factor){
for(PointType& vertice : _pvertices){
    vertice = PointType((Point::_pos.x() + factor * 
                        (vertice.x() - Point::_pos.x())),
                        (Point::_pos.y() + factor * 
                        (vertice.y() - Point::_pos.y())));
}
}

0 个答案:

没有答案