CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2010
    Posts
    5

    Trig Problem: Rotating points in array

    Hi guys,

    I'm having a bit of trouble with a bit of trig in C#. I've written code similar to this in actionscript3 (flash), but it is giving me issues in C#. I am not receiving errors, but all the shapes I rotate are somewhat screwed up.

    It is worth mentioning that Form1.rad is a constant I am using to convert degrees into radians and back. Math.pi/180

    Here is the points array I am using:
    Code:
    static private Point[] drawingPoints = new Point[13]{new Point(14,0),new Point(19,9),new Point(19,18),new Point(25,30),new Point(19,26),new Point(19,18),new Point(19,26),new Point(8,26),new Point(8,18),new Point(0,30),new Point(8,26),new Point(8,9),new Point(14,0)};
    Here is the rotation code + the code to draw this.

    Code:
    public void draw (Graphics g)
            {
                Point[] rotatedPoints = new Point[13];
    
                
                for (short index = 0; index < rotatedPoints.Length; index++)
                {
                    //Finding the location of the points relative to the upper left of the ship.
                    Point curPoint = drawingPoints[index];
                    Point newPoint = new Point(curPoint.X,curPoint.Y);
    
                    double distToPoint = Form1.distTo(curPoint.X,curPoint.Y,centerPoint.X,centerPoint.Y);//we treat this as the hypotenuse.
                    
                    decimal degToPoint = (decimal)Math.Atan(centerPoint.Y - curPoint.Y / centerPoint.X - curPoint.X)/Form1.rad;//Gets the degrees from the center point to the current drawing point without any rotation yet.
                    decimal degToNewPoint = degToPoint + rotation;//Add the rotation
    
                    decimal cosToNewPoint = (decimal)Math.Cos((double)(degToNewPoint*Form1.rad));//From the center the cosine.
    
                    //Since cos is adj/hyp to get adj multiply by the hyp.
                    newPoint.X = (int)(cosToNewPoint * (decimal)distToPoint);
                    
                    decimal sinToNewPoint = (decimal)Math.Sin((double)(degToNewPoint * Form1.rad));//From the center the cosine.
    
                    Debug.Fail(sinToNewPoint.ToString());
    
                    //Same idea as with the cosine
                    newPoint.Y = (int)(sinToNewPoint * (decimal)distToPoint);
    
                    //Adding points to drawing array
                    rotatedPoints[index] = newPoint;
                }
                //Adding the screen x y location to the points:
    
                for (short i = 0; i < rotatedPoints.Length; i++)
                {
                    rotatedPoints[i].X += this._x;
                    rotatedPoints[i].Y += this._y;
                }
                Pen shipPen = new Pen(shipColor, 2);
                g.DrawLines(shipPen, rotatedPoints);
            }
    I know that the drawing code works correctly. If you comment out the rotation parts of the code, it displays my drawing just fine. What is annoying, is that if the rotation is something other than zero, the rotation works, but the shape is still flattened/scewed on one side.


    This problem is driving me up the wall, please help .

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trig Problem: Rotating points in array

    Why not replace all of that code with a simple rotation matrix? It takes a point and a theta to rotate by.

    Code:
    public PointF Rotate(Point p, float theta)
    {
        float rx = (p.x * Math.Cos(theta)) - (p.y * Math.Sin(theta))
        float ry = (p.y * Math.Cos(theta)) + (p.x * Math.Sin(theta))
        return new PointF( rx, ry );
    }

  3. #3
    Join Date
    Jul 2010
    Posts
    5

    Re: Trig Problem: Rotating points in array

    This seems like it would be much more effective. However I don't understand this exactly. Do I the point that I am rotating in the first argument, and the degrees in the second? If that is the case, I don't see how I can get it to rotate around a specific point?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trig Problem: Rotating points in array

    The rotation will be around the origin, wherever that may be. For example, in our case the origin will likely be the top left corner. You can offset the point afterward if needed.

  5. #5
    Join Date
    Jul 2010
    Posts
    5

    Re: Trig Problem: Rotating points in array

    How would I do that?

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trig Problem: Rotating points in array

    Well, I suppose that the simplest way would just be to add to each point. For example, if you want an origin in the center of the screen:

    Code:
    Point rotated = Rotate(somePoint, someTheta);
    rotated.X += screenWidth / 2 
    rotated.Y += screenHeight / 2
    Or you could just incorporate that logic in the rotate method itself, i.e., pass in another point that defines the origin.

  7. #7
    Join Date
    Jul 2010
    Posts
    5

    Re: Trig Problem: Rotating points in array

    Alright, that makes sense. Sorry to keep pestering you, but what is theta?

    Further how does it know to keep the distance from the point of rotation the same?

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trig Problem: Rotating points in array

    theta is a degree measured in radians. As for the second question, it knows because of the trigonometric functions. Not sure how else to explain it, if you know what the trig functions actually mean it makes sense.

  9. #9
    Join Date
    Jul 2010
    Posts
    5

    Re: Trig Problem: Rotating points in array

    Alright, thanks for your help - it works, so I guess I should be happy. Will look into it further next time.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trig Problem: Rotating points in array

    Think of it as building triangles inside of the circle at some angle theta. You can find the vertices of that triangle using trig, i.e., you can find points on the circle.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured