<?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>Cloudwisp &#187; Programming</title>
	<atom:link href="http://www.cloudwisp.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cloudwisp.net</link>
	<description>Tech Blog</description>
	<lastBuildDate>Fri, 09 Apr 2010 22:22:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>VB.net and Visual Studio 2008</title>
		<link>http://www.cloudwisp.net/programming/vb-net-and-visual-studio-2008/</link>
		<comments>http://www.cloudwisp.net/programming/vb-net-and-visual-studio-2008/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 03:22:40 +0000</pubDate>
		<dc:creator>Rols</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[visual studio 2008]]></category>
		<category><![CDATA[windows applications]]></category>

		<guid isPermaLink="false">http://www.cloudwisp.net/?p=253</guid>
		<description><![CDATA[For my latest project, I got a chance to use Visual Studio 2008 to create a Windows application in the vb.net language. After learning my way around the interface, I was off and running at the speed of light. <p>Continue reading <a href="http://www.cloudwisp.net/programming/vb-net-and-visual-studio-2008/">VB.net and Visual Studio 2008</a></p>]]></description>
			<content:encoded><![CDATA[<p>For my latest project, I got a chance to use Visual Studio 2008 to create a Windows application in the vb.net language. After learning my way around the interface, I was off and running at the speed of light.</p>
<p>My previous encounters with Visual Basic were with Visual Basic 6.0  and the VB macro editor that comes with built into Microsoft Office 2002. So I have hardly come to this with no experience, however it is true to say that without much effort and a quick tutorial, even beginners can achieve quite interesting things.</p>
<p>For those that aren&#8217;t familiar with it, VB.net is an evolution of the Visual Basic language created by Microsoft. It includes a whole new set of .net framework class libraries, as well as the most of the traditional class libraries.  VB.net is used for both offline and online applications. For this article, I&#8217;m interested only in the traditional Windows &#8220;forms&#8221; application.</p>
<p><strong>The Visual Editor:</strong></p>
<div id="attachment_311" class="wp-caption alignnone" style="width: 510px"><a href="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsVisual-e1265397658948.jpg" rel="lightbox"><img class="size-full wp-image-311" title="The Visual Editor" src="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsVisual-e1265397658948.jpg" alt="" width="500" height="313" /></a><p class="wp-caption-text">Screenshot of the visual editor</p></div>
<p>As the name suggests, Visual Basic involves a visual component, which is the graphical forms editor. This allows you to add components to the forms space, such as buttons, text boxes, scroll bars, labels etc, and then you code in the interactions between these components. At the side of the editing area is a properties box, which lists all the various properties that dictate the behavior of those components.</p>
<p>Visual Studio functions much the same as Visual Basic 6.0 in regards to the visual editor.</p>
<p>When I first learnt to use Visual Basic, I picked it up very fast because it was more or less intuitive. What made it very easy to achieve things that I had not even learnt, but only imagined, was one of the program&#8217;s great features:</p>
<p><strong>The Object Browser:</strong></p>
<p><strong> </strong></p>
<div id="attachment_315" class="wp-caption alignnone" style="width: 510px"><a href="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsObjBrowser1-e1265397518521.jpg" rel="lightbox"><img class="size-full wp-image-315" title=" vsObjBrowser" src="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsObjBrowser1-e1265397518521.jpg" alt="" width="500" height="222" /></a><p class="wp-caption-text">Object Browser</p></div>
<p>The object browser contains a browse-able list of class libraries that you can use. Looking through the list, you can get an idea of what can be achieved with Visual Basic, and can browse the syntax and required objects/parameters for various functions. It is important to note that some sub-classes and operations are not shown in the browser.</p>
<p><strong>User Friendliness:</strong></p>
<p>One of my more favourite features in Visual Studio (and its&#8217; predecessors) is the drop down auto-complete list.</p>
<p>Visual Basic is an object oriented language, and the objects are elaborated through the . (dot). For example, to reference a parameter &#8220;height&#8221; of a text box named &#8220;txtBox&#8221;, we would refer to it as txtBox.height</p>
<p>The auto-complete list will pop up when I type the . (dot), and list all those properties that I may choose from. Height would be found within it, and have an icon that represents that it is a property.</p>
<div id="attachment_314" class="wp-caption alignnone" style="width: 510px"><a href="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsAutoComplete1-e1265397414524.jpg" rel="lightbox"><img class="size-full wp-image-314" title="vsAutoComplete" src="http://www.cloudwisp.net/wp-content/uploads/2010/01/vsAutoComplete1-e1265397414524.jpg" alt="" width="500" height="152" /></a><p class="wp-caption-text">Drop-down box</p></div>
<p>Also found within the auto-complete list, are functions. These have different icons. Examples of these would be show(), hide(), close()</p>
<p>This makes for very easy coding. I can be guided along by the auto-complete without having learnt the ins and outs of the object with which i&#8217;m interacting.</p>
<p>As for user-friendliness as a whole, there is more complexity in Visual Studio than there was in Visual Basic 6.0. This has great advantages, but one disadvantage is that it is definitely less user-friendly than it once was, especially at compile time.</p>
<p>Builds can be debug or release, and there are versions and references that must be accounted for. All of this can be learnt fairly easily.</p>
<p><strong>Releasing your software to the world:</strong></p>
<p>Visual Studio has a publish option, that lets you publish a software installer. For me this process was counter-intuitive to my expectations, as most applications I have downloaded have been in standalone single install files, either msi or exe.</p>
<p>When you publish your application it will be placed in a folder with subdirectories that contain your application and its&#8217; files.</p>
<p>The best way to publish with a standalone installer is to compile your application in the standard way, and then create a new project choosing the &#8220;installer package&#8221; template. This will create an msi installer into which you can add your executable and program files.</p>
<p>I didn&#8217;t find any good tutorials that go through this process from start to finish, so be prepared to spend some time searching the web for any answers you may have, and do plenty of testing if you wish to take this path.</p>
<p>Of course you can stick with the published installer.</p>
<p><strong>Conclusion:</strong></p>
<p>Overall, Visual Studio 2008 and vb.net (with net framework 3.5) has plenty of great features and lots of class libraries to help you achieve what you want.</p>
<p>I haven&#8217;t even touched on the possibilities that arise from the online projects that can be created and managed with VS2008, so I&#8217;m sure there&#8217;ll be plenty more adventures for me in the years to come in the world of VB.net</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudwisp.net/programming/vb-net-and-visual-studio-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AJAX for beginners</title>
		<link>http://www.cloudwisp.net/programming/ajax-for-beginners/</link>
		<comments>http://www.cloudwisp.net/programming/ajax-for-beginners/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 18:18:50 +0000</pubDate>
		<dc:creator>Rols</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[DHTML]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[shout]]></category>
		<category><![CDATA[shoutBox]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.cloudwisp.net/?p=181</guid>
		<description><![CDATA[The advent of AJAX (Asynchronous Javascript and XML) has opened up new doors to web developers, allowing for interaction with and display of database and other external data in a web document, without the need to load a new page.

This is my tutorial for beginners wanting to use AJAX on their websites. <p>Continue reading <a href="http://www.cloudwisp.net/programming/ajax-for-beginners/">AJAX for beginners</a></p>]]></description>
			<content:encoded><![CDATA[<p>The advent of AJAX (Asynchronous Javascript and XML) has opened up new doors to web developers, allowing for interaction with and display of database and other external data in a web document, without the need to load a new page.</p>
<p>This is my tutorial for beginners wanting to use AJAX on their websites.</p>
<p><strong>xmlHttpRequest</strong></p>
<p>AJAX is made possible by a DOM API by the name of xmlHttpRequest, which allows HTTP or HTTPS requests to be initiated and handled from within Javascript.</p>
<p>Below is the function to place in your .js file for the current document.</p>

<div class="wp_codebox"><table><tr id="p1811"><td class="code" id="p181code1"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> xmlRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	<span style="color: #006600; font-style: italic;">// code for IE7+, Firefox, Chrome, Opera, Safari</span>
  		xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// code for IE6, IE5</span>
		xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Your browser does not support XMLHTTP!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> xmlHttp<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Grabbing and Displaying a Record-set</strong></p>
<p>Suppose you wanted to display data on your website that is updated every 10 seconds, showing a list of recent entries into a shout-out wall table, looking a bit like this:</p>
<p><em><strong>Louis</strong>: Love the site!</em></p>
<p>Disregarding the process beyond the scope of this tutorial (of entering the data into the database), we will need to take these steps to achieve this outcome:<br />
1. Write the query and output server-side script (php for this tutorial)<br />
2. Write some javascript to request the server-side script, and output the response to a designated area of the site.</p>
<p><strong>The Shout-out Table</strong></p>
<p>In the database, we will have a table called &#8220;shoutouts&#8221; with the following structure:<br />
<span style="text-decoration: underline;">soID</span>(int,auto_increment),userName(varchar 50),shoutOutMsg(varchar 255)</p>
<p><strong>The PHP script</strong></p>
<p>The script below will show you how to query the database and output the results. The query uses a limit to display only the most recent shout-outs.</p>
<p><strong>getShoutOuts.php</strong></p>

<div class="wp_codebox"><table><tr id="p1812"><td class="code" id="p181code2"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;connect.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM shoutouts ORDER BY soID DESC LIMIT 5&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;ERROR|&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;OK|&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userName'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/strong&gt;: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'shoutOutMsg'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We use &#8220;ORDER BY soID DESC&#8221; to make the latest posts show up first, and &#8220;LIMIT 5&#8243; to select the first 5 out of this reversed order set. The script then loops through each of the 5 records and outputs a row of html that displays the user name and message.</p>
<p><strong>The HTML</strong></p>
<p>We need a place within our document to put the shouts. You can create elements with javascript, but for beginners it may be easier to simply add a placeholder element in the source document. Either way, the element must have an ID that can be called upon later by the javascript function, looking like this:</p>
<p><strong>The Javascript</strong></p>
<p>Our Javascript will consist of a function that will request the data  and subsequently insert it into the document. You should take note that the function called upon by this script &#8220;xmlRequest&#8221; was shown at the beginning of the tutorial.</p>

<div class="wp_codebox"><table><tr id="p1813"><td class="code" id="p181code3"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getShoutOuts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> xmlObject<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> xmlRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	xmlObject.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;getShoutOuts.php&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	xmlObject.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	xmlObject.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlObject.<span style="color: #660066;">readyState</span><span style="color: #339933;">==</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> returnString<span style="color: #339933;">=</span>xmlObject.<span style="color: #660066;">responseText</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;|&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>returnString<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;OK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">//query was successful</span>
				<span style="color: #003366; font-weight: bold;">var</span> returnedHTML<span style="color: #339933;">=</span>returnString<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'shoutContainer'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>returnedHTML<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>returnString<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;ERROR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">//query failed in php and reported an error</span>
				<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>returnString<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t<span style="color: #339933;">=</span>setTimeout<span style="color: #009900;">&#40;</span>getShoutOuts<span style="color: #339933;">,</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Simply invoke the function getShoutOuts() on the loading of your document, and the 5 most recent shoutOut entries will be displayed on your page within the shoutContainer div element.</p>
<p><strong>General note about the Ajax Request</strong>:</p>
<p>There&#8217;s a lot I could say about dealing with these AJAX requests, but something that is often not noticed when starting to use them, is that the onreadystatechange function you construct, will not execute until data is returned. So even though it is above the script below it, it will not execute before it but after it at some point undetermined because of the delay in communications.</p>
<p>What this means&#8230; is if you set a variable using data returned from an AJAX request, don&#8217;t expect it to be available until xmlObject.readyState==4 and the code within that has been executed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudwisp.net/programming/ajax-for-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Point in a Polygon</title>
		<link>http://www.cloudwisp.net/programming/point-in-a-polygon/</link>
		<comments>http://www.cloudwisp.net/programming/point-in-a-polygon/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 18:28:28 +0000</pubDate>
		<dc:creator>Rols</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[event listener]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mouse events]]></category>
		<category><![CDATA[mouseover]]></category>
		<category><![CDATA[point in polygon]]></category>
		<category><![CDATA[polygon]]></category>
		<category><![CDATA[ray casting]]></category>
		<category><![CDATA[raycasting]]></category>
		<category><![CDATA[tiles]]></category>

		<guid isPermaLink="false">http://www.cloudwisp.net/?p=171</guid>
		<description><![CDATA[I just though I might share a function that I found in the course of trying to capture accurate mouse events in a grid Map editor for my isometric game. The function can be used to find out if a given coordinate falls within a polygon, defined by it's bounding points. <p>Continue reading <a href="http://www.cloudwisp.net/programming/point-in-a-polygon/">Point in a Polygon</a></p>]]></description>
			<content:encoded><![CDATA[<p>I just though I might share a function that I found in the course of trying to capture accurate mouse events in a grid Map editor for my isometric game. The function can be used to find out if a given coordinate falls within a polygon, defined by it&#8217;s bounding points.</p>
<p>This is a function by Jonas Raoni Soares Silva, taken from <a href="http://jsfromhell.com/math/is-point-in-poly">here</a>:</p>

<div class="wp_codebox"><table><tr id="p1714"><td class="code" id="p171code4"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> isPointInPoly<span style="color: #009900;">&#40;</span>poly<span style="color: #339933;">,</span> pt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> i <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> poly.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> j <span style="color: #339933;">=</span> l <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> l<span style="color: #339933;">;</span> j <span style="color: #339933;">=</span> i<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> pt.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> pt.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> poly<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>poly<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> pt.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> pt.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>pt.<span style="color: #660066;">x</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>poly<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>pt.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span>poly<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> poly<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">=</span> <span style="color: #339933;">!</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> c<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The function takes an object containing a property x, and property y, as well as an array, containing the bounding points of the polygon.</p>
<p>If you&#8217;re interested in how this applied to my isometric grid space, I&#8217;ll go over the basic concept below:</p>
<p>The grid space is made up of 400 (20&#215;20 grid) images, layered and spaced in the DOM at a ratio of 2:1 W:H, making it look like a squashed diamond.</p>
<p>If we know the top and left value of each image, these can be used to construct an array of polygon coordinates for each tile.</p>
<p>Imagine a diamond in a rectangle shaped image. The left value will correspond to the x value of the first point, going from left to right, clockwise around the diamond polygon, the top value will correspond to the y value of the second point.</p>
<p>So going around the diamond, it would look like this;</p>

<div class="wp_codebox"><table><tr id="p1715"><td class="code" id="p171code5"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> polyPoints<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// put this in global</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// values known: topVal, leftVal, tileHeight and tileWidth (W/H of diamond in each tile image)</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//for each tile(tileX,tileY):</span>
polyPoints<span style="color: #009900;">&#91;</span>tileX<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span>tileY<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>leftVal<span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>topVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>tileHeight<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>leftVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>tileWidth<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>topVal<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>leftVal<span style="color: #339933;">+</span>tileWidth<span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>topVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>tileHeight<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>leftVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>tileWidth<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>topVal<span style="color: #339933;">+</span>tileHeight<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #006600; font-style: italic;">//don't forget to repeat the very first point to complete the polygon</span>
<span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>leftVal<span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>topVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>tileHeight<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now to determine if an event such as click or mousemove is within one of the polygons, an event listener must be added to the entire surrounding element, and on each event trigger, the entire array contained in polyPoints must be checked with the function isPointInPoly;</p>
<p>So when you have cursorX, cursorY, load them into an object and pass it to the following function:</p>

<div class="wp_codebox"><table><tr id="p1716"><td class="code" id="p171code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> gridSize<span style="color: #339933;">=</span><span style="color: #CC0000;">20</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//global declaration</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> isPosInTiles<span style="color: #009900;">&#40;</span>cursorPos<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> isFound<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> foundCoord<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>x<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span>gridSize<span style="color: #339933;">;</span>x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> y<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>y<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span>gridSize<span style="color: #339933;">;</span>y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isPointInPoly<span style="color: #009900;">&#40;</span>cursorPos<span style="color: #339933;">,</span>polyPoints<span style="color: #009900;">&#91;</span>x<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			isFound<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			foundCoord<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">:</span>y<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isFound<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">return</span> foundCoord<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If the function returns anything other than false, then the event has taken place within one of the tiles, and the tile&#8217;s x and y values within the grid will be returned and can subsequently be used.</p>
<p>All in all, a very handy function, which I&#8217;m sure will be used many more times in the course of my work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudwisp.net/programming/point-in-a-polygon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>z-index Organization strategy</title>
		<link>http://www.cloudwisp.net/programming/z-index-organization-strategy/</link>
		<comments>http://www.cloudwisp.net/programming/z-index-organization-strategy/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 22:06:40 +0000</pubDate>
		<dc:creator>Rols</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[layers]]></category>
		<category><![CDATA[organization]]></category>
		<category><![CDATA[strategy]]></category>
		<category><![CDATA[z-index]]></category>
		<category><![CDATA[zIndex]]></category>

		<guid isPermaLink="false">http://www.cloudwisp.net/?p=165</guid>
		<description><![CDATA[<p>In my latest adventure in the land of Javascript I have streamlined my strategy for organizing my z-index structure for complex applications. Complex being where &#8220;windows&#8221; and various items may need to be laid over the top of other elements in the DOM. I thought I would share this strategy.</p>
<p></p>
<p>in the global section of my javascript document, I declare a lower and upper zIndex boundary for each &#8220;layer&#8221; within the document.</p>
<p>For example, I might have a space used for a <p>Continue reading <a href="http://www.cloudwisp.net/programming/z-index-organization-strategy/">z-index Organization strategy</a></p>]]></description>
			<content:encoded><![CDATA[<p>In my latest adventure in the land of Javascript I have streamlined my strategy for organizing my z-index structure for complex applications. Complex being where &#8220;windows&#8221; and various items may need to be laid over the top of other elements in the DOM. I thought I would share this strategy.</p>
<p><span id="more-165"></span></p>
<p>in the global section of my javascript document, I declare a lower and upper zIndex boundary for each &#8220;layer&#8221; within the document.</p>
<p>For example, I might have a space used for a chess game, laid out in an isometric fashion, meaning chess pieces will overlap to some degree. And all chess pieces will lay over the board (consisting of several layers giving the illusion of a single board). In addition, several menus will be laid over the entire game viewer when called.</p>
<p>So in this example, my global declarations might look like this.</p>

<div class="wp_codebox"><table><tr id="p1658"><td class="code" id="p165code8"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> numBoardLayers<span style="color: #339933;">=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> numWindowLayers<span style="color: #339933;">=</span><span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> numPieces<span style="color: #339933;">=</span><span style="color: #CC0000;">32</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> boardLower<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> boardUpper<span style="color: #339933;">=</span>boardLower<span style="color: #339933;">+</span>numBoardLayers<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> pieceLower<span style="color: #339933;">=</span>boardUpper<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> pieceUpper<span style="color: #339933;">=</span>pieceLower<span style="color: #339933;">+</span>numPieces<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> windowLower<span style="color: #339933;">=</span>pieceUpper<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> windowUpper<span style="color: #339933;">=</span>windowLower<span style="color: #339933;">+</span>numWindowLayers<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the number of layers in the board may be increased etc without the need to change scores of z-index values. Now that the global variables are set, they can be used within the functions that construct the board.</p>
<p>For example, a chess piece is rendered into the DOM at tile number (x), the z-index value can be set at pieceLower+(x)-1</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudwisp.net/programming/z-index-organization-strategy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
