<?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>dev.enekoalonso.com &#187; math</title>
	<atom:link href="http://dev.enekoalonso.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.enekoalonso.com</link>
	<description>having fun with code</description>
	<lastBuildDate>Wed, 12 Oct 2011 21:40:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Russian multiplication in Javascript</title>
		<link>http://dev.enekoalonso.com/2009/08/11/russian-multiplication-in-javascript/</link>
		<comments>http://dev.enekoalonso.com/2009/08/11/russian-multiplication-in-javascript/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 00:43:04 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[multiplication]]></category>
		<category><![CDATA[russian]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=365</guid>
		<description><![CDATA[Answering this post from The Daily WTF about Russian multiplication, here is my code: function russian&#40;a,b&#41; &#123; &#160; return &#40;a&#62;1&#41;? b*&#40;a&#38;1&#41; + russian&#40;a&#62;&#62;1, b&#60;&#60;1&#41; : b; &#125; russian&#40;18,23&#41;; Nice one liner :) Related Posts:December 25, 2008 -- Splitting a vector into two vectors 90 degrees apart (1)December 6, 2008 -- Fibonacci vs. 2n (0)]]></description>
			<content:encoded><![CDATA[<p>Answering this post from <a href="http://thedailywtf.com/Articles/Programming-Praxis-Russian-Peasant-Multiplication.aspx">The Daily WTF about Russian multiplication</a>, here is my code:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> russian<span class="br0">&#40;</span>a,b<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>a<span class="sy0">&gt;</span><span class="nu0">1</span><span class="br0">&#41;</span>? b<span class="sy0">*</span><span class="br0">&#40;</span>a<span class="sy0">&amp;</span><span class="nu0">1</span><span class="br0">&#41;</span> + russian<span class="br0">&#40;</span>a<span class="sy0">&gt;&gt;</span><span class="nu0">1</span>, b<span class="sy0">&lt;&lt;</span><span class="nu0">1</span><span class="br0">&#41;</span> : b;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">russian<span class="br0">&#40;</span><span class="nu0">18</span>,<span class="nu0">23</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Nice one liner :)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>December 25, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/25/splitting-a-vector-into-two-vectors-90-degrees-apart/" title="Splitting a vector into two vectors 90 degrees apart">Splitting a vector into two vectors 90 degrees apart</a> (1)</li><li>December 6, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/06/fibonacci-vs-2n/" title="Fibonacci vs. 2<sup>n</sup>">Fibonacci vs. 2<sup>n</sup></a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2009/08/11/russian-multiplication-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Splitting a vector into two vectors 90 degrees apart</title>
		<link>http://dev.enekoalonso.com/2008/12/25/splitting-a-vector-into-two-vectors-90-degrees-apart/</link>
		<comments>http://dev.enekoalonso.com/2008/12/25/splitting-a-vector-into-two-vectors-90-degrees-apart/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 06:58:39 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[moving]]></category>
		<category><![CDATA[pitagoras]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[trigonometry]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=192</guid>
		<description><![CDATA[I wanted to add something special to the Particle-Balls demo I made. At first, the idea seemed pretty simple: When clicking on a moving ball, split it in two with a 90&#176; angle between them (&#960;/2 in radians). To keep it simple, both balls will have the same size as the original and the same [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to add something special to the <a href="http://dev.enekoalonso.com/research/particle-balls.php">Particle-Balls demo</a> I made. At first, the idea seemed pretty simple: When clicking on a moving ball, split it in two with a 90&deg; angle between them (&pi;/2 in radians). To keep it simple, both balls will have the same size as the original and the same speed.</p>
<p><center><img src="research/media/images/graphs/vector-split.png" alt="Vector split" /></center></p>
<h3>Moving objects</h3>
<p>So far, the way I have always animated objects is by storing a 2D position (x, y) and a 2D speed (horizontal speed and vertical speed). If the vertical speed is zero the object will be moving horizontally. If the vertical speed and horizontal are the same, the object will be moving at 45 degrees (&pi;/4) and so on. It is very easy to update the position on every step of the animation according to the speed:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// Speed is the number of pixels per second.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Interval is the time between calls in milliseconds.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">this</span>.<span class="me1">position</span>.<span class="me1">x</span> += <span class="kw1">this</span>.<span class="me1">speed</span>.<span class="me1">x</span> <span class="sy0">*</span> <span class="br0">&#40;</span>interval<span class="sy0">/</span><span class="nu0">1000</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">this</span>.<span class="me1">position</span>.<span class="me1">y</span> += <span class="kw1">this</span>.<span class="me1">speed</span>.<span class="me1">y</span> <span class="sy0">*</span> <span class="br0">&#40;</span>interval<span class="sy0">/</span><span class="nu0">1000</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>Calculating the moving vector</h3>
<p>When I started writing the code to split the ball in two, soon I realized my math was way deep in my memory where I couldn&#8217;t find it. Yes, trigonometry had to be involved, since I needed to calculate the angle of a moving ball based on the vertical and horizontal velocity. Then I would add 45 degrees (&pi;/4) to the existing ball and create a clone of it, but subtracting 45 degrees (&pi;/4) from the original angle. Easy, uh?</p>
<p>Let&#8217;s see. First we need to get the angle of the vector the object is moving on. This vector is determined by the vertical and horizontal speed, which form a <a href="http://en.wikipedia.org/wiki/Right_triangle#Types_of_triangles">right triangle</a>:</p>
<p><center><img src="http://l.wordpress.com/latex.php?latex=%5Ctheta%3Darctan%28%5Cfrac%7Bopposite%7D%7Badjacent%7D%29&#038;bg=0D324F&#038;fg=FFFFFF&#038;s=1" title="\theta=arctan(\frac{opposite}{adjacent})" style="vertical-align:-20%;" class="tex" alt="\theta=arctan(\frac{opposite}{adjacent})" /></center></p>
<p>Or in Javascript:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> angle = Math.<span class="me1">atan2</span><span class="br0">&#40;</span>ball.<span class="me1">speed</span>.<span class="me1">y</span>, ball.<span class="me1">speed</span>.<span class="me1">x</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<h3>Angle of new vectors</h3>
<p>Once we have the angle, we can calculate the angle of the new vectors:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> V1angle = angle + Math.<span class="me1">PI</span><span class="sy0">/</span><span class="nu0">4</span>; <span class="co1">// Add 90 degrees</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> V2angle = angle &#8211; Math.<span class="me1">PI</span><span class="sy0">/</span><span class="nu0">4</span>; <span class="co1">// Subtract 90 degrees</span></div>
</li>
</ol>
</div>
<h3>2D speeds of new vectors</h3>
<p>Once we have the final angles we can calculate the new horizontal and vertical speeds of the new vectors. But first we need Pitagoras to calculate the actual speed of the original vector:</p>
<p><center><img src="http://l.wordpress.com/latex.php?latex=speed%3D%5Csqrt%7Badjacent%5E2%2Bopposite%5E2%7D&#038;bg=0D324F&#038;fg=FFFFFF&#038;s=1" title="speed=\sqrt{adjacent^2+opposite^2}" style="vertical-align:-20%;" class="tex" alt="speed=\sqrt{adjacent^2+opposite^2}" /></center></p>
<p>In Javascript:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> speed = Math.<span class="me1">sqrt</span><span class="br0">&#40;</span>Math.<span class="me1">pow</span><span class="br0">&#40;</span>ball.<span class="me1">speed</span>.<span class="me1">x</span>,<span class="nu0">2</span><span class="br0">&#41;</span>+Math.<span class="me1">pow</span><span class="br0">&#40;</span>ball.<span class="me1">speed</span>.<span class="me1">y</span>,<span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Given the hypotenuse (speed) and the angle, we can calculate the sides:<br />
<center><img src="http://l.wordpress.com/latex.php?latex=opposite%3Dspeed%2Asin%28%5Ctheta%29&#038;bg=0D324F&#038;fg=FFFFFF&#038;s=1" title="opposite=speed*sin(\theta)" style="vertical-align:-20%;" class="tex" alt="opposite=speed*sin(\theta)" /></center><br />
<center><img src="http://l.wordpress.com/latex.php?latex=adjacent%3Dspeed%2Acos%28%5Ctheta%29&#038;bg=0D324F&#038;fg=FFFFFF&#038;s=1" title="adjacent=speed*cos(\theta)" style="vertical-align:-20%;" class="tex" alt="adjacent=speed*cos(\theta)" /></center></p>
<p>In Javascript:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">ball1.<span class="me1">speed</span>.<span class="me1">x</span> = speed <span class="sy0">*</span> Math.<span class="me1">cos</span><span class="br0">&#40;</span>V1angle<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">ball1.<span class="me1">speed</span>.<span class="me1">y</span> = speed <span class="sy0">*</span> Math.<span class="me1">sin</span><span class="br0">&#40;</span>V1angle<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">ball2.<span class="me1">speed</span>.<span class="me1">x</span> = speed <span class="sy0">*</span> Math.<span class="me1">cos</span><span class="br0">&#40;</span>V2angle<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">ball2.<span class="me1">speed</span>.<span class="me1">y</span> = speed <span class="sy0">*</span> Math.<span class="me1">sin</span><span class="br0">&#40;</span>V2angle<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And that is it! At the end, the math is not that complicated, as you can see. But if you haven&#8217;t used trigonometry in years, it kind of hurts a little bit when you have to figure it out.</p>
<p>PS: Thanks to my German sister Sarah for your help (we figured it out while having dinner)</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 11, 2009 -- <a href="http://dev.enekoalonso.com/2009/08/11/russian-multiplication-in-javascript/" title="Russian multiplication in Javascript">Russian multiplication in Javascript</a> (0)</li><li>March 29, 2009 -- <a href="http://dev.enekoalonso.com/2009/03/29/290/" title="Chrome Experiments">Chrome Experiments</a> (0)</li><li>January 18, 2009 -- <a href="http://dev.enekoalonso.com/2009/01/18/spinning-color-wheel-mootools-raphael-svg/" title="Spinning Color Wheel: Mootools + Raphael + SVG">Spinning Color Wheel: Mootools + Raphael + SVG</a> (7)</li><li>December 26, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/26/forest-demo-was-grass/" title="Forest demo (was Grass)">Forest demo (was Grass)</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/25/splitting-a-vector-into-two-vectors-90-degrees-apart/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fibonacci vs. 2n</title>
		<link>http://dev.enekoalonso.com/2008/12/06/fibonacci-vs-2n/</link>
		<comments>http://dev.enekoalonso.com/2008/12/06/fibonacci-vs-2n/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 17:05:03 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[sequence]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=89</guid>
		<description><![CDATA[Which sequence of numbers grows faster, Fibonacci or 2n? It seems like 2n grows faster. It only takes 1025 steps to overflow a Javascript numeric variable, while Fibonacci needs 1477 steps. Check it out: http://dev.enekoalonso.com/research/threads-fibonacci-vs-2N.php Related Posts:August 11, 2009 -- Russian multiplication in Javascript (0)December 25, 2008 -- Splitting a vector into two vectors 90 [...]]]></description>
			<content:encoded><![CDATA[<p>Which sequence of numbers grows faster, Fibonacci or 2<sup>n</sup>?<br />
It seems like 2<sup>n</sup> grows faster. It only takes 1025 steps to overflow a Javascript numeric variable, while Fibonacci needs 1477 steps.<br />
Check it out:<br />
<a href="http://dev.enekoalonso.com/research/threads-fibonacci-vs-2N.php">http://dev.enekoalonso.com/research/threads-fibonacci-vs-2N.php</a></p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>August 11, 2009 -- <a href="http://dev.enekoalonso.com/2009/08/11/russian-multiplication-in-javascript/" title="Russian multiplication in Javascript">Russian multiplication in Javascript</a> (0)</li><li>December 25, 2008 -- <a href="http://dev.enekoalonso.com/2008/12/25/splitting-a-vector-into-two-vectors-90-degrees-apart/" title="Splitting a vector into two vectors 90 degrees apart">Splitting a vector into two vectors 90 degrees apart</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2008/12/06/fibonacci-vs-2n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

