Advertisement

04.11.2008 at 05:48PM PDT, ID: 23316788
[x]
Attachment Details

Rotate a 2D polygon in C#

Asked by dsmclaughlin in Microsoft Visual C#.Net, Game Programming, Math & Science Questions

Tags: C#, .NET, Visual Studio

Hi,

I am in the process of writing a piece of code that will rotate a polygon (in C# a Point[]) I found an example here http://www.codeguru.com/forum/showthread.php?t=346093 (4th post) and tried to recreate it in C# (see attached code Snippet).

This code works to a certain degree, however I have two problems with it:

1) The polygon does rotate but not by the angle specified.

2) The polygon seems to shrink as it rotates.

The effect of calling rotatePolygon can be seen in the attached image. The red point is the point around which the polygon is being rotated.

My maths is not good enough to try and figure out why this isn't working so any advice on how to tweak this method is very much appreciated!

Many thanks

DrewStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public Point[] rotatePolygon(Point[] polygon, Point centroid, double angle)
        {
            for(int i = 0; i < polygon.Length; i++)
            {
                 polygon[i] = rotatePoint(polygon[i], centroid, angle);
            }
 
            return polygon;
        }
 
        public Point rotatePoint(Point point, Point centroid, double angle)
        {
            int x = centroid.X + (int)((point.X - centroid.X) * Math.Cos(angle) - (point.Y - centroid.Y) * Math.Sin(angle));
 
            int y = centroid.Y + (int)((point.X - centroid.X) * Math.Sin(angle) + (point.Y - centroid.Y) * Math.Cos(angle));
 
            return new Point(x, y);
        }
[+][-]04.11.2008 at 05:51PM PDT, ID: 21339330

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.11.2008 at 06:46PM PDT, ID: 21339479

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.11.2008 at 08:57PM PDT, ID: 21339776

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Game Programming, Math & Science Questions
Tags: C#, .NET, Visual Studio
Sign Up Now!
Solution Provided By: jgordos
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.11.2008 at 08:59PM PDT, ID: 21339780

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.12.2008 at 03:46AM PDT, ID: 21340520

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.12.2008 at 07:24AM PDT, ID: 21341255

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.12.2008 at 07:45AM PDT, ID: 21341350

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.12.2008 at 08:13AM PDT, ID: 21341452

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628