﻿<?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>Keith Hair &#187; Polygon</title>
	<atom:link href="http://keith-hair.net/blog/tag/polygon/feed/" rel="self" type="application/rss+xml" />
	<link>http://keith-hair.net/blog</link>
	<description>Scripting is fun like any other hobby</description>
	<lastBuildDate>Sun, 21 Mar 2010 20:08:24 +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>Line to Polygon intersection data</title>
		<link>http://keith-hair.net/blog/2008/08/08/line-to-polygon-intersection-data/</link>
		<comments>http://keith-hair.net/blog/2008/08/08/line-to-polygon-intersection-data/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 03:59:24 +0000</pubDate>
		<dc:creator>Keith H</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Intersection]]></category>
		<category><![CDATA[Line]]></category>
		<category><![CDATA[Point]]></category>
		<category><![CDATA[Polygon]]></category>

		<guid isPermaLink="false">http://keith-hair.com/blog/?p=24</guid>
		<description><![CDATA[This Actionscript 3 function returns some properties that are about a line-to-polygon intersection.
It's useful for knowing all intersection points of a segment intersecting with a polygon, and knowing if the end points of the segment are inside the polygon.

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_line_to_polygon_intersection_1948921994"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/blog/examples/intersections/line_to_polygon_intersection.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/blog/examples/intersections/line_to_polygon_intersection.swf"
			name="fm_line_to_polygon_intersection_1948921994"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>

This uses the "lineIntersectLine" function of the earlier post.
&#160;
/*---------------------------------------------------------------------------
Returns an Object with the [...]]]></description>
			<content:encoded><![CDATA[<p>This Actionscript 3 function returns some properties that are about a line-to-polygon intersection.<br />
It's useful for knowing all intersection points of a segment intersecting with a polygon, and knowing if the end points of the segment are inside the polygon.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_line_to_polygon_intersection_20918263"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/blog/examples/intersections/line_to_polygon_intersection.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/blog/examples/intersections/line_to_polygon_intersection.swf"
			name="fm_line_to_polygon_intersection_20918263"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><span id="more-24"></span></p>
<p>This uses the "<a href="http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/">lineIntersectLine</a>" function of the earlier post.</p>
<pre class="actionscript">&nbsp;
<span style="color: #808080; font-style: italic;">/*---------------------------------------------------------------------------
Returns an Object with the following properties:
intersects        -Boolean indicating if an intersection exists.
start_inside      -Boolean indicating if Point A is inside of the polygon.
end_inside       -Boolean indicating if Point B is inside of the polygon.
intersections    -Array of intersection Points along the polygon.
centroid          -A Point indicating &quot;center of mass&quot; of the polygon.
&nbsp;
&quot;pa&quot; is an Array of Points.
----------------------------------------------------------------------------*/</span>
<span style="color: #000000; font-weight: bold;">function</span> lineIntersectPoly<span style="color: #66cc66;">&#40;</span>A : Point, B : Point, pa:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> An:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">1</span>;
	<span style="color: #000000; font-weight: bold;">var</span> Bn:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">1</span>;
	<span style="color: #000000; font-weight: bold;">var</span> C:Point;
	<span style="color: #000000; font-weight: bold;">var</span> D:Point;
	<span style="color: #000000; font-weight: bold;">var</span> i:Point;
	<span style="color: #000000; font-weight: bold;">var</span> cx:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0</span>;
	<span style="color: #000000; font-weight: bold;">var</span> cy:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">0</span>;
	<span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	pa.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>pa<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	result.<span style="color: #006600;">intersects</span> = <span style="color: #000000; font-weight: bold;">false</span>;
	result.<span style="color: #006600;">intersections</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	result.<span style="color: #006600;">start_inside</span>=<span style="color: #000000; font-weight: bold;">false</span>;
	result.<span style="color: #006600;">end_inside</span>=<span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #000000; font-weight: bold;">var</span> n:<span style="color: #0066CC;">int</span>=pa.<span style="color: #006600;">length</span><span style="color: #cc66cc;">-1</span>;
	<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>n &gt; <span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		C=Point<span style="color: #66cc66;">&#40;</span>pa<span style="color: #66cc66;">&#91;</span>n<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>n &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			cx+=C.<span style="color: #006600;">x</span>;
			cy+=C.<span style="color: #006600;">y</span>;
			D=Point<span style="color: #66cc66;">&#40;</span>pa<span style="color: #66cc66;">&#91;</span>n<span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>||Point<span style="color: #66cc66;">&#40;</span>pa<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			i=lineIntersectLine<span style="color: #66cc66;">&#40;</span>A,B,C,D<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				result.<span style="color: #006600;">intersections</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lineIntersectLine<span style="color: #66cc66;">&#40;</span>A,<span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>C.<span style="color: #006600;">x</span>+D.<span style="color: #006600;">x</span>,A.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>,C,D<span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				An++;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lineIntersectLine<span style="color: #66cc66;">&#40;</span>B,<span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>C.<span style="color: #006600;">x</span>+D.<span style="color: #006600;">x</span>,B.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>,C,D<span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				Bn++;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		n--;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>An % <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		result.<span style="color: #006600;">start_inside</span>=<span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>Bn % <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		result.<span style="color: #006600;">end_inside</span>=<span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
	result.<span style="color: #006600;">centroid</span>=<span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>cx/<span style="color: #66cc66;">&#40;</span>pa.<span style="color: #006600;">length</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>,cy/<span style="color: #66cc66;">&#40;</span>pa.<span style="color: #006600;">length</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	result.<span style="color: #006600;">intersects</span> = result.<span style="color: #006600;">intersections</span>.<span style="color: #0066CC;">length</span> &gt; <span style="color: #cc66cc;">0</span>;
	<span style="color: #b1b100;">return</span> result;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://keith-hair.net/blog/2008/08/08/line-to-polygon-intersection-data/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
