C++ WinAPI多边形旋转变形
我的polygonsquare有点问题,它在旋转时变形,尝试了标准函数SetWorldTransform,但失望了。 旋转功能正常。可能,主要问题在于每次旋转后的误差C++ WinAPI多边形旋转变形,c++,winapi,graphics,C++,Winapi,Graphics,我的polygonsquare有点问题,它在旋转时变形,尝试了标准函数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;
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);
}
将坐标旋转为浮点数,然后强制转换为整数以存储回点结构。截断错误正在累积并导致失真。将点存储到自定义点结构中,使用双精度替换整数。对于所有逻辑操作,使用此类型替换点
struct PrecisePoint
{
double x;
double y;
}
然后将它们复制到Polygonhdc、pnts、5之前的点阵列中;
您可以添加如下方法:
precisePointsToPoints( PrecisePoint[] src, POINT[] dst, length);
你有x-yCenter和y-xCenter,对吗?x和y中心是一样的,所以问题不在这一点上,谢谢你的回复。你的代码不完整;特别是,它似乎缺少一个主要功能和至少一个include。请输入您的代码,使其成为您问题的一部分,然后我们可以尝试复制并解决它。你也应该阅读。