<?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>Flassari.is &#187; C++</title>
	<atom:link href="http://flassari.is/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://flassari.is</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 19 Aug 2010 11:36:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Line-Line Intersection in C++</title>
		<link>http://flassari.is/2008/11/line-line-intersection-in-cplusplus/</link>
		<comments>http://flassari.is/2008/11/line-line-intersection-in-cplusplus/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 11:44:43 +0000</pubDate>
		<dc:creator>Flassari</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.flassari.is/?p=41</guid>
		<description><![CDATA[<img src="http://flassari.com/wp-content/uploads/2008/11/linesect.png" alt="Line line intersection function explanation" title="linesect" width="259" height="134" /><br/>I've just been tasked with creating a function to get the intersection of two lines.
With the help of equations from Wolfram Mathworld I created this nifty function: [...]]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://flassari.com/wp-content/uploads/2008/11/linesect.png" alt="Line line intersection function explanation" title="linesect" width="259" height="134" class="size-full wp-image-71" /></center></p>
<p>I&#8217;ve just been tasked with creating a function to get the intersection of two lines.<br />
With the help of equations from Wolfram Mathworld I created this nifty function:</p>
<pre class="brush: cpp">Point* intersection(Point p1, Point p2, Point p3, Point p4) {
	// Store the values for fast access and easy
	// equations-to-code conversion
	float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
	float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;

	float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
	// If d is zero, there is no intersection
	if (d == 0) return NULL;

	// Get the x and y
	float pre = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4);
	float x = ( pre * (x3 - x4) - (x1 - x2) * post ) / d;
	float y = ( pre * (y3 - y4) - (y1 - y2) * post ) / d;

	// Check if the x and y coordinates are within both lines
	if ( x < min(x1, x2) || x > max(x1, x2) ||
		x < min(x3, x4) || x > max(x3, x4) ) return NULL;
	if ( y < min(y1, y2) || y > max(y1, y2) ||
		y < min(y3, y4) || y > max(y3, y4) ) return NULL;

	// Return the point of intersection
	Point* ret = new Point();
	ret->x = x;
	ret->y = y;
	return ret;
}</pre>
<p>Hope it will be to as good use to you as it is to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://flassari.is/2008/11/line-line-intersection-in-cplusplus/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
