绕另一点一个点 [英] Rotate a point around another point

查看:216
本文介绍了绕另一点一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个任务绘制特定图形。由于这个任务,我需要在旋转45度的一些点的一部分。

I have a task to draw a specific graphic. As part of this task I need to rotate some dot's on 45 degrees.

我已经花了2天尝试计算公式,但只是无法得到它对。
我一直在寻找全国各地,包括这个特定的网站,我已经非常接近,但我仍然不存在。

I've spent already 2 days trying to calculate a formula, but just couldn't get it right. I've been searching all over the place including this particular website, I'm getting very close, but I'm still not there.

这地方是:
我需要绘制4个不同的点

Here it is: I need to draw 4 different points

我有一个特定的公式来计算有位置,这是出了问题的范围,但这里是什么我得到作为它的结果:

I have a specific formula to calculate there position, which is out of scope of the question, but here is what I'm getting as a result of it:

int radius = 576;
int diameter = radius * 2;
Point blueA = new Point(561, 273);
Point greenB = new Point(273, 561);
Point yellowC = new Point (849, 561);
Point redD = new Point (561, 849);



结果

现在我需要在45度旋转,这点。我用下面的代码来实现它:

Now I need to rotate this dots on 45 degrees. I use the following code to achieve it:

double rotationAngle = 45;
double rotationRadians = rotationAngle * (Math.PI / 180);
int center = radius;    
result.X = (int)(Math.Cos(rotationRadians) * ((double)result.X - (double)center) - (double)Math.Sin(rotationRadians) * ((double)result.Y - center) + (double)center);
result.Y = (int)(Math.Sin(rotationRadians) * ((double)result.X - (double)center) + (double)Math.Cos(rotationRadians) * ((double)result.Y - center) + (double)center);



但是,这就是我得到:

But that's what I'm getting:

任何帮助将不胜感激

推荐答案

问题是 INT中心=半径为其设置 INT半径= 576 。因为肯定你绕应该有一个X和Y位置的点这是没有意义的。

The problem is int center = radius which you are setting int radius = 576. This doesn't make sense as surely you are rotating about a point that should have an x and y location.

给你周围的起源中心 X 既要为 0 不是 576

Given you are rotating around the origin the center x and y should both be 0 not 576.

因此,鉴于这种情况,试试这个

So, given that, try this.

/// <summary>
/// Rotates one point around another
/// </summary>
/// <param name="pointToRotate">The point to rotate.</param>
/// <param name="centerPoint">The center point of rotation.</param>
/// <param name="angleInDegrees">The rotation angle in degrees.</param>
/// <returns>Rotated point</returns>
static Point RotatePoint(Point pointToRotate, Point centerPoint, double angleInDegrees)
{
    double angleInRadians = angleInDegrees * (Math.PI / 180);
    double cosTheta = Math.Cos(angleInRadians);
    double sinTheta = Math.Sin(angleInRadians);
    return new Point
    {
        X =
            (int)
            (cosTheta * (pointToRotate.X - centerPoint.X) -
            sinTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.X),
        Y =
            (int)
            (sinTheta * (pointToRotate.X - centerPoint.X) +
            cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y)
    };
}

使用像这样。

Point center = new Point(0, 0); 
Point newPoint = RotatePoint(blueA, center, 45);

如果中心点总是很显然 0,0 则可以相应地简化功能,否则使中心点通过默认的参数可选,或者通过重载方法。你也可能会想要封装了一些可重复使用的数学到其他静态方法了。

Obviously if the center point is always 0,0 then you can simplify the function accordingly, or else make the center point optional via a default parameter, or by overloading the method. You would also probably want to encapsulate some of reusable the math into other static methods too.

例如

/// <summary>
/// Converts an angle in decimal degress to radians.
/// </summary>
/// <param name="angleInDegrees">The angle in degrees to convert.</param>
/// <returns>Angle in radians</returns>
static double DegreesToRadians(double angleInDegrees)
{
   return angleInDegrees * (Math.PI / 180);
}

/// <summary>
/// Rotates a point around the origin
/// </summary>
/// <param name="pointToRotate">The point to rotate.</param>
/// <param name="angleInDegrees">The rotation angle in degrees.</param>
/// <returns>Rotated point</returns>
static Point RotatePoint(Point pointToRotate, double angleInDegrees)
{
   return RotatePoint(pointToRotate, new Point(0, 0), angleInDegrees);
}

使用像这样。

Point newPoint = RotatePoint(blueA, 45);



最后,如果您使用的是GDI,你也可以简单地做一个 RotateTransform
请参阅: http://msdn.microsoft.com/en-us /library/a0z3f662.aspx

Graphics g = this.CreateGraphics();
g.TranslateTransform(blueA);
g.RotateTransform(45);

这篇关于绕另一点一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆