<?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>scriptNode &#187; math</title>
	<atom:link href="http://scriptnode.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bresenham&#8217;s Line Algorithm In JavaScript</title>
		<link>http://scriptnode.com/article/bresenhams-line-algorithm-in-javascript/</link>
		<comments>http://scriptnode.com/article/bresenhams-line-algorithm-in-javascript/#comments</comments>
		<pubDate>Sun, 06 Jul 2008 22:03:40 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[just-for-fun]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[script-sunday]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=37</guid>
		<description><![CDATA[
Today for Script Sunday I implemented Bresenham&#8217;s line algorithm
in the web browser. If you&#8217;ve got JavaScript enabled, you should see a red square in the upper-left hand corner
of the box below.
Clicking anywhere within the box should send the square to that location.



Here&#8217;s the source code:


/**
 * Bresenham&#8217;s line algorithm function
 * by Matt Hackett [scriptnode.com]
 [...]]]></description>
			<content:encoded><![CDATA[<p>
Today for <a href="/tag/script-sunday/">Script Sunday</a> I implemented <a href="http://en.wikipedia.org/wiki/Bresenham's_line_algorithm">Bresenham&#8217;s line algorithm</a><br />
in the web browser. If you&#8217;ve got <a href="/article/how-to-enable-javascript/">JavaScript enabled</a>, you should see a red square in the upper-left hand corner<br />
of the box below.<br />
Clicking anywhere within the box should send the square to that location.
</p>
<div id="example" style="border: 1px dashed #000; height: 200px; margin-bottom: 1em; position: relative;"></div>
<p>
Here&#8217;s the source code:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
/**<br />
 * Bresenham&#8217;s line algorithm function<br />
 * by Matt Hackett [scriptnode.com]<br />
 * @param {Object} el The element to target (accepts both strings for id&#8217;s and element objects themselves)<br />
 * @param {Number} x1 The starting X coordinate<br />
 * @param {Number} y1 The starting Y coordinate<br />
 * @param {Number} x2 The finishing X coordinate<br />
 * @param {Number} y2 The finishing Y coordinate<br />
 * @param {Object} params Additional parameters [delay, onComplete, speed]<br />
 */<br />
var bresenham = function(el, x1, y1, x2, y2, params) {</p>
<p>	var<br />
		interval,<br />
		b,<br />
		m,<br />
		sx,<br />
		y;</p>
<p>	var init = function() {</p>
<p>		var dx, dy, sy;</p>
<p>		// Default parameters<br />
		params = params || {};<br />
		params.delay = params.delay || 10;<br />
		params.onComplete = params.onComplete || function(){};<br />
		params.speed = params.speed || 5;</p>
<p>		// No point in doing anything if we&#8217;re not actually moving<br />
		if ((x1 == x2) &amp;&amp; (y1 == y2)) {<br />
			plot(x1, y);<br />
			params.onComplete();<br />
			return;<br />
		}</p>
<p>		el = ((typeof el === &#8217;string&#8217;) ? document.getElementById(el) : el);</p>
<p>		// Initalize the math<br />
		dx = x2 &#8211; x1;<br />
		sx = (dx &lt; 0) ? -1 : 1;<br />
		dy = y2 &#8211; y1;<br />
		sy = (dy &lt; 0) ? -1 : 1;<br />
		m = dy / dx;<br />
		b = y1 &#8211; (m * x1);</p>
<p>		interval = setInterval(next, params.delay);</p>
<p>	};</p>
<p>	/**<br />
	 * Execute the algorithm and move the element<br />
	*/<br />
	var next = function() {</p>
<p>		y = Math.round((m * x1) + b);<br />
		plot(x1, y);<br />
		x1 += (sx * params.speed);</p>
<p>		if (x1 &gt;= x2) {<br />
			clearInterval(interval);<br />
			params.onComplete();<br />
		}</p>
<p>	};</p>
<p>	/**<br />
	 * Move the target element to the given coordinates<br />
	 * @param float x The horizontal coordinate<br />
	 * @param float y The vertical coordinate<br />
	*/<br />
	var plot = function(x, y) {<br />
		el.style.left = x + &#8216;px&#8217;;<br />
		el.style.top = y + &#8216;px&#8217;;<br />
	};</p>
<p>	init();</p>
<p>};<br />
</textarea></p>
<p>
The parameters are self-explanatory except for the (optional) <code>params</code> object. Here&#8217;s a breakdown:
</p>
<ul>
<li><code>delay</code>: The interval that the <code>setInterval</code> call uses [default: 10]</li>
<li><code>onComplete</code>: An optional function to call when the algorithm is finished</li>
<li><code>speed</code>: How quickly to move the element down the line [default: 5]</li>
</ul>
<p>
I&#8217;m not sure how much use this has in the &#8220;real world&#8221; but it&#8217;s typically pretty neat to see elements dancing around the page,<br />
and I always love to see classic algorithms put to good use.<br />
Enjoy!
</p>
<ul>
<li><a href="/assets/js/bresenhams-line-algorithm/bresenhams-line-algorithm.js">[Download bresenhams-line-algorithm.js]</a> (1,769 bytes)</li>
<li><a href="/assets/js/bresenhams-line-algorithm/bresenhams-line-algorithm-min.js">[Download bresenhams-line-algorithm-min.js]</a> (516 bytes; compressed)</li>
</ul>
<p><script src="/assets/js/bresenhams-line-algorithm/bresenhams-line-algorithm-min.js" type="text/javascript"></script><br />
<script type="text/javascript">
<!--//	
YAHOO.util.Event.onDOMReady(function() {</p>
<p>	var
		busy = false,
		con = document.getElementById('example'),
		el = document.createElement('div'),
		x = 0,
		y = 0;</p>
<p>	el.style.border = '1px solid red';
	el.style.left = '0px';
	el.style.top = '0px';
	el.style.position = 'absolute';
	el.style.width = '25px';
	el.style.height = '25px';</p>
<p>	con.appendChild(el);</p>
<p>	// Listen for the click event
	YAHOO.util.Event.addListener(con, 'click', function(e) {</p>
<p>		if (busy) {
			return;
		}</p>
<p>		var coords = YAHOO.util.Event.getXY(e);
		busy = true;
		x = coords[0] - YAHOO.util.Dom.getX(con);
		y = coords[1] - YAHOO.util.Dom.getY(con);</p>
<p>		bresenham(el, 0, 0, x, y, {
			onComplete : function() {
				busy = false;
			}
		});</p>
<p>	});</p>
<p>});
//-->
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/bresenhams-line-algorithm-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting a Random Number Within a Range</title>
		<link>http://scriptnode.com/article/getting-a-random-number-within-a-range/</link>
		<comments>http://scriptnode.com/article/getting-a-random-number-within-a-range/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 04:35:38 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Novice]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=28</guid>
		<description><![CDATA[
Getting a random number in JavaScript is easy. Just use Math.random(). The problem is that this returns a huge float between 0 and 1. It&#8217;s pretty trivial to get something useful out of it, such as a random number between 0 and X, but typically the behavior a developer wants is within a range, for [...]]]></description>
			<content:encoded><![CDATA[<p>
Getting a random number in JavaScript is easy. Just use <code>Math.random()</code>. The problem is that this returns a huge <code>float</code> between 0 and 1. It&#8217;s pretty trivial to get something useful out of it, such as a random number between 0 and X, but typically the behavior a developer wants is within a range, for example from 50 to 100 (like the terrific <a href="http://us3.php.net/manual/en/function.rand.php"><code>rand()</code> function</a> provides in <a href="http://php.net/">PHP</a>).
</p>
<p>
Here&#8217;s a simple script to get a random range:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var rand = function(from, to) {<br />
	return (from + Math.floor((to &#8211; from + 1) * Math.random()));<br />
};<br />
</textarea></p>
<p>
Try the button below to get a random number between 50 and 100:
</p>
<p>
<button id="example-1">50 &#8211; 100</button>
</p>
<p><script type="text/javascript"></p>
<p>var rand = function(from, to) {
	return (from + Math.floor((to - from + 1) * (Math.random() % 1)));
};</p>
<p>YAHOO.util.Event.addListener('example-1', 'click', function() {
alert(rand(50, 100));
});</p>
<p></script></p>
<p>
It&#8217;s that easy! Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/getting-a-random-number-within-a-range/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
