What goes around... revolutions in Revolution

Derek Huby derek.huby at ntlworld.com
Mon Dec 9 17:27:01 EST 2002


Dear all,

I think it's nice to have something new to play with while waiting for 2.0
;-) 

If anybody's interested, I've been taking some polygons for a spin...

The ability to rotate an image is built in to Rev, of course; but rotating a
polygon-style graphic seems to offer a lot more flexibility. Imagine - you
could build an arrow 'pointer' that swings around a scale, rotate a
'forearm' around an 'elbow', model the listing of a ship's hull... oh, lots
of good stuff, really.

So, how to do it? My attempt (script below) rotates a graphic by a given
angle, using another small graphic to mark the centre of rotation. I'd be
interested to know whether I'm reinventing the wheel here; however, even if
I am, I enjoyed doing it!

Here's the card script:

***************

on rotateshape shapename, angle
  -- this will rotate gc shapename through 'angle' degrees anticlockwise
 
  -- check the graphic exists
  if there is not a graphic shapename
  then
    exit rotateshape
  end if
 
  --"CoR" (centre of rotation) is a small cicular 'grabbable' gc marking the
centre
  put item 1 of the loc of graphic "CoR" into XCentre
  put item 2 of the loc of graphic "CoR" into YCentre
 
  --"origPts" is a custom property containing the original point list.
  --You can't just rotate from the 'current' point list; rounding errors
will 'degrade' the shape.
 
  if the origPts of graphic shapename = empty
  then
    set the origPts of graphic shapename to the points of graphic shapename
  end if
 
  --"rotAngle" is a custom property that tells you how far the shape is
--currently rotated
 
  if the rotAngle of graphic shapename = empty
  then
    set the rotAngle of graphic shapename to 0
  end if
  
  put empty into newPointList
  put the origPts of graphic shapename into pointList
 
  -- Build new list of coords relative to CoR as origin
  repeat for each line k in pointlist
    put (item 1 of k)- Xcentre & "," & (item 2 of k)- Ycentre & return after
newPointList
  end repeat
 
  
  set the rotangle of graphic shapename to (the rotangle of graphic
shapename + angle) mod 360
 
 
  put sin(the rotangle of graphic shapename * pi/180) into S
  put cos(the rotangle of graphic shapename * pi/180) into C
 
  put empty into rotptlist
 
  --Now do the actual rotation
  repeat for each line k in newPointlist
    put round(c*(item 1 of k)+ s*(item 2 of k) + xCentre) after rotptlist
    put "," after rotptlist
    put round(c*(item 2 of k) - s*(item 1 of k)+ Ycentre)after rotptlist
    put return after rotPtList
  end repeat
 
  set the points of graphic shapename to rotPtlist
 
end rotateshape

***************

I've also got a button to call this handler; its script is:

***************

on mouseup
  repeat 180
    rotateshape "arrow1", 2
    wait 1 millisecond
  end repeat
end mouseup

***************

This rotates a graphic called "arrow1" through 360 degrees.
Finally, I have a small circular graphic called CoR. It's script is
(predictably):

***************
on mousedown
  grab me
end mousedown
***************

This could all do with a bit more work - changing the centre of rotation
doesn't work properly, for example. But it does show the possibilities...


Derek


-- 
Derek Huby
KS3 Mathematics Consultant
East Sussex County Council




More information about the use-livecode mailing list