C# 在 WinForms 中以默认角度围绕中心点旋转 2 个控件
C# Rotate 2 controls around a center point at the defaul angle in WinForms
我试图旋转 2 个 windows 表单按钮,如下图所示:
在旋转过程中,它们之间的距离应为0,当您单击标签时,它们应"rotate" 90度,如:
if (red is up and black is down)
{
red will be down and black will be up;
}
else
{
red will be up and black will be down;
}
我用这个方法return想要的点"location",但是我无法获得想要的旋转"effect":
public static Point Rotate(Point point, Point pivot, double angleDegree)
{
double angle = angleDegree * Math.PI / 180;
double cos = Math.Cos(angle);
double sin = Math.Sin(angle);
int dx = point.X - pivot.X;
int dy = point.Y - pivot.Y;
double x = cos * dx - sin * dy + pivot.X;
double y = sin * dx + cos * dy + pivot.X;
Point rotated = new Point((int)Math.Round(x), (int)Math.Round(y));
return rotated;
}
就像我的评论一样,它必须看起来像这样:
private Point calculateCircumferencePoint(double radoffset, Point center, double radius)
{
Point res = new Point();
double x = center.X + radius * Math.Cos(radoffset);
double y = center.Y + radius * Math.Sin(radoffset);
res.X = (int)x;
res.Y = (int)y;
return res;
}
这里还有一个测试应用:https://github.com/hrkrx/TestAppCircularRotation
编辑:对于第二个按钮,您只需将初始偏移设置为 Math.PI
;
EDIT2:要像交叉一样旋转按钮(如 8 的路径),您需要将半径设置为 Sin(radoffset)
或 Cos(radoffset)
我试图旋转 2 个 windows 表单按钮,如下图所示:
在旋转过程中,它们之间的距离应为0,当您单击标签时,它们应"rotate" 90度,如:
if (red is up and black is down)
{
red will be down and black will be up;
}
else
{
red will be up and black will be down;
}
我用这个方法return想要的点"location",但是我无法获得想要的旋转"effect":
public static Point Rotate(Point point, Point pivot, double angleDegree)
{
double angle = angleDegree * Math.PI / 180;
double cos = Math.Cos(angle);
double sin = Math.Sin(angle);
int dx = point.X - pivot.X;
int dy = point.Y - pivot.Y;
double x = cos * dx - sin * dy + pivot.X;
double y = sin * dx + cos * dy + pivot.X;
Point rotated = new Point((int)Math.Round(x), (int)Math.Round(y));
return rotated;
}
就像我的评论一样,它必须看起来像这样:
private Point calculateCircumferencePoint(double radoffset, Point center, double radius)
{
Point res = new Point();
double x = center.X + radius * Math.Cos(radoffset);
double y = center.Y + radius * Math.Sin(radoffset);
res.X = (int)x;
res.Y = (int)y;
return res;
}
这里还有一个测试应用:https://github.com/hrkrx/TestAppCircularRotation
编辑:对于第二个按钮,您只需将初始偏移设置为 Math.PI
;
EDIT2:要像交叉一样旋转按钮(如 8 的路径),您需要将半径设置为 Sin(radoffset)
或 Cos(radoffset)