WinAPI多边形在旋转时变形

时间:2017-03-06 23:40:11

标签: c++ winapi graphics

我的多边形(方形)有问题,它在旋转时变形,尝试了标准功能SetWorldTransform,但很失望。 旋转功能正常。可能,主要问题是每次旋转后的错误。

int xCenter = 105;
int yCenter = 105;

POINT pnts[5];

square()
    {
        pnts[0].x = 70;
        pnts[0].y = 70;
        pnts[1].x = 140;
        pnts[1].y = 70;
        pnts[2].x = 140;
        pnts[2].y = 140;
        pnts[3].x = 70;
        pnts[3].y = 140;
        pnts[4].x = 70;
        pnts[4].y = 70;
    }

void Drawsquare(HWND hWin)
    {
        HDC hdc;
        HBRUSH hBrush;
        HPEN hPen;
        LOGBRUSH lBrush;
        hdc = GetDC(hWin);

        lBrush.lbStyle = BS_HOLLOW;
        hBrush = CreateBrushIndirect(&lBrush);
        hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
        SelectObject(hdc, hBrush);
        SelectObject(hdc, hPen);

        Polygon(hdc, pnts, 5);

        ReleaseDC(hWin, hdc);
    }

void Rotate(HWND hWin)
    {
        HDC hdc;
        RECT rect;
        hdc = GetDC(hWin);

        double pi = acos(-1);
        double ang = 45 * pi / 180;

        for(int i = 0; i < 5; i++)
        {
            pnts[i].x = (pnts[i].x - xCenter)*cos(ang) - (pnts[i].y - yCenter)*sin(ang) + xCenter;
            pnts[i].y = (pnts[i].x - xCenter)*sin(ang) + (pnts[i].y - yCenter)*cos(ang) + yCenter;
        }

        GetClientRect(hWin, &rect);
        ClearScreen(hdc, rect);
        Drawsquare(hWin);
        ReleaseDC(hWin, hdc);
    }

2 个答案:

答案 0 :(得分:2)

将您的积分存储到自定义点结构中,而不是使用此类型代替POINT进行所有逻辑运算

struct PrecisePoint 
{
   double x;
   double y;
}

然后将它们复制到Polygon(hdc,pnts,5)之前的POINT数组中; 你可以添加如下方法:

 precisePointsToPoints( PrecisePoint[] src, POINT[] dst, length);

答案 1 :(得分:1)

您的坐标被旋转为浮点数,然后强制转换为整数以存储回POINT结构。截断误差正在累积并导致失真。