22

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

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.

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);

result

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:

Result

Any help would be much appreciated

Fraser
  • 15,275
  • 8
  • 53
  • 104
Allan Spreys
  • 5,287
  • 5
  • 39
  • 44

2 Answers2

57

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.

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)
    };
}

Use like so.

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

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 the reusable math into other static methods too.

e.g.

/// <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);
}

Use like so.

Point newPoint = RotatePoint(blueA, 45);

Finally, if you are using the GDI you can also simply do a RotateTransform. See: http://msdn.microsoft.com/en-us/library/a0z3f662.aspx

Graphics g = this.CreateGraphics();
g.TranslateTransform(blueA);
g.RotateTransform(45);
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • 1
    it is perfect! Thank you. Here is the current screenshot: http://s8.postimage.org/e7r44klcl/result.png – Allan Spreys Dec 05 '12 at 00:30
  • 1
    You are right. A contributor to an OS lib must have hand copied this because the parenthesis were screwed up and it came out wrong in the unit tests. – C Bauer Sep 14 '15 at 12:03
  • Could someone point to a resource to understand the equations in `RotatePoint`? – Bryce Guinta Aug 19 '20 at 23:11
  • 1
    @BryceGuinta It is just basic trig - https://www.mathsisfun.com/algebra/trigonometry-index.html – Fraser Aug 20 '20 at 11:41
  • 1
    I used this formula to figure out the angle needed to rotate something to a fixed position by solving for theta and setting the left hand of the equation to the value I want. For `x=0` this formula is `Math.Atan2(pointToRotate.Y - centerPoint.Y, centerPoint.X - pointToRotate.Y)`. Note that the regular atan does not work for all quadrants, so Atan2 is needed. – Bryce Guinta Aug 22 '20 at 01:35
1

You're math looks weird to me. I think dx = r*Cos(theta) and dy = r*Sin(theta).

Here's a little program I wrote because this was bothering me, and I haven't done math is years.

Point center = new Point() { X = 576, Y = 576 };

Point previous = new Point() { X = 849, Y=561 };
double rotation = 45;
double rotationRadians = rotation * (Math.PI / 180);

//get radius based on the previous point and r squared = a squared + b squared
double r = Math.Sqrt(Math.Pow(previous.X - center.X, 2) + Math.Pow(previous.Y - center.Y, 2));
Console.WriteLine("r = " + r.ToString());

//calculate previous angle
double previousAngle = Math.Atan((previous.Y - center.Y) / (previous.X - center.X));
Console.WriteLine("Previous angle: " + previousAngle.ToString());

double newAngle = previousAngle + rotationRadians;

Point newP = new Point();
newP.X = center.X + r * Math.Cos(newAngle);
newP.Y = center.Y + r * Math.Sin(newAngle);

Console.WriteLine("(" + newP.X.ToString() + ", " + newP.Y.ToString() + ")");

Console.ReadLine();
Nick Bray
  • 1,953
  • 12
  • 18
  • I use the method in different parts of the code and at some stage I got the "previous" point with these coordinates: X = 576 Y = 20. In this case I'm getting the "DivideByZeroException" Here is the result if I put the "try -> catch" block in: http://s10.postimage.org/azjdc7rex/exception.png – Allan Spreys Dec 05 '12 at 00:46