Article View: adobe.scripting.indesign
Article #16946[AS] Drawing n-sided polygons
From: Olav Kvern
Date: Tue, 22 Jul 2003 22:51
Date: Tue, 22 Jul 2003 22:51
105 lines
3928 bytes
3928 bytes
Gang--
Some time ago, I promised John Darnell that I'd post a method for drawing n-sided regular polygons (using geometric bounds to define the size of a polygon often distorts the shape of the polygon). Here it is:
--DrawPolygon.scpt
--Draw an equilateral polygon from a given center point.
--This first part is just an example use of the handler
tell application "InDesign 2.0.2"
set myDocument to make document
set horizontal measurement units of view preferences of myDocument to points
set vertical measurement units of view preferences of myDocument to points
set myPageCenterX to (page width of document preferences of myDocument) / 2
set myPageCenterY to (page height of document preferences of myDocument) / 2
tell page 1 of myDocument
set myPolygon to make polygon
set myPathPoints to my myCalculatePolygon(myPageCenterX, myPageCenterY, 128, 12, true, 0.5)
set entire path of path 1 of myPolygon to myPathPoints
end tell
end tell
on myCalculatePolygon(myXCenter, myYCenter, myRadius, myNumberOfSides, myStarPolygon, myInsetPercentage)
set myPathPoints to {}
if myStarPolygon = true then
set myNumberOfSides to myNumberOfSides * 2
end if
set myAngleIncrement to 360 / myNumberOfSides
repeat with myPointCounter from 1 to myNumberOfSides
if myPointCounter mod 2 = 0 or myStarPolygon = false then
set myX to myXCenter + (myRadius * (my cosine_of(myAngleIncrement * myPointCounter)))
set myY to myYCenter + (myRadius * (my sine_of(myAngleIncrement * myPointCounter)))
else
set myX to myXCenter + ((myRadius * myInsetPercentage) * (my cosine_of(myAngleIncrement * myPointCounter)))
set myY to myYCenter + ((myRadius * myInsetPercentage) * (sine_of(myAngleIncrement * myPointCounter)))
end if
copy {myX, myY} to end of myPathPoints
end repeat
return myPathPoints
end myCalculatePolygon
--Trigonometric routines from Apple
--(replace with a scripting addition, if possible,
--to speed things up)
on sine_of(x)
repeat until x ? 0 and x < 360
if x ? 360 then
set x to x - 360
end if
if x < 0 then
set x to x + 360
end if
end repeat
--convert from degrees to radians
set x to x * (2 * pi) / 360
set answer to 0
set numerator to x
set denominator to 1
set factor to -(x ^ 2)
repeat with i from 3 to 40 by 2
set answer to answer + numerator / denominator
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat
return answer
end sine_of
on cosine_of(x)
repeat until x ? 0 and x < 360
if x ? 360 then
set x to x - 360
end if
if x < 0 then
set x to x + 360
end if
end repeat
--convert from degrees to radians
set x to x * (2 * pi) / 360
set answer to 0
set numerator to 1
set denominator to 1
set factor to -(x ^ 2)
repeat with i from 2 to 40 by 2
set answer to answer + numerator / denominator
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat
return answer
end cosine_of
Thanks,
Ole
Technical Details
Message-ID:
<1dea0681.-1@WebX.la2eafNXanI>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!216.170.153.135.MISMATCH!tdsnet-transit!newspeer.tds.net!205.215.62.125.MISMATCH!in.100proofnews.com!in.100proofnews.com!nntp.infostrada.it!newsfeed01.sul.t-online.de!t-online.de!WebXShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f939a,5730a16972759f58,start X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2003-07-22 16:07:52 PST From: "Olav Kvern" <okvern@adobe.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!216.170.153.135.MISMATCH!tdsnet-transit!newspeer.tds.net!205.215.62.125.MISMATCH!in.100proofnews.com!in.100proofnews.com!nntp.infostrada.it!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.scripting.indesign Subject: [AS] Drawing n-sided polygons Message-ID: <1dea0681.-1@WebX.la2eafNXanI> Date: Tue, 22 Jul 2003 15:51:27 -0700 Lines: 113 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit