<?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>Webdevelopment at huuah.com &#187; point</title>
	<atom:link href="http://huuah.com/tag/point/feed/" rel="self" type="application/rss+xml" />
	<link>http://huuah.com</link>
	<description>webdevelopment, cms, php, javascript etc</description>
	<lastBuildDate>Sun, 20 Nov 2011 20:30:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Point in polygon with php</title>
		<link>http://huuah.com/point-in-polygon-with-php/</link>
		<comments>http://huuah.com/point-in-polygon-with-php/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 09:27:36 +0000</pubDate>
		<dc:creator>js - huuah</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[polygon]]></category>

		<guid isPermaLink="false">http://huuah.com/?p=395</guid>
		<description><![CDATA[<p>Have you ever had the need to check if a specific coordinate (point) is located inside or outside a polygon shaped area? I never did, so I had to found out how to automate this process, so I didn&#8217;t have to do this by hand. </p>
<p>The problem with this&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Have you ever had the need to check if a specific coordinate (point) is located inside or outside a polygon shaped area? I never did, so I had to found out how to automate this process, so I didn&#8217;t have to do this by hand. </p>
<p>The problem with this kind of assignment is, that I did not know what to search for on Google. Hadn&#8217;t though of it, as being a point in a polygon shaped area. I tried searching with words like &#8216;coordinate inside area&#8217;, &#8216;longitude latititude in area&#8221; and things like that, without much luck. Then a guy on IRC suggested a search for &#8220;point in polygon&#8221; and wuhu. Bingo.</p>
<p>I then found this page: <a href="http://www.assemblysys.com/dataServices/php_pointinpolygon.php">point in polygon</a>, where the author has posted a PHP class that does all the hard work for you. Brilliant.</p>
<p>I have a database table in MySQL containing x and y coordinates for a number of different points. In another table a have coordinates for a number of polygon shapes and I then want to check which point are located inside which polygons.</p>
<p>This is actually pretty simple to do.</p>
<p>Lets say we have a point (2,2) and want to check if this is inside a polygon with these coordinates (1,1), (3,6), (4,0):</p>
<pre class="brush: php; title: ; notranslate">

  //include the point in the polygon classs
  include_once(&quot;point_in_polygon.php&quot;);

  $point = &quot;2 2&quot;;
  $polygon = array(&quot;1 1&quot;, &quot;3 6&quot;, &quot;4 0&quot;)

  $pointLocation = new pointLocation();
  print &quot;Point in &quot; . $pointLocation-&gt;pointInPolygon($point, $polygon) . &quot; the polygon&quot;;
</pre>
<p><em>(NB: remember to arrange the coordinates in a clockwise direction, as counterclockwise returns incorrect results. Thanks to carson, for this tip).</em></p>
<p>Run the code and it will print:</p>
<pre class="brush: plain; title: ; notranslate">
$ php polygon.php
Point is inside the polygon
</pre>
<p><strong>So how does this work?</strong> All you have to do, is to &#8220;draw&#8221; a straight line from the point and all the way through the shape and then count have many lines you cross. If you cross an even or zero number of lines the point is outside the shape and if the number the uneven you are inside. </p>
<p><strong>Got it?</strong> Well, there&#8217;s some quick drawings from Gimp to illustrate it:</p>
<p>The example from the above code:<br />
<img src="http://huuah.com/images/2-2-inside.jpg" alt="point in polygon inside" /></p>
<p>Point is inside because of a uneven number of line crosses:<br />
<img src="http://huuah.com/images/1-cross-inside.jpg" alt="point in polygon inside" /></p>
<p>Point is outside because of a even or zero number of line crosses:<br />
<img src="http://huuah.com/images/2-cross-outside.jpg" alt="point in polygon outside" /></p>
<p><strong>There you go.</strong> This is one way to check if a point is inside a polygon shaped area.</p>
]]></content:encoded>
			<wfw:commentRss>http://huuah.com/point-in-polygon-with-php/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

