rotate polygon in iOS

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

rotate polygon in iOS

Post by joel.epsteinBUS31vi » Sat Nov 03, 2012 6:32 am

Hi all -

I've used the RevRotatePoly command and it works great on the Mac, but I see that it's not supported for iOS. What options do I have to rotate a polygon on the iOS platform?

Thanks so much for any advice.

Joel

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: rotate polygon in iOS

Post by dave_probertGA6e24 » Sat Nov 03, 2012 7:44 am

Hi,

For a Regular Polygon you should just need to do something like this:

Code: Select all

set the angle of grc "mypoly" to 120
simply use a variable in place of the 120 and you can have complete control.

Put the code above into a recursive callback routine (ie. a handler that calls itself after a short delay) and it could be animated.

For a points based polygon, you will need to get the points as a list and apply the sin/cos stuff to each point and then set the points of the graphic. More longwinded, but not too difficult.

Hope that helps,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: rotate polygon in iOS

Post by joel.epsteinBUS31vi » Sat Nov 03, 2012 3:24 pm

Thanks for the reply. I am indeed trying to rotate a point-based polygon. Based on your suggestion, I did some searching and found Jim Hurley's example which I modified as follows and put into the script of a scrollbar:

Code: Select all

global gOldRotVal, gWhereAt

on MouseDown
   put the loc of graphic "myPoly" into gWhereAt
end MouseDown

on scrollbarDrag newValue 
   put gOldRotVal-newValue into tDelta
   put newValue into gOldRotVal
   rotatePoly "myPoly", gWhereAt, tDelta
end scrollbarDrag

On rotatePoly theGraphic, thePivot, theAngle
   put the points of graphic theGraphic into tPoints
   put item 1 of thePivot into xPivot
   put item 2 of thePivot into yPivot
   put empty into newPointList

   repeat for each line tLine in tPoints
     put (item 1 of tLine)- xPivot & "," & (item 2 of tLine)- yPivot & return after tRelPoints
   end repeat

   put sin(theAngle* pi/180) into S
   put cos(theAngle * pi/180) into C

   repeat for each line tLine in tRelPoints
     put round(C*(item 1 of tLine)+ S*(item 2 of tLine) + xPivot)& comma after rotPtlist
     put round(-S*(item 1 of tLine) + C*(item 2 of tLine)+ yPivot)after rotPtlist
     put return after rotPtList
   end repeat

   set the points of graphic theGraphic to rotPtlist

end rotatePoly
Dragging the scrollbar produces a beautifully smooth rotation of the polygon both clockwise and counterclockwise. That's great.

But...

My problem is that with the rounding above, the polygon gets horribly distorted. I read somewhere that there's a way to handle that. I didn't quite understand, but it seemed to be that the procedure would be to keep a clean copy of the polygon somewhere else for reference.

Can anyone please help me figure out what to do in order to rotate the point-based polygon and minimize the distortion?

Thanks so much.

Joel

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: rotate polygon in iOS

Post by sturgis » Sat Nov 03, 2012 3:58 pm

Here is an example stack that does what you ask, there will still be some slight munging but it will be relative to the start position. Also, the script you posted works REALLY well I noticed very little deformation in my tests. Here is the stack with adjusted script.

https://dl.dropbox.com/u/11957935/polyrotate.livecode

And here is the script itself. Requires a slider (scrollbar) with values 0 to 359 (or whatever if you want multi rotation or partial rotation capability)
There is an init handler that saves the original points to a global as well as the start loc.
Rather than do relative changes angle to angle, just takes a value and rotates the original points to the desired position.

Code: Select all

global gWhereAt, gStartPoints -- added gstartpoints gOldRotVal not needed anymore. 

on preopenstack
## disable stuff until an init has been done. Could just init the polygon here.
   set the enabled of scrollbar 1 to false
   set the enabled of grc "myPoly" to false
end preopenstack

on initRot
   -- the init handler.  Called from a button or however you wish
   put the loc of graphic "myPoly" into gWhereAt
   put the points of grc "myPoly" into gStartPoints -- store the points
end initRot

on scrollbarDrag newValue 
   put newValue into tDelta -- no math here, take the value directly from the scroolbar. 
    rotatePoly "myPoly", gWhereAt, tDelta
end scrollbarDrag

On rotatePoly theGraphic, thePivot, theAngle
   lock screen
   --using only the original points for rotation, so don't grab 
   -- the current points
   --put the points of graphic theGraphic into tPoints

   -- use the original points for all calculations
   put gStartPoints into tPoints
   put item 1 of thePivot into xPivot
   put item 2 of thePivot into yPivot
   put empty into newPointList
   
   repeat for each line tLine in tPoints
      put (item 1 of tLine)- xPivot & "," & (item 2 of tLine)- yPivot & return after tRelPoints
   end repeat
   
   put sin(theAngle* pi/180) into S
   put cos(theAngle * pi/180) into C
   
   repeat for each line tLine in tRelPoints
      put round(C*(item 1 of tLine)+ S*(item 2 of tLine) + xPivot)& comma after rotPtlist
      put round(-S*(item 1 of tLine) + C*(item 2 of tLine)+ yPivot)after rotPtlist
      put return after rotPtList
   end repeat
   
   set the points of graphic theGraphic to rotPtlist
   unlock screen
end rotatePoly

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: rotate polygon in iOS

Post by joel.epsteinBUS31vi » Sun Nov 04, 2012 5:56 am

Oh my goodness! I'm so grateful for your help. That works perfectly.
Thank you so much.
Joel

Post Reply