Rotating Polygons
Alex Tweedly
alex at tweedly.net
Tue Mar 29 12:10:45 EST 2005
Rick Harrison wrote:
> Hi there,
>
> I'm trying to rotate some simple squares and grouped polygon
> objects around a normal center point.
>
> I can't seem to get it to work. The examples in the documentation
> don't seem to work or make logical sense to me either.
>
> Has anyone done this? It seems like it should be a simple thing to do.
I hadn't done it until now. In fact, I hadn't even noticed
revRotatePoly at all. (So, thanks !!)
I had a play with it, and managed to get it to work; I created a new
stack, created a "freehand polygon", and then in the message box typed
revRotatePoly the name of graphic "Graphic 1", 45
and the polygon did indeed rotate by 45 degrees. However, this produces
the resulting polygon rotated such that it occupies the same location in
screen co-ordinates (i.e. same topLeft as the original polygon).
If you want to rotate a number of polygons around a common centre point,
you'd need to do this, then calculate the appropriately rotated topLeft
point and set that. I thought it would be easier to simply convert the
code to do what we want directly, so I copied the revRotatePoly code
from the Rev scripts, and modified it as below. (It's in button
"revCommon" backscript if you want to see the original code).
NOTE - the docs say that we are welcome to read, use and modify this
code - but that once modified, it is completely unsupported. Sounds
perfectly reasonable to me :-)
Here is a new handler myRotatePoly which takes a graphic (polygon), and
angle and a centre point around which the polygon should be rotated. It
has been (somewhat) tested; if you have any trouble, let me know and
I'll put this simple test stack up on RevOnline .... but it is just a
simple stack with one button and one graphic (freehand polygon).
> on mouseUp
> myRotatePoly the name of graphic "Graphic 1", -45,250,200
> end mouseUp
>
> on myRotatePoly pGraphic, pAngle, basex, basey
> put the topLeft of pGraphic into tTopLeft
> put myPoints(pGraphic, basex, basey) into tPoints -- returns the
> list of points, realtive to the base point
> put the loc of pGraphic into tLoc
> put sin(pAngle * (pi / 180)) into tSinAngle
> put cos(pAngle * (pi / 180)) into tCosAngle
> put (number of items of tPoints) div 2 into tNumItems
> repeat with i = 1 to tNumItems
> put (item (i + (i - 1)) of tPoints) into tCurrentH -- + item 1 of
> the center
> put (item (i + i) of tPoints) into tCurrentV -- + item 2 of the center
> put trunc((tCosAngle * tCurrentH) - (tSinAngle * tCurrentV)) into
> tTempH
> put trunc((tSinAngle * tCurrentH) + (tCosAngle * tCurrentV)) into
> tTempV
> put tTempH + basex into tCurrentH -- and add back the base x and y
> put tTempV + basey into tCurrentV
> put tCurrentH,tCurrentV & comma after tFinalPoints
> end repeat
> delete last char of tFinalPoints
> set the points of pGraphic to tFinalPoints
> -- set the topLeft of pGraphic to tTopLeft
> end myRotatePoly
>
> function myPoints pGraphic, basex, basey
> put the number of lines in the points of pGraphic into tNumPoints
> put empty into tResult
> -- put item 1 of loc of pGraphic into tStartlH
> -- put item 2 of loc of pGraphic into tStartV
> put the points of pGraphic into tPoints
> put basex into tStartlH
> put basey into tStartV
>
> replace cr with comma in tPoints
> if last char of tPoints is comma then delete last char of tPoints
> repeat with i = 1 to tNumPoints
> put (item (i + (i - 1)) of tPoints) - tStartlH into tCurrentH
> put (item (i + i) of tPoints) - tStartV into tCurrentV
> put tCurrentH,tCurrentV & comma after tResult
> end repeat
> delete last char of tResult
> return tResult
> end myPoints
btw - remember that this rotation loses accuracy - because of the trunc
to integer coordinates - so repeatedly transforming the same polygon
will not, in general, get you back to the original polygon. You may need
to keep a pristine copy somewhere, and copy the points / rotate the poly
each time, to prevent the polygon from degrading after multiple rotations.
--
Alex Tweedly http://www.tweedly.net
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.8.4 - Release Date: 27/03/2005
More information about the use-livecode
mailing list