求救,坐标旋转算法问题

电脑不需要休息吗? 电脑不需要休息吗? 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大神,遇到一个坐标旋转的问题,旋转后坐标总是不对求助。
具体问题:有一个点坐标(-75,350),需要求解以坐标原点为轴旋转45的坐标。
DPoint 为自己重载的Point类,改用double类型表示坐标。

解决方法1:使用c#Graphic的方法进行旋转:
private DPoint Get45DPoint(DPoint pt, float angle)
{
float tAngle = 0;
tAngle = angle;
Bitmap bit = (Bitmap)this.pSpots.BackgroundImage;
bit = (Bitmap)this.pSpots.BackgroundImage;
Graphics gct = Graphics.FromImage(bit);
PointF tp = _Origin.getFormPoint(new PointF((float)pt.X, (float)pt.Y), 5);
PointF[] points = new PointF[] { new PointF((float)tp.X, (float)tp.Y) };
gct.TranslateTransform(_Origin.X, _Origin.Y);
gct.RotateTransform(tAngle);
gct.TranslateTransform(-_Origin.X, -_Origin.Y);
gct.Transform.TransformPoints(points);

DPoint c2 = new DPoint((double)points[0].X, (double)points[0].Y);
c2 = _Origin.getDPointFromFormPoint(new Point((int)points[0].X, (int)points[0].Y));
return c2;
}
原坐标(-75,350)旋转后坐标(194,305);距离原点的距离:357.94552658190884,361.47060738046184

方法2:自己实现坐标旋转
private DPoint Get45DPoint2(DPoint p, float angle = 45)
{
float tAngle = 45;
tAngle = angle;
DPoint ori = new DPoint(0, 0);

double x = ori.X + (p.X - ori.X) * Math.Cos(tAngle) - (p.Y - ori.Y) * Math.Sin(tAngle);
double y = ori.Y + (p.X - ori.X) * Math.Sin(tAngle) - (p.Y - ori.Y) * Math.Cos(tAngle);

DPoint c2 = new DPoint(x, y);

return c2;
}
原坐标(-75,350)旋转后坐标( -337.22,-247.68);到原点距离分别为357.94552658190884,418.40496029564468
方法三:自己实现旋转算法2
private DPoint Get45DPoint3(DPoint p, float angle = 45)
{
float tAngle = 45;
tAngle = angle;
if (angle < 0)
{
tAngle = 360 + angle;
}
DPoint ori = new DPoint(0, 0);

double x = 0;
double y = 0;

x = p.X * Math.Cos(angle) - p.Y * Math.Sin(angle);
y = -p.X * Math.Sin(angle) + p.Y * Math.Cos(angle);

DPoint c2 = new DPoint(x, y);
return c2;
}

原坐标(-75,350)旋转后坐标(-337.22,247.68);到原点的距离分别为:357.94552658190884,418.40496029564468

求教:为什么旋转后到原点的距离会有这么大偏差?有没有误差在0.5以内的算法?哪位大神提供一个围绕原点旋转45度计算后重新转回原坐标的算法?项目紧急状态,望大神帮忙!
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
电脑不需要休息吗?
C# 111,129 成员 642,540 内容