{"id":89,"date":"2007-07-10T07:45:58","date_gmt":"2007-07-10T12:45:58","guid":{"rendered":"http:\/\/www.pixelwit.com\/blog\/2007\/07\/10\/draw-an-arc-with-actionscript\/"},"modified":"2009-08-13T22:33:11","modified_gmt":"2009-08-14T03:33:11","slug":"draw-an-arc-with-actionscript","status":"publish","type":"post","link":"https:\/\/www.pixelwit.com\/blog\/2007\/07\/10\/draw-an-arc-with-actionscript\/","title":{"rendered":"Draw an Arc with ActionScript"},"content":{"rendered":"<p>Drawing an arc (a segment of a circle&#8217;s circumference) is very similar to <a title=\"Basic Circle Drawing ActionScript\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/06\/basic-circle-drawing-actionscript\/\">drawing a complete circle<\/a>.  Just like drawing a circle, drawing an arc requires you to specify the center, the radius, and the number of steps to take from the first point to the last.  But drawing an arc also requires additional information such as the angle of the first point on the arc, the direction (clockwise or counter-clockwise) to travel around the circle&#8217;s circumference, and the angle of the last point on the arc.<!--more--><\/p>\n<p>So the arguments in our &#8220;drawArc&#8221; function might look something like this:<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">function<\/span> drawArc <span class=\"br0\">&#40;<\/span>centerX, centerY, radius, startAngle, endAngle, direction, steps<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>That isn&#8217;t too bad, but it could be better.  The &#8220;endAngle&#8221; and the &#8220;direction&#8221; arguments could be combined into one &#8220;arcAngle&#8221; argument.  That way we can say, &#8220;Start at an angle of 45 degrees then sweep counter-clockwise 90 degrees&#8221; rather than, &#8220;Start at an angle of 45 degrees then go counter-clockwise until you get to 315 degrees.&#8221;  Our modified arguments would look like this:<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">function<\/span> drawArc <span class=\"br0\">&#40;<\/span>centerX, centerY, radius, startAngle, arcAngle, steps<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>If you read my <a title=\"How to Draw a Circle with ActionScript\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/06\/basic-circle-drawing-actionscript\/\">previous article<\/a> you probably understand that drawing a circle involves moving through a series of points and that each point is described by its angle from center.  The angle can be described using degrees, radians or a ratio.  I prefer using a ratio from 0 to 1 as it&#8217;s simple and easily converted to any other unit of measurement such as degrees or radians.<\/p>\n<blockquote><p><strong>Example:<\/strong> A quarter of a full rotation can be represented in any number of ways: 90 degrees, 25%, 1\/2 Math.PI radians, or as .25 when expressed as a ratio from 0 to 1.  Using a ratio has the benefit of abstracting your data and removing &#8220;arbitrary&#8221; numbers such as 360, 100, 2*Math.PI, 2 to the 64th power, etc.  To convert from a ratio to any other unit of measure, simply multiply the ratio times the number of units in one full rotation.<\/p><\/blockquote>\n<p>Now that we have our arguments all set up and know how they&#8217;ll be formatted, it&#8217;s time to move on to writing the actual guts of the code which will be responsible for drawing the arc.  Using the knowledge gained in the previous <a title=\"Basic ActionScript Circle Drawing Tutorial\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/06\/basic-circle-drawing-actionscript\/\">circle article<\/a> you should understand how to plot any point on a circle&#8217;s circumference as long as you know its angle from center.  Since we can draw any point at a given angle and we know the arguments we&#8217;ll be using, writing the &#8220;drawArc&#8221; function is as easy as determining the angle of each point on the arc and then plotting its position.<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:100%;height:700px;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Angles are expressed as a number between 0 and 1. &nbsp;.25 = 90 degrees.<\/span><br \/>\n<span class=\"co1\">\/\/ If you prefer using degrees, write 90 degrees like so &quot;90\/360&quot;.<\/span><br \/>\n<span class=\"kw2\">function<\/span> drawArc<span class=\"br0\">&#40;<\/span>centerX, centerY, radius, startAngle, arcAngle, steps<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ For convenience, store the number of radians in a full circle.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> twoPI = <span class=\"nu0\">2<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">PI<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ To determine the size of the angle between each point on the<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ arc, divide the overall angle by the total number of points.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> angleStep = arcAngle<span class=\"sy0\">\/<\/span>steps;<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Determine coordinates of first point using basic circle math.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> xx = centerX + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">cos<\/span><span class=\"br0\">&#40;<\/span>startAngle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> yy = centerY + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">sin<\/span><span class=\"br0\">&#40;<\/span>startAngle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Move to the first point.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">moveTo<\/span><span class=\"br0\">&#40;<\/span>xx, yy<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Draw a line to each point on the arc.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i=<span class=\"nu0\">1<\/span>; i<span class=\"sy0\">&lt;<\/span>=steps; i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Increment the angle by &quot;angleStep&quot;.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> angle = startAngle + i <span class=\"sy0\">*<\/span> angleStep;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Determine next point's coordinates using basic circle math.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; xx = centerX + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">cos<\/span><span class=\"br0\">&#40;<\/span>angle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; yy = centerY + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">sin<\/span><span class=\"br0\">&#40;<\/span>angle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Draw a line to the next point.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>xx, yy<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Set a line style so we can see what we are drawing.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span>, 0xFF0000<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Draw an arc with a center of (250, 250) and a radius of 200<\/span><br \/>\n<span class=\"co1\">\/\/ that starts at an angle of 45 degrees then rotates counter-<\/span><br \/>\n<span class=\"co1\">\/\/ clockwise 90 degrees. &nbsp;We'll span the arc with 20 evenly spaced points.<\/span><br \/>\ndrawArc<span class=\"br0\">&#40;<\/span><span class=\"nu0\">250<\/span>, <span class=\"nu0\">250<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">45<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">360<\/span>, -<span class=\"nu0\">90<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">360<\/span>, <span class=\"nu0\">20<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>The above code produces the following SWF file:<\/p>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_drawarc_01_797539477\"\n\t\t\tclass=\"flashmovie\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"500\">\n\t<param name=\"movie\" value=\"\/blog\/wp-content\/uploads\/2007\/07\/drawarc_01.swf\" \/>\n\t<!--[if !IE]>-->\n\t<object\ttype=\"application\/x-shockwave-flash\"\n\t\t\tdata=\"\/blog\/wp-content\/uploads\/2007\/07\/drawarc_01.swf\"\n\t\t\tname=\"fm_drawarc_01_797539477\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"500\">\n\t<!--<![endif]-->\n\t\t\n\t<!--[if !IE]>-->\n\t<\/object>\n\t<!--<![endif]-->\n<\/object>\n<p>That may or may not be what you were expecting to see.  The thing to note about the above SWF is that the point with no angle of rotation (0 degrees) is at the 3 o'clock position.  Moving the the angle to better suit your needs is a very simple matter.  If you want the point with no rotation to be at 12 o'clock you subtract .25 (a quarter of a turn) from your start angle like so:<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"actionscript codecolorer\"><span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">function<\/span> drawArc<span class=\"br0\">&#40;<\/span>centerX, centerY, radius, startAngle, arcAngle, steps<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Rotate the point of 0 rotation 1\/4 turn counter-clockwise.<\/span><br \/>\n&nbsp; &nbsp; startAngle -= .25;<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> twoPI = <span class=\"nu0\">2<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">PI<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> angleStep = arcAngle<span class=\"sy0\">\/<\/span>steps;<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> xx = centerX + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">cos<\/span><span class=\"br0\">&#40;<\/span>startAngle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; <span class=\"kw2\">var<\/span> yy = centerY + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">sin<\/span><span class=\"br0\">&#40;<\/span>startAngle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">moveTo<\/span><span class=\"br0\">&#40;<\/span>xx, yy<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">var<\/span> i=<span class=\"nu0\">1<\/span>; i<span class=\"sy0\">&lt;<\/span>=steps; i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> angle = startAngle + i <span class=\"sy0\">*<\/span> angleStep;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; xx = centerX + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">cos<\/span><span class=\"br0\">&#40;<\/span>angle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; yy = centerY + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">sin<\/span><span class=\"br0\">&#40;<\/span>angle <span class=\"sy0\">*<\/span> twoPI<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> radius;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>xx, yy<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span>, 0xFF0000<span class=\"br0\">&#41;<\/span>;<br \/>\ndrawArc<span class=\"br0\">&#40;<\/span><span class=\"nu0\">250<\/span>, <span class=\"nu0\">250<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">45<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">360<\/span>, -<span class=\"nu0\">90<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">360<\/span>, <span class=\"nu0\">20<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>The above code produces the following SWF:<\/p>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_drawarc_02_1963405774\"\n\t\t\tclass=\"flashmovie\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"500\">\n\t<param name=\"movie\" value=\"\/blog\/wp-content\/uploads\/2007\/07\/drawarc_02.swf\" \/>\n\t<!--[if !IE]>-->\n\t<object\ttype=\"application\/x-shockwave-flash\"\n\t\t\tdata=\"\/blog\/wp-content\/uploads\/2007\/07\/drawarc_02.swf\"\n\t\t\tname=\"fm_drawarc_02_1963405774\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"500\">\n\t<!--<![endif]-->\n\t\t\n\t<!--[if !IE]>-->\n\t<\/object>\n\t<!--<![endif]-->\n<\/object>\n<p>So there you have it.  All the code needed to draw an arc or plot the points along a segment of a circle's circumference.  If you're looking for an easy challenge, try modifying the code so it draws a wedge (like a slice of pie) rather than an arc.<\/p>\n<p><strong>Update<\/strong>: The next tutorial in this series is <a title=\"Swing Back and Forth Along an Arc Path.\" href=\"https:\/\/www.pixelwit.com\/blog\/2008\/01\/swing-pendulum-arc\/\">How to Swing a Pendulum Arc<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An in-depth ActionScript tutorial explaining the code and concepts needed to draw an arc or segment of a circle.  Handy for making anything from pie charts to motion paths.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[8],"tags":[23,45,36,37],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-flash","tag-actionscript","tag-draw","tag-swfs","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}