{"id":256,"date":"2008-04-08T05:04:51","date_gmt":"2008-04-08T10:04:51","guid":{"rendered":"http:\/\/www.pixelwit.com\/blog\/2008\/04\/08\/how-to-draw-a-spiral\/"},"modified":"2009-08-14T19:30:00","modified_gmt":"2009-08-15T00:30:00","slug":"how-to-draw-a-spiral","status":"publish","type":"post","link":"https:\/\/www.pixelwit.com\/blog\/2008\/04\/08\/how-to-draw-a-spiral\/","title":{"rendered":"How to Draw a Spiral"},"content":{"rendered":"<p>This tutorial covers the basic concepts needed to draw a simple spiral with ActionScript.  A &#8220;simple spiral&#8221; or an &#8220;Archimedean spiral&#8221; is created by moving &#8216;<em>away<\/em> from&#8217; and &#8216;<em>around<\/em>&#8216; a central point at a constant additive rate.  If that sounds a bit foreign to you, don&#8217;t worry it&#8217;s nothing new.  If you&#8217;ve coiled a hose or rope on a flat surface, you have experience working with Archimedean spirals.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/05\/simple-archimedean-spiral.jpg\" alt=\"Archimedean Spiral\" title=\"simple-archimedean-spiral\" width=\"206\" height=\"220\" \/><br \/>(Photo by <a href=\"http:\/\/flickr.com\/photos\/pneedham\/590649519\/sizes\/s\/\">Paul Needham<\/a>)<\/p>\n<p>The most notable characteristic of the simple spiral above is that each coil (a full rotation around center) will always increase the spiral&#8217;s radius by the same amount (the width of the rope).<\/p>\n<p>Now that you understand what a simple spiral is, I&#8217;d like to jump ahead a bit and show you the final state of our code:<!--more--><\/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\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ centerX-- X origin of the spiral.<\/span><br \/>\n<span class=\"co1\">\/\/ centerY-- Y origin of the spiral.<\/span><br \/>\n<span class=\"co1\">\/\/ radius--- Distance from origin to outer arm.<\/span><br \/>\n<span class=\"co1\">\/\/ sides---- Number of points or sides along the spiral's arm.<\/span><br \/>\n<span class=\"co1\">\/\/ coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)<\/span><br \/>\n<span class=\"co1\">\/\/ rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180\/360'=180 degrees)<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">function<\/span> spiral<span class=\"br0\">&#40;<\/span>centerX, centerY, radius, sides, coils, rotation<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">with<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">this<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><span class=\"co1\">\/\/ Draw within the clip calling the function.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Start at the center.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">moveTo<\/span><span class=\"br0\">&#40;<\/span>centerX, centerY<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ How far to step away from center for each side.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> awayStep = radius<span class=\"sy0\">\/<\/span>sides;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ How far to rotate around center for each side. <\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> aroundStep = coils<span class=\"sy0\">\/<\/span>sides;<span class=\"co1\">\/\/ 0 to 1 based.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Convert aroundStep to radians.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> aroundRadians = aroundStep <span class=\"sy0\">*<\/span> <span class=\"nu0\">2<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">PI<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Convert rotation to radians.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; rotation <span class=\"sy0\">*<\/span>= <span class=\"nu0\">2<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">PI<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ For every side, step around and away from center.<\/span><br \/>\n&nbsp; &nbsp; &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>=sides; i++<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ How far away from center<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> away = i <span class=\"sy0\">*<\/span> awayStep;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ How far around the center.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> around = i <span class=\"sy0\">*<\/span> aroundRadians + rotation;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Convert 'around' and 'away' to X and Y.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> x = centerX + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">cos<\/span><span class=\"br0\">&#40;<\/span>around<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> away;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">var<\/span> y = centerY + <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">sin<\/span><span class=\"br0\">&#40;<\/span>around<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> away;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ Now that you know it, do it.<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">lineTo<\/span><span class=\"br0\">&#40;<\/span>x, y<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/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\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ spiral(centerX, centerY, radius, sides, coils, rotation).<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Big center spirals.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">27<\/span>, 0x0000FF<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ Blue.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">250<\/span>, <span class=\"nu0\">210<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">4<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">27<\/span>, 0xFF0000<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ Red.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">250<\/span>, <span class=\"nu0\">210<\/span>, <span class=\"nu0\">127<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">2.5<\/span>, .5<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/ Small corner spirals.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">4<\/span>, 0xFF00FF<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Magenta.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">50<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">6<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; Big.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">125<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">25<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">2<\/span>, .5<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; Small.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">4<\/span>, 0x00FFFF<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cyan.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">450<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">200<\/span>, -<span class=\"nu0\">6<\/span>, .5<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp;Big.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">375<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">25<\/span>, <span class=\"nu0\">200<\/span>, -<span class=\"nu0\">2<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; Small.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">4<\/span>, 0xFFFF00<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Yellow.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">50<\/span>, <span class=\"nu0\">350<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">200<\/span>, -<span class=\"nu0\">4<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; Big.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">125<\/span>, <span class=\"nu0\">350<\/span>, <span class=\"nu0\">25<\/span>, <span class=\"nu0\">200<\/span>, -<span class=\"nu0\">3<\/span>, .5<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; Small.<\/span><br \/>\n<span class=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">4<\/span>, 0x00FF00<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Green.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">450<\/span>, <span class=\"nu0\">350<\/span>, <span class=\"nu0\">50<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">4<\/span>, .5<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp;Big.<\/span><br \/>\nspiral<span class=\"br0\">&#40;<\/span><span class=\"nu0\">375<\/span>, <span class=\"nu0\">350<\/span>, <span class=\"nu0\">25<\/span>, <span class=\"nu0\">200<\/span>, <span class=\"nu0\">3<\/span>, <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ &nbsp; &nbsp; Small.<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>As you can see, the &#8220;spiral&#8221; function accepts arguments for &#8220;centerX&#8221;, &#8220;centerY&#8221;, &#8220;radius&#8221;, &#8220;sides&#8221;, &#8220;coils&#8221;, and &#8220;rotation&#8221;.  The only arguments you might have trouble fully understanding are &#8220;sides&#8221;, &#8220;coils&#8221; and &#8220;rotation&#8221;.<\/p>\n<ul>\n<li><strong>Sides<\/strong>: If you&#8217;ve read my previous <a title=\"How to Draw a Circle with ActionScript\" href=\"https:\/\/www.pixelwit.com\/blog\/2007\/06\/basic-circle-drawing-actionscript\/\">Circle Drawing Tutorial<\/a> you understand that Flash approximates circles and arcs by connecting a series of straight lines.  The &#8220;sides&#8221; argument determines how many straight lines will be used to approximate our spiral.<\/li>\n<li><strong>Rotation<\/strong>: The &#8220;rotation&#8221; argument controls where the outer arm of the spiral will be in relation to center.  To avoid arbitrary units of measure, the rotation argument accepts values between 0 and 1 (zero and one) where 0 = no rotation and 1 = 360 degrees of rotation.<\/li>\n<li><strong>Coils<\/strong>: The &#8220;coils&#8221; or windings of the spiral determine the amount of times the spiral will wrap around center.  &#8220;Coils&#8221; can be any real number, positive numbers produce clockwise spirals and negative numbers produce counter-clockwise spirals.<\/li>\n<\/ul>\n<p>Once you&#8217;re familiar with the arguments of the spiral function, it&#8217;s a little easier to visualize what&#8217;s happening in the rest of the code.<\/p>\n<p>In my mind I picture a straight line the length of the spiral&#8217;s radius.  I place one end of the line at the center point of the spiral and rotate the line until it is at the desired &#8220;rotation&#8221;.  I then divide the the line into segments of equal length (awayStep) with one segment for every side of the spiral.  From that point I start rotating all of the slices except the center one a set amount (aroundStep).  I exclude the center-most segment and rotate all of the outer segments the same amount again.  I repeat this process until I have excluded all segments from the center out, connecting each line segment&#8217;s end to the previous segment&#8217;s beginning.<\/p>\n<p>Here&#8217;s an animation to illustrate the concept:<\/p>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_spiral-illustration_01_488927434\"\n\t\t\tclass=\"flashmovie\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"400\">\n\t<param name=\"movie\" value=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/04\/spiral-illustration_01.swf\" \/>\n\t<!--[if !IE]>-->\n\t<object\ttype=\"application\/x-shockwave-flash\"\n\t\t\tdata=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/04\/spiral-illustration_01.swf\"\n\t\t\tname=\"fm_spiral-illustration_01_488927434\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"400\">\n\t<!--<![endif]-->\n\t\t\n\t<!--[if !IE]>-->\n\t<\/object>\n\t<!--<![endif]-->\n<\/object>\n<p>Hopefully that's enough information to get you started using ActionScript to draw some spirals of your own.  Have fun.<\/p>\n<p><\/p>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_spiral-tutorial_01_711092797\"\n\t\t\tclass=\"flashmovie\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"400\">\n\t<param name=\"movie\" value=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/04\/spiral-tutorial_01.swf\" \/>\n\t<!--[if !IE]>-->\n\t<object\ttype=\"application\/x-shockwave-flash\"\n\t\t\tdata=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/04\/spiral-tutorial_01.swf\"\n\t\t\tname=\"fm_spiral-tutorial_01_711092797\"\n\t\t\twidth=\"500\"\n\t\t\theight=\"400\">\n\t<!--<![endif]-->\n\t\t\n\t<!--[if !IE]>-->\n\t<\/object>\n\t<!--<![endif]-->\n<\/object>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial covers the basic concepts needed to draw a simple Archimedean  spiral with ActionScript.  &#8220;Archimedean&#8221; spirals are created by moving &#8216;away from&#8217; and &#8216;around&#8217; a central point at a constant additive rate.<\/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":[42,45,36,37],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-flash","tag-code","tag-draw","tag-swfs","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/256","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=256"}],"version-history":[{"count":0,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}