<?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>Echo.2 &#187; algorithm</title>
	<atom:link href="http://blogs.infoecho.net/echo/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.infoecho.net/echo</link>
	<description>ping and pong in the ocean of network</description>
	<lastBuildDate>Tue, 27 Mar 2012 05:49:06 +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>Demonstrate &#8220;quickhull&#8221; implementation in javascript</title>
		<link>http://blogs.infoecho.net/echo/2007/03/13/demonstrate-quickhull-implementation-in-javascript/</link>
		<comments>http://blogs.infoecho.net/echo/2007/03/13/demonstrate-quickhull-implementation-in-javascript/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 06:54:23 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=95</guid>
		<description><![CDATA[Quickhull is an algorithm that is similar to the quicksort using a divide and conquer strategy to find the convex hull for scattered points. The green line in the plot is the initial base line and gray lines are the intermediate base line. The final convex hull is shown in red. Get Random Points Get [...]]]></description>
			<content:encoded><![CDATA[<p>Quickhull is an algorithm that is similar to the quicksort using a divide and conquer strategy to find the <a href="http://en.wikipedia.org/wiki/Convex_hull" rel="nofollow"> convex hull</a> for scattered points.  The green line in the plot is the initial base line and gray lines are the intermediate base line.  The final convex hull is shown in red.<br />
<script languaage='javascript' src='http://blogs.infoecho.net/echo/js/qh.js'></script><br />
<button onclick='qhPlotPoints()'>Get Random Points</button> <button onclick='qhPlotConvexHull()'>Get convex hull</button><br />
<canvas id="qh_demo" width=200 height=200 style='margin:20pt;border:solid 1px #888;'></canvas><br />
</p>
<p>source code: qh.js</p>
<pre>
<textarea cols= 50 rows = 10>
function getRandomPoints(numPoint, xMax, yMax) {
    var points = new Array();
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ xMax /4 + r * Math.cos(theta), yMax/2 + 2 * r * Math.sin(theta) ] )
    }
    var phase = Math.random() * Math.PI * 2;
    for (var i = 0; i < numPoint/2; i++) {
        var r =  Math.random()*xMax/4;
        var theta = Math.random() * 1.5 * Math.PI + phase;
        points.push( [ xMax /4 * 3 +  r * Math.cos(theta), yMax/2 +  r * Math.sin(theta) ] )
    }
    return points
}

function getDistant(cpt, bl) {
    Vy = bl[1][0] - bl[0][0];
    Vx = bl[0][1] - bl[1][1];
    return (Vx * (cpt[0] - bl[0][0]) + Vy * (cpt[1] -bl[0][1]))
}

function findMostDistantPointFromBaseLine(baseLine, points) {
    var maxD = 0;
    var maxPt = new Array();
    var newPoints = new Array();
    for (var idx in points) {
        var pt = points[idx];
        var d = getDistant(pt, baseLine);

        if ( d > 0) {
            newPoints.push(pt);
        } else {
            continue;
        }

        if ( d > maxD ) {
            maxD = d;
            maxPt = pt;
        }

    }
    return {'maxPoint':maxPt, 'newPoints':newPoints}
}

var allBaseLines = new Array();
function buildConvexHull(baseLine, points) {

    //plotBaseLine(baseLine,'rgb(180,180,180)');
    allBaseLines.push(baseLine)
    var convexHullBaseLines = new Array();
    var t = findMostDistantPointFromBaseLine(baseLine, points);
    if (t.maxPoint.length) {
        convexHullBaseLines = convexHullBaseLines.concat( buildConvexHull( [baseLine[0],t.maxPoint], t.newPoints) );
        convexHullBaseLines = convexHullBaseLines.concat( buildConvexHull( [t.maxPoint,baseLine[1]], t.newPoints) );
        return convexHullBaseLines;
    } else {
        return [baseLine];
    }
}

function getConvexHull(points) {
    //find first baseline
    var maxX, minX;
    var maxPt, minPt;
    for (var idx in points) {
        var pt = points[idx];
        if (pt[0] > maxX || !maxX) {
            maxPt = pt;
            maxX = pt[0];
        }
        if (pt[0] < minX || !minX) {
            minPt = pt;
            minX = pt[0];
        }
    }
    var ch = [].concat(buildConvexHull([minPt, maxPt], points),
                       buildConvexHull([maxPt, minPt], points))
    return ch;
}

function plotBaseLine(baseLine,color) {
    var ctx = document.getElementById('qh_demo').getContext('2d');
    var pt1 = baseLine[0]
    var pt2 = baseLine[1];
    ctx.save()
    ctx.strokeStyle = color;
    ctx.beginPath();
    ctx.moveTo(pt1[0],pt1[1]);
    ctx.lineTo(pt2[0],pt2[1]);
    ctx.stroke();
    ctx.restore();
}

var pts;

function qhPlotPoints() {
    ctx = document.getElementById('qh_demo').getContext('2d');
    ctx.clearRect(0,0,200,200);
    ctx.fillStyle = 'rgb(0,0,0)';
    pts = getRandomPoints(250,200,200);
    for (var idx in pts) {
        var pt = pts[idx];
        ctx.fillRect(pt[0],pt[1],2,2);
    }
}

function qhPlotConvexHull() {
    var ch = getConvexHull(pts);
    var eBL = allBaseLines[0];
    function plotIntermediateBL() {
        var l = allBaseLines.shift();
        if (l) {
            plotBaseLine(l, 'rgb(180,180,180)');
            setTimeout(plotIntermediateBL, 250);
        } else {
            for (var idx in ch) {
                var baseLine = ch[idx];
                plotBaseLine(baseLine, 'rgb(255,0,0)');
            }
            plotBaseLine(eBL,'rgb(0,255,0)');
        }
    }
    plotIntermediateBL();
}

</textarea>
< </pre>
<p></textarea></pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2007/03/13/demonstrate-quickhull-implementation-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

