繁体   English   中英

WinAPI多边形在旋转时变形

[英]WinAPI Polygon Deform on Rotation

我的多边形(正方形)在问题上有点卡住,它在旋转时变形,尝试使用标准功能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);
    }

将您的点存储为带有双精度的自定义点结构,而不是ints对所有逻辑运算使用此类型而不是POINT

struct PrecisePoint 
{
   double x;
   double y;
}

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

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

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

暂无
暂无

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

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