2012-12-04 10 views
17

Mam zadanie narysować określoną grafikę. W ramach tego zadania muszę obrócić niektóre kropki na 45 stopni.Obracanie punktu wokół innego punktu

Spędziłem już 2 dni próbując obliczyć formułę, ale po prostu nie mogłem tego naprawić. Szukałem wszędzie w tym tej konkretnej witryny, jestem bardzo blisko, ale wciąż mnie nie ma.

Oto ona: muszę narysować 4 różne punkty

mam konkretny wzór do obliczenia tam pozycję, która jest poza zakresem tego pytania, ale tutaj jest to, co dostaję w wyniku z tego:

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

teraz trzeba obracać to kropki na 45 stopni. Używam następujący kod do osiągnięcia go:

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

Ale to co mi chodzi:

Result

Każda pomoc będzie mile widziane

Odpowiedz

35

Problem polega int center = radius który jesteś ustawienie int radius = 576. To nie ma sensu, ponieważ na pewno obracasz się wokół punktu, który powinien mieć lokalizację xiy.

Biorąc pod uwagę, że obracasz się wokół punktu początkowego, środek x i y powinny być zarówno 0 nie 576.

Więc, biorąc pod uwagę to, spróbuj tego.

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

Użyj jak.

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

Oczywiście jeśli punkt środkowy jest zawsze 0,0 następnie można uprościć funkcję odpowiednio, albo sprawić, że punkt środkowy poprzez opcjonalny parametr domyślny, lub przeciążenia metody. Prawdopodobnie chciałbyś również zarekomatyzować niektóre z matematyki wielokrotnego użytku na inne metody statyczne.

np.

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

Użyj jak.

Point newPoint = RotatePoint(blueA, 45); 

Wreszcie, jeśli używasz GDI można też po prostu zrobić RotateTransform. Patrz: http://msdn.microsoft.com/en-us/library/a0z3f662.aspx

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

Właśnie zobaczyłem ten post po tym, jak napisałem. Ta formuła działa. –

+0

jest idealny! Dziękuję Ci. Oto aktualny zrzut ekranu: http://s8.postimage.org/e7r44klcl/result.png –

+1

Masz rację. Uczestnik biblioteki systemu operacyjnego musiał skopiować tę kopię ręcznie, ponieważ nawiasy zostały zgniecione, a testy jednostkowe wypadły źle. –

1

Jesteś matematyki wygląda dziwnie na mnie. Myślę, że dx = r * Cos (theta) i dy = r * Sin (theta).

Oto mały program, który napisałem, ponieważ przeszkadzało mi to, a nie uczyniłem matematyki, to lata.

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(); 
+0

Używam tej metody w różnych częściach kodu i na pewnym etapie otrzymałem "poprzedni" punkt o tych współrzędnych: X = 576 Y = 20. W tym przypadku otrzymuję "DivideByZeroException" Tutaj jest wynik, jeśli wstawię blok "try -> catch" w: http://s10.postimage.org/azjdc7rex/exception.png –

Powiązane problemy