求救,坐标旋转算法问题
各位大神,遇到一个坐标旋转的问题,旋转后坐标总是不对求助。
具体问题:有一个点坐标(-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度计算后重新转回原坐标的算法?项目紧急状态,望大神帮忙!
具体问题:有一个点坐标(-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度计算后重新转回原坐标的算法?项目紧急状态,望大神帮忙!
...全文
相关推荐
C#坐标旋转算法
理解并熟练运用这些基本的坐标旋转和距离计算算法,将有助于你在C#项目中创建复杂的图形效果和动态交互。
EXCEL 三维图坐标旋转
可以在excel中显示三维x、y、z散点图,坐标旋转公式、vba编程
坐标旋转公式推导
理解并熟练运用这些公式,可以帮助我们更好地理解和处理与旋转相关的各种问题。
[转]坐标旋转
由于没有直接提供文章内容,我将根据一般坐标旋转的概念和常见方法来详细解释这一主题。坐标旋转通常涉及到二维或三维空间中的点或对象相对于固定原点(通常是坐标系的中心)的转动。
OpenTK坐标旋转.zip
相对于矩阵,四元数通常更节省内存,避免了万向锁问题,并且更容易执行组合旋转。在OpenTK中,`Quaternion.CreateFromAxisAngle`方法可以创建一个基于指定轴和角度的四元数。


