<?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>lab.joelGillman.com &#187; normalize</title>
	<atom:link href="http://lab.joelgillman.com/archives/tag/normalize/feed" rel="self" type="application/rss+xml" />
	<link>http://lab.joelgillman.com</link>
	<description>Sketches, Code Bites and Toys</description>
	<lastBuildDate>Tue, 12 Jul 2011 15:03:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Map Function</title>
		<link>http://lab.joelgillman.com/archives/87_map-function</link>
		<comments>http://lab.joelgillman.com/archives/87_map-function#comments</comments>
		<pubDate>Mon, 29 Sep 2008 22:39:41 +0000</pubDate>
		<dc:creator>joel gillman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[normalize]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://lab.joelgillman.com/?p=87</guid>
		<description><![CDATA[If you&#8217;ve ever used Processing you&#8217;ve probably used the &#8216;map&#8217; function. From the Processing Reference section it reads: &#8220;Re-maps a number from one range to another.&#8221; Unfortunately the function doesn&#8217;t exist in Flash, so I rewrote it. Why is this function useful? Lets say your mouse can move within the bounds of your stage from [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used <a href="http://www.processing.org/">Processing</a> you&#8217;ve probably used the &#8216;map&#8217; function.  From the Processing Reference section it <a href="http://processing.org/reference/map_.html">reads</a>: &#8220;Re-maps a number from one range to another.&#8221;</p>
<p><span id="more-87"></span>Unfortunately the function doesn&#8217;t exist in Flash, so I rewrote it.  Why is this function useful?  Lets say your mouse can move within the bounds of your stage from 0 to 550 pixels along its x-axis.  You want a box to move back and forth from 100 to 200 pixels according to the position of the mouse.  So when your mouse is halfway across the stage your box should be halfway between 100 and 200 pixels.  You would simply use:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">myBox<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>Easy as that!</p>
<p>Usage:<br />
map(value, low1, high1, low2, high2);<br />
<em>low2 and high2 default to 0 and 1, respectively so you don&#8217;t have to put them in if you just want to normalize your value.</em></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;low1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;high1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;low2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;high2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//if the value and the 1st range low are equal to</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// the new value must be low2</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> == low1<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> low2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//normalize both sets to a 0-? range</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> range1 = high1 <span style="color: #000066; font-weight: bold;">-</span> low1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> range2 = high2 <span style="color: #000066; font-weight: bold;">-</span> low2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//normalize the value to the new normalized range</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> result = <span style="color: #004993;">value</span> <span style="color: #000066; font-weight: bold;">-</span> low1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//define the range as a percentage (0.0 to 1.0)</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> ratio = result <span style="color: #000066; font-weight: bold;">/</span> range1<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//find the value in the new normalized-range</span><br />
&nbsp; &nbsp; result = ratio <span style="color: #000066; font-weight: bold;">*</span> range2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//un-normalize the value in the new range</span><br />
&nbsp; &nbsp; result <span style="color: #000066; font-weight: bold;">+</span>= low2<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> result<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>And super compressed (less readable, less space, works the same):</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span>v<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> a<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>v == a<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000;">&#40;</span>v <span style="color: #000066; font-weight: bold;">-</span> a<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span> <span style="color: #000066; font-weight: bold;">-</span> a<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://lab.joelgillman.com/archives/87_map-function/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

