<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>mandarin.no &#187; modulo</title>
	<atom:link href="http://mandarin.no/tag/modulo/feed/" rel="self" type="application/rss+xml" />
	<link>http://mandarin.no</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 09:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Actionscript: Outputting a grid using only one for-loop</title>
		<link>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/</link>
		<comments>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 20:44:09 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[modulo]]></category>
		<category><![CDATA[modulus]]></category>
		<category><![CDATA[row]]></category>
		<guid isPermaLink="false">http://mandarin.no/?p=18</guid>
		<description><![CDATA[Sometime this winter a friend of mine (Knut Urdalen) introduced me to a part of mathematics called modular arithmetic. Now that is a nifty little thing. In modular arithmetic, or modulus if you like, numbers reset themselves when reaching a given limit. Modulus is also known as &#8220;clock arithmetic&#8221;, and the clock is a very [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime this winter a friend of mine (<a href="http://www.urdalen.com/" target="_self">Knut Urdalen</a>) introduced me to a part of mathematics called <a href="http://mathworld.wolfram.com/ModularArithmetic.html" target="_self">modular arithmetic</a>. Now that is a nifty little thing.</p>
<p>In modular arithmetic, or modulus if you like, numbers reset themselves when reaching a given limit. Modulus is also known as &#8220;clock arithmetic&#8221;, and the clock is a very good example for explaining modulus.</p>
<p>A 12 hour clock has a limit of 12, making the hours reset at 12 o&#8217;clock. So instead of becoming 13, it simply jumps back to 1. The same thing happens with a 24 hour clock, but at 24 hours instead of 12. This is as simple as I would&#8217;ve explained it, but <a href="http://mathworld.wolfram.com/ModularArithmetic.html" target="_self">Wolfram</a> has a more in-depth article with links to further reading. I&#8217;m sure <a href="http://www.google.com/search?hl=en&amp;q=+Modular+Arithmetic" target="_self">Google</a> has even more.</p>
<p>In ActionScript you can use modulus like this: x = y % z. Which reads like &#8220;x is y modulus to z&#8221;.</p>
<p>Following the example of the clock we would have to do a modulus of 12 on the total numbers of hours since the beginning of time to get the current time. Why? Ever since we started keeping track of time the numbers of hours have kept increasing. Time never stops. So instead of saying &#8220;oh, the time is 87654826253745 hours&#8221;, we do a modulus of 12 on 87654826253745 and get 7. And by looking out of the window we can tell if it is morning or evening.</p>
<p>Modulus can however not be used to count the number of turns the hour hand has turned at 87654826253745 o&#8217;clock. It only knows when it has reached the limit, not how many times it has turned.</p>
<p>So, as you probably have guessed already modulus is a drop dead simple way of outputting grids. And this is how I would&#8217;ve made a grid of images:</p>
<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> _max:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">20</span>; <span style="color: #808080; font-style: italic;">// Total number of items</span>
<span style="color: #0066CC;">private</span> _rows:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">5</span>; <span style="color: #808080; font-style: italic;">// Items per row</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">_width</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">100</span>;
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">_height</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createGrid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">var</span> ix:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&amp;</span>lt;_max; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// Iterate through all items</span>
        <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">var</span> col:<span style="color: #0066CC;">Number</span> = i <span style="color: #66cc66;">%</span> _rows;
                col==<span style="color: #cc66cc;">0</span> ? ++ix : <span style="color: #0066CC;">void</span>;
                x = ix <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">_width</span>;
                y = col <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">_height</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>
<p>Let&#8217;s look at each of the lines;<br />
01 : define max number of items in the grid<br />
02 : define max number of items in each row<br />
03 &amp; 04 : width and height of each of the items<br />
08 : x position of row &lt;em&gt;n&lt;/em&gt; (increases for each new row)<br />
09 : loop through all items<br />
11 : col is &#8216;current item&#8217; modulus to &#8216;items per row&#8217;<br />
12 : if col has reset, increase ix by one. Here we jump to the next column<br />
13 : place item at x position &#8216;ix * _width&#8217;<br />
14 : place item at y position &#8216;col * _height&#8217;</p>
<p>And that&#8217;s about it. Simple, right?</p>
<p>If anyone of a more mathematical kind of person discovers something wrong, or would&#8217;ve explained modulus in a better way, do arrest me. I&#8217;m still learning.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
