<?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>Sai Panyam.NET &#187; Computational Geometry</title>
	<atom:link href="http://www.saipanyam.net/tag/computational-geometry/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saipanyam.net</link>
	<description>On all things .NET &#38; Software Engineering</description>
	<lastBuildDate>Thu, 05 Jan 2012 16:42:35 +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>Algorithms in Computational Geometry – Part 2</title>
		<link>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html</link>
		<comments>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html#comments</comments>
		<pubDate>Fri, 09 Apr 2010 05:01:58 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Expression Design]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=277</guid>
		<description><![CDATA[In Part 1 we discussed the base technique for determining relative orientation. We used that to answer the first two questions: To find relative orientation of two points and two directed segments with a common end point. In Part 2 we answer the next two questions: Given two line segments p0p1 and p1p2, if we [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a> we discussed the base technique for determining relative orientation. We used that to answer the first two questions: To find relative orientation of two points and two directed segments with a common end point. In Part 2 we answer the next two questions:</p>
<ul>
<li>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub>, if we traverse from p<sub>0</sub> to p<sub>2</sub>, did we make a left, right or no turn at p<sub>1</sub>?</li>
<li>Do line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> intersect?</li>
</ul>
<p>We build on what we learned in <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a>, the <strong>cross product</strong>. </p>
<p>Download the demo application <a href="http://www.saipanyam.net/code-center/computational-geometry">here</a></p>
<p><strong>Determining whether consecutive segments turn left or right</strong></p>
<p>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub> we simply check whether the directed segment p<sub>0</sub>p<sub>2</sub> is clockwise or counterclockwise relative to directed segment p<sub>0</sub>p<sub>1</sub>. This is similar to the solution to question 2 in <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a>. If the cross product is negative, then p<sub>0</sub>p<sub>2</sub> is counterclockwise to p<sub>0</sub>p<sub>1</sub>. Hence we should turn <strong>left</strong> when we reach p<sub>1</sub> to reach p<sub>2</sub> while traversing p<sub>0</sub>, p<sub>1</sub> and p<sub>2</sub>. If the cross product is <strong>0</strong> (boundary condition) then it means all the three points are <strong>collinear</strong> (on the same line). The image below shows the three possible scenarios<br />
<div id="attachment_287" class="wp-caption alignnone" style="width: 610px"><img src="http://www.saipanyam.net/wp-content/uploads/2010/04/Consecutive-Segments1-e1270768161354.png" alt="Consecutive Segments Cross Product" title="Consecutive Segments" width="600" height="375" class="size-full wp-image-287" /><p class="wp-caption-text">Using the cross product to determine how p0p1 and p1p2 turn at p1</p></div></p>
<p><strong>Determining whether two line segments intersect</strong></p>
<p>Two line segments intersect if and only if either or both of the following conditions are true:</p>
<ul>
<li>Each line segment straddles the other line segment.</li>
<li>An end point of one line segment is on the other line segment</li>
</ul>
<p>A line segment p<sub>1</sub>p<sub>2</sub> <strong>straddles</strong> another line if p<sub>1</sub> and p<sub>2</sub> are on opposite sides of the line. A boundary condition occurs when one of the end points is <strong>on</strong> the other line.</p>
<p><strong>Algorithm</strong></p>
<p>Calculate the orientation (using cross product!) d<sub>i</sub> for each point of one segment w.r.t the end points of the other segment. For line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> we will get d<sub>1</sub>, d<sub>2</sub>, d<sub>3</sub> and d<sub>4</sub>.</p>
<p>if none of the d<sub>i</sub> are collinear (i.e. d<sub>i</sub> !=0) we check if d<sub>1</sub> and d<sub>2</sub> <strong>and</strong> d<sub>3</sub> and d<sub>4</sub> have opposite orientations (if one is clockwise,other should be counterclockwise). If this condition is true then we know that the line segments <strong>straddle</strong> each other and hence <strong>intersect</strong>. If it is false we can straight away say they <strong>don&#8217;t intersect</strong>.</p>
<p>If any d<sub>i</sub> is collinear (boundary condition) and the corresponding p<sub>i</sub> is <strong>on</strong> the other line segment, the lines <strong>intersect</strong>.<br />
<div id="attachment_306" class="wp-caption alignnone" style="width: 610px"><img src="http://www.saipanyam.net/wp-content/uploads/2010/04/Intersects-e1270789093105.png" alt="Intersect cases" title="Intersects" width="600" height="375" class="size-full wp-image-306" /><p class="wp-caption-text">Shows the different cases that we encounter visually</p></div></p>
<pre>
bool Intersects(p<sub>1</sub>, p<sub>2</sub>, p<sub>3</sub>, p<sub>4</sub>)
{
    d<sub>1</sub> = CrossProduct (p<sub>1</sub>p<sub>3</sub>, p<sub>1</sub>p<sub>2</sub>)
    d<sub>2</sub> = CrossProduct (p<sub>1</sub>p<sub>4</sub>, p<sub>1</sub>p<sub>2</sub>)
    d<sub>3</sub> = CrossProduct (p<sub>3</sub>p<sub>1</sub>, p<sub>3</sub>p<sub>4</sub>)
    d<sub>4</sub> = CrossProduct (p<sub>3</sub>p<sub>2</sub>, p<sub>3</sub>p<sub>4</sub>)

    if( ((d<sub>1</sub> > 0 and d<sub>2</sub><0) or (d<sub>2</sub> > 0 and d<sub>1</sub><0)) <strong>and </strong> ((d<sub>3</sub> > 0 and d<sub>4</sub><0) or (d<sub>4</sub> > 0 and d<sub>3</sub><0)))
    {
      then return <strong>true</strong>
    }
    else if (d<sub>1</sub> = 0 <strong>and</strong> PointOnLine (p<sub>3</sub>, p<sub>4</sub>, p<sub>1</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>2</sub> = 0 <strong>and</strong> PointOnLine (p<sub>3</sub>, p<sub>4</sub>, p<sub>2</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>3</sub> = 0 <strong>and</strong> PointOnLine (p<sub>1</sub>, p<sub>2</sub>, p<sub>3</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>4</sub> = 0 <strong>and</strong> PointOnLine (p<sub>1</sub>, p<sub>2</sub>, p<sub>4</sub>))
    {
       then return <strong>true</strong>
    }
    else
    {
       return <strong>false</strong>
    }
}

bool PointOnLine (p<sub>i</sub>, p<sub>j</sub>, p<sub>k</sub>)
{
    if ( Min (x<sub>i</sub>, x<sub>j</sub>)<= x<sub>k</sub> <= Max (x<sub>i</sub>, x<sub>j</sub>) <strong>and</strong> Min (y<sub>i</sub>, y<sub>j</sub>)<= y<sub>k</sub> <= Max (y<sub>i</sub>, y<sub>j</sub>) )
    {
        return <strong>true</strong>
    }
    else
    {
        return <strong>false</strong>
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Algorithms in Computational Geometry &#8211; Part 1</title>
		<link>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html</link>
		<comments>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html#comments</comments>
		<pubDate>Thu, 08 Apr 2010 18:52:32 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Expression Design]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=219</guid>
		<description><![CDATA[Computational Geometry (CG) deals with algorithms for solving geometric problems. It is used in computer graphics, robotics, computer aided design,statistics among others. A typical CG problem takes as input a set of geometric objects like a set of points, a set of line segments etc. The output is most commonly finding certain attributes of the [...]]]></description>
			<content:encoded><![CDATA[<p>Computational Geometry (CG) deals with algorithms for solving geometric problems. It is used in computer graphics, robotics, computer aided design,statistics among others. A typical CG problem takes as input a set of geometric objects like a set of points, a set of line segments etc. The output is most commonly finding certain attributes of the geometric objects. For e.g. we are interested in answering the following questions in two dimensions (in a plane):</p>
<ol>
<li>Given two points p<sub>1</sub> &amp; p<sub>2</sub> in a co-ordinate plane, is the directed segment p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to directed segment p<sub>0</sub>p<sub>2</sub>? Where p<sub>0</sub> is the <em>origin</em> of the co-ordinate plane</li>
<li>Given two directed segments p<sub>0</sub>p<sub>1</sub> and p<sub>0</sub>p<sub>2</sub>, is p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to p<sub>0</sub>p<sub>2</sub> with respect to the common end point p<sub>0</sub>?</li>
<li>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub>, if we traverse from p<sub>0</sub> to p<sub>2</sub>, did we make a left, right or no turn at p<sub>1</sub>?</li>
<li>Do line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> intersect?</li>
</ol>
<p>Download the demo application <a href="http://www.saipanyam.net/code-center/computational-geometry">here</a></p>
<p>Before we proceed any further a few definitions. <strong>Clockwise </strong>and <strong>Counterclockwise </strong>are intuitively easy to understand. They have the usual colloquial meaning. There is one thing to note though. A co-ordinate plane is not a <em>watch face</em>! Here we are determining the <em>relative orientations</em> of one segment w.r.t the other. Instead of trying to explain it in words, I will show you a picture (should replace a thousand words!).</p>
<div style="float: left;">
<div id="attachment_225" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-225" title="Orientation" src="http://www.saipanyam.net/wp-content/uploads/2010/04/Orientation-300x225.png" alt="Orientation" width="300" height="225" /><p class="wp-caption-text">The darkly shaded orientation region contains vectors counterclockwise relative to p. The lightly shaded region contains vectors clockwise to p </p></div>
</div>
<p>Two vectors are <strong>colinear</strong> if they are pointing in the same or opposite direction. In the picture all vectors that lie along the diagonal will be colinear to p.</p>
<p>With definitions out of the way, let us proceed to answer the initial queries that we posed. A brute force method of computing whether two line segments intersect, would be to find the line equations of the form y=mx + c where m is the slope and c the y-intercept and then compute the point of intersection, and verify whether this point lies on both lines. This involves a lot of expensive computational resources. Further this involves division which is sensitive to round-off errors depending upon the precision of the division operation. For e.g. almost parallel line segments will give incorrect results.</p>
<p>The algorithms given here will only use additions, multiplications, subtractions and comparisons ! No trigonometry also !<br />
We do this by computing the <strong>cross product</strong>. This is a fundamental technique that is at the base of most CG algorithms. Let me say that again. <strong>Cross Product</strong> is a most fundamental and important technique for solving CG problems. A <strong>cross product</strong> p<sub>1</sub> X p<sub>2</sub> is defined as the determinant of the matrix comprising of x and y values of the points.</p>
<div style="float: left;">
<div id="attachment_255" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-255" title="Determinant of Matrix" src="http://www.saipanyam.net/wp-content/uploads/2010/04/Determinant1-300x225.png" alt="Determinant" width="300" height="225" /><p class="wp-caption-text">Determinant of the above matrix will be equal to (x1y2-x2y1)</p></div>
</div>
<p>If the <strong>cross product </strong>is positive then p<sub>1</sub> is clockwise to p<sub>2</sub>. A boundary condition occurs if the cross product is <strong>0</strong>. In that case p<sub>1</sub> and p<sub>2</sub> are <strong>colinear</strong>.</p>
<p><strong>Proof</strong>: We use trigonometric function <strong>tan</strong> on the angle formed by the point (x,y), origin (0,0) and (x,0) for both the points. If the ratio of one tangent to other determines orientation depending upon whether the value is greater than 1 or not.</p>
<pre>//Clockwise:
x1y2-x2y1&gt;0

//So
x1y2&gt;x2y1

//alternatively
(x1y2)/(x2y1)&gt;1

//rearranging the terms
(y2/x2)/(y1/x1)&gt;1

//Notice that tan θ is the ratio of opposite side by adjacent side
//in a right-angled triangle
//so the numerator of the above fraction is tan θ<sub>2</sub> and
//denominator tan θ<sub>1</sub>

(tan θ<sub>2</sub>)/(tan θ<sub>1</sub>)&gt;1

<strong>If the angle θ<sub>2</sub> is greater than θ<sub>1</sub>
then we have proved that p<sub>1</sub> is clockwise to p<sub>2</sub></strong></pre>
<p>The above technique of using cross product makes for an easy and efficient way to answer <strong>Question 1</strong>.<br />
To answer <strong>Question 2</strong> just involves an extra pre-processing step before doing the cross product. To determine whether two directed segments p<sub>0</sub>p<sub>1</sub> and p<sub>0</sub>p<sub>2</sub>, is p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to p<sub>0</sub>p<sub>2</sub> with respect to the common end point p<sub>0</sub> we just translate the axis to use p<sub>0</sub> as the origin and use the cross product as above.</p>
<pre>// To translate the origin to the common end point p<sub>0</sub>
//We let p<sub>1</sub>' (p prime) be the point where 

x<sub>1</sub>' =x<sub>1</sub> - x<sub>0</sub> and
y<sub>1</sub>' =y<sub>1</sub> - y<sub>0</sub>

//We do the above for  p<sub>2</sub> and calculate its prime: p<sub>2</sub>' 

x<sub>2</sub>' =x<sub>2</sub> - x<sub>0</sub> and
y<sub>2</sub>' =y<sub>2</sub> - y<sub>0</sub>

//Then we calculate the cross product of p<sub>1</sub>' x p<sub>2</sub>' 

<strong>If it is positive then the directed segment p<sub>0</sub>p<sub>1</sub> is clockwise to
directed segment p<sub>0</sub>p<sub>2</sub></strong></pre>
<p>In <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-2.html">Part 2</a> we will answer <strong>questions 3 &amp; 4</strong> using the techniques and principles learned here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

