{"id":268,"date":"2008-05-14T22:31:56","date_gmt":"2008-05-15T03:31:56","guid":{"rendered":"http:\/\/www.pixelwit.com\/blog\/?p=268"},"modified":"2009-08-14T19:37:35","modified_gmt":"2009-08-15T00:37:35","slug":"how-to-draw-logarithmic-spiral","status":"publish","type":"post","link":"https:\/\/www.pixelwit.com\/blog\/2008\/05\/14\/how-to-draw-logarithmic-spiral\/","title":{"rendered":"How to Draw a Logarithmic Spiral"},"content":{"rendered":"<p>What is a Logarithmic spiral?  A logarithmic spiral steps <em>around<\/em> center at an additive rate while stepping <em>away<\/em> from center at a <strong>multiplicative<\/strong> rate.  Increasing the distance from center at a multiplicative rate causes the arm of a logarithmic spiral to increase in size as it gets further from center.  To illustrate, take a look at this photo of a spiral staircase:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/farm3.static.flickr.com\/2271\/2020975929_afb848ce5b_m.jpg\" alt=\"bottom-up view\" border=\"0\" \/><br \/><small>Photo by: <a href=\"http:\/\/www.flickr.com\/photos\/78771543@N00\/2020975929\/\" title=\"darijus\" target=\"_blank\">darijus<\/a><\/small><\/p>\n<p>As you can see in the above photo, the number of steps<!--more--> going around each floor stays the same on every level but perspective makes the stairs on the outside of the spiral look as though they&#8217;re getting bigger and bigger.  The increased size of the stairs as they get further from center is what differentiates a logarithmic spiral form a simple <a title=\"Photo of Archimedean Spiral\" href=\"https:\/\/www.pixelwit.com\/blog\/wp-content\/uploads\/2008\/05\/simple-archimedean-spiral.jpg\">Archimedean spiral<\/a>.<\/p>\n<p>The process for drawing a logarithmic spiral is pretty straightforward, you only need two numbers.  One to specify how far <em>around<\/em> center you must go every step, and another number to specify how far <em>away<\/em> from center you must go every step.  For example&#8217;s sake we&#8217;ll rotate <em>around<\/em> 10 degrees every step and multiply our distance <em>away<\/em> from center by 2 every step.  If we start at 10 degrees rotation around center and 2 pixels away from center, our second point would be 20 degrees around center and 4 pixels away from center, our third point would be 30 degrees around and 8 pixels away, our fourth point would be 40 degrees around and 16 pixels away, and so on.<\/p>\n<p>Now that you understand what a logarithmic spiral is we can finally start learning how to draw one.  Drawing a logarithmic spiral is very similar to drawing a simple spiral so I strongly suggest reading my <a title=\"How to Draw a Simple Spiral\" href=\"https:\/\/www.pixelwit.com\/blog\/2008\/04\/how-to-draw-a-spiral\/\">Simple Spiral Tutorial<\/a> to get acquainted with some basic spiral drawing concepts.<\/p>\n<p>Now let&#8217;s take a look at the ActionScript code used to draw a logarithmic spiral:<\/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> logSpiral<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 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 = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span>radius, i<span class=\"sy0\">\/<\/span>sides<span class=\"br0\">&#41;<\/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 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=\"kw3\">lineStyle<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span>, 0x000000<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ Black.<\/span><br \/>\n<span class=\"co1\">\/\/ &nbsp; &nbsp; &nbsp; (centerX, centerY, radius, sides, coils, rotation)<\/span><br \/>\nlogSpiral<span class=\"br0\">&#40;<\/span> &nbsp; &nbsp;<span class=\"nu0\">250<\/span>, &nbsp; &nbsp; <span class=\"nu0\">200<\/span>, &nbsp; &nbsp;<span class=\"nu0\">320<\/span>, &nbsp; <span class=\"nu0\">700<\/span>, &nbsp; &nbsp;<span class=\"nu0\">14<\/span>, &nbsp; &nbsp; &nbsp; &nbsp;<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\">2<\/span>, 0xD0D0D0<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ Gray.<\/span><br \/>\nlogSpiral<span class=\"br0\">&#40;<\/span> &nbsp; &nbsp;<span class=\"nu0\">250<\/span>, &nbsp; &nbsp; <span class=\"nu0\">200<\/span>, &nbsp; &nbsp;<span class=\"nu0\">320<\/span>, &nbsp; <span class=\"nu0\">700<\/span>, &nbsp; &nbsp;<span class=\"nu0\">14<\/span>, &nbsp; &nbsp; &nbsp; .5<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p \/>\n\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n\t\t\tid=\"fm_trigspiral_02_2117038709\"\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\/05\/trigspiral_02.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\/05\/trigspiral_02.swf\"\n\t\t\tname=\"fm_trigspiral_02_2117038709\"\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><\/p>\n<p>The most important part of the above code is the line where we determine how far away from center we should be.  It's this part right here:<\/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=\"co1\">\/\/ How far away from center<\/span><br \/>\n<span class=\"kw2\">var<\/span> away = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span>radius, i<span class=\"sy0\">\/<\/span>sides<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>In the <a title=\"How to Draw a Simple Spiral\" href=\"https:\/\/www.pixelwit.com\/blog\/2008\/04\/how-to-draw-a-spiral\/\">Simple Spiral Tutorial<\/a> we determined the distance from the center 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=\"co1\">\/\/ How far away from center<\/span><br \/>\n<span class=\"kw2\">var<\/span> away = radius <span class=\"sy0\">*<\/span> i<span class=\"sy0\">\/<\/span>sides;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>In the simple spiral we were essentially dividing the radius into equally sized segments, one segment for every step taken around center.  For example, if we wanted to draw a simple spiral with 20 sides and a 100 pixel radius, we would step 5 pixels away from center every time we drew a side (100\/20=5).  In other words we determined what number would equal 100 when <strong>added<\/strong> to itself 20 times.  <strong>But<\/strong>, when drawing a logarithmic spiral we must determine what number would equal 100 when <strong>multiplied<\/strong> by itself 20 times.  To do this we need to determine the 20th root of 100 (1.25892541179417).<\/p>\n<p>Determining the Nth root of a number is as simple as raising the number to its inverse power.  That may sound complicated, but it's not really.  Let's start off with something really easy like raising a number to a power. <\/p>\n<p>How do you raise a number to a certain power? For example, how do you raise 2 to the 3rd power?  In Flash all you have to do is 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=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">var<\/span> twoCubed = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2<\/span>, <span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Two cubed = &quot;<\/span>+twoCubed<span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ prints &quot;Two cubed = 8&quot;.<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>That's pretty easy, but the tricky part is getting Flash to work in the opposite direction and determine the 3rd root of 8, or in other words, what number multiplied by itself 3 times will equal 8.  To do that we need to tell Flash to do the following:<\/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=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">var<\/span> cubeRootOfEight = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">8<\/span>, <span class=\"nu0\">1<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Cube root of eight = &quot;<\/span>+cubeRootOfEight <span class=\"br0\">&#41;<\/span>;<span class=\"co1\">\/\/ prints &quot;Cube root of eight = 2&quot;.<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>Getting the root of a number is a simple matter of <em>inverting<\/em> your \"power\" argument.  The inverse of 3 (or 3\/1) is 1\/3.  Pretty slick right?  But wait, you ain't seen nothing yet!  The Math.pow function also provides a really cool way to get the square of the cubed root of 8, that is to say, (the cubed root of 8&#41; * (the cubed root of 8&#41;.  Check this out:<\/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=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">var<\/span> squaredCubeRootOfEight = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">8<\/span>, <span class=\"nu0\">2<\/span><span class=\"sy0\">\/<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Squared cube root of eight = &quot;<\/span>+squaredCubeRootOfEight <span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/ prints &quot;Squared cube root of eight = 4&quot;.<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>To square the result from Math.pow(8, 1\/3) we simply change the numerator of the fraction to the power of our desired result like so, Math.pow(8, 2\/3).  Pretty neat aye?<\/p>\n<p>So when you see 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=\"co1\">\/\/<\/span><br \/>\n<span class=\"kw2\">var<\/span> N = sides;<br \/>\n<span class=\"kw2\">var<\/span> away = <span class=\"kw3\">Math<\/span>.<span class=\"kw3\">pow<\/span><span class=\"br0\">&#40;<\/span>radius, i<span class=\"sy0\">\/<\/span>N<span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"co1\">\/\/<\/span><br \/>\n<span class=\"co1\">\/\/<\/span><\/div><\/div>\n<p>...it's really saying, \"Get me the <em><strong>N<\/strong><\/em>th root of <em><strong>radius<\/strong><\/em> and then raise it to the <em><strong>i<\/strong><\/em> power.\"  Placing the above line of code inside a For loop that iterates \"Sides\" times will essentially keep multiplying the initial \"away\" value (the Nth root of radius) times itself until it equals the \"Radius\".<\/p>\n<p>That's a lot of information to digest and I probably didn't present it all in the clearest possible fashion so don't be afraid to ask questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flash ActionScript tutorial explaining how to draw a logarithmic which is a spiral that steps around center at an additive rate while stepping away from center at a multiplicative 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-268","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\/268","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=268"}],"version-history":[{"count":0,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixelwit.com\/blog\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}