Select to view content in your preferred language

dynamic rotation of polygon - how?

1188
2
06-28-2010 01:07 PM
grahamcooke
Regular Contributor
I need to allow the user to rotate a polygon in a graphics layer. I can do this via text input into a textbox but I would like to allow them to do this via the mouse if they wish and for the angle of rotation to be displayed in the input box as they change it with the mouse.

This is the code I have for the rotation bit at the moment!

//If a rotation angle is specified, call the rotation function

                            var rotationNumber:Number = 0;

                            rotationNumber = parseFloat(txt_Rotation.text);                                     

                            if(rotationNumber != 0)

                                  { //Go through array of points and calculate new point location.                                 

                                  var x:Number = 0;

                                  var y:Number = 0;                         

                                   

                                  var rotationAngle:Number = parseFloat("-" + txt_Rotation.text); //made negative so that behaves as compass bearing degrees rather than radian degree logic

                                        for(i = 0; i < ringArr.length; i++)

                                              { //Logic as follows: New x = originX (around which polygon must rotate) + ((point to rotate X - origin X) * cos(angle value) - (point to rotate Y - origin Y) * sin (angle value))  

                                                x = ringArr[0].x + (int) ((ringArr.x - ringArr[0].x) * Math.cos(Math.PI/180 * rotationAngle) - (ringArr.y - ringArr[0].y) * Math.sin(Math.PI/180 * rotationAngle));

                                            y = ringArr[0].y + (int) ((ringArr.x - ringArr[0].x) * Math.sin(Math.PI/180 * rotationAngle) + (ringArr.y - ringArr[0].y) * Math.cos(Math.PI/180 * rotationAngle));

 

                                            //Original code the above was derived from:

                                            //int X = origin.x + (int) ((point.x - origin.x) * cos(angle) -      (point.y - origin.y) * sin(angle));

                                            //int Y = origin.y + (int) ((point.x - origin.x) * sin(angle) +      (point.y - origin.y) * cos(angle));

                                            //point = CPoint(X, Y);

                                                ringArr = new MapPoint(x,y);   

                                              } 

                                    }     



this is part of the routine that displays a polygon which originates at an x/y location input by the user. The purpose is for angles of fire of a weapon and the polygon represents the weapons danger area which will be plotted on a map to safely plan firing exercises.

Hope someone can help

cheers

graham
Tags (2)
0 Kudos
2 Replies
ReneRubalcava
Esri Frequent Contributor
What you need to do is grab the coordinates of your mouse and calculate the angle of the mouse from your polygon. This site has a great simple explanation of how to rotate an object using mouse events.
http://www.foundation-flash.com/tutorials/as3rotation/

You could adapt that to the code you have now. That is probably the simplest way I've seen to calculate mouse to object angles.
0 Kudos
grahamcooke
Regular Contributor
Thanks very much for your post odoe, it helped a lot.

The polygons I am working with can also be dragged around the screen as well as rotated. So how can I distinguish between a mouse click for dragging and a mouse click for rotating? I was wondering about somehow creating "handles" on the polygon in the corners so that when the user wants to rotate it they click the handles, but when they want to drag it around they click the middle of the shape.

Does anyone have any experience of doing something like this in flex please?
0 Kudos