I'm trying to translate a point around the circumference of a circle.
To test my code I just translate the point and draw a line from the centre to the translated point for each angle increment.
This should look like a bike wheel, however it looks more like a spade. Oddly, it appears the same whether I used radians or degrees in the Math functions.
for (double degrees = 0; degrees <= 360; degrees += 1)
{
double radians = Math.PI * degrees / 180.0;
Console.Write("degrees = " + degrees + " radians = " + radians);
double sx = 0.0;
double sy = -100.0;
sx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(ra开发者_StackOverflowdians)));
sy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));
Console.WriteLine(", sx = " + sx + ", sy = " + sy);
g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)sx+200, (int)sy+200);
}
So your maths is right. You do calculate the rotation using this matrix.
[cos(r) sin(r)]
[-sin(r) cos(r)]
But your coding is wrong!
You have already changed sx
to the rotated value, before you use it to calculate sy
!
You are performing calculations on x and y sequentially, whereas in a matrix, they are done simultaneously. So your y coordinate is computed incorrectly, using a sheared coordinate system, as it were...
You need to do this:
double sx = 0.0;
double sy = -100.0;
double nsx = ((sx * Math.Cos(radians)) + (sy * Math.Sin(radians)));
double nsy = (-(sx * Math.Sin(radians)) + (sy * Math.Cos(radians)));
g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, (int)nsx+200, (int)nsy+200);
double radius = 200;
for (double degrees = 0; degrees <= 360; degrees += 1)
{
double radians = Math.PI * degrees / 180.0;
int sx = (int) (200 + radius * Math.Cos(radians));
int sy = (int) (200 + radius * Math.Sin(radians));
g.DrawLine(new Pen(Brushes.GhostWhite), 200, 200, sx, sy);
}
I think it's purely a mathematical issue. Try:
double radius = 100;
double sx = radius * Math.Cos(radians);
double sy = radius * Math.Sin(radians);
精彩评论