<?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>Playing on the frontier &#187; xaml</title>
	<atom:link href="http://siphon9.net/loune/tag/xaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://siphon9.net/loune</link>
	<description></description>
	<lastBuildDate>Wed, 18 May 2011 12:10:37 +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>Getting the intersection points of two [path] geometries in WPF</title>
		<link>http://siphon9.net/loune/2009/08/getting-the-intersection-points-of-two-path-geometries-in-wpf/</link>
		<comments>http://siphon9.net/loune/2009/08/getting-the-intersection-points-of-two-path-geometries-in-wpf/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 12:33:05 +0000</pubDate>
		<dc:creator>Loune</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[intersection]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://siphon9.net/loune/?p=71</guid>
		<description><![CDATA[While working on an app that utilises geometries in WPF, I needed a way to get the intersection points of the lines of two arbitrary geometries. A google search didn&#8217;t yield any useful hints except a post suggesting to use mathematics. That would be ideal for very simple geometries like lines, but with complex geometries, [...]]]></description>
			<content:encoded><![CDATA[<p>While working on an app that utilises geometries in WPF, I needed a way to get the intersection points of the lines of two arbitrary geometries. A google search didn&#8217;t yield any useful hints except a post suggesting to use mathematics. That would be ideal for very simple geometries like lines, but with complex geometries, it becomes tiresome real quick. The framework doesn&#8217;t seem to have any built in functions that calculate that, so it&#8217;s time for some hack and slash.</p>
<p>After a few days of thinking, I came up with a simple, yet effective (but not the most efficient solution). For those who just want the intersection between two geometries, there is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.combinedgeometry.aspx">CombinedGeometry</a> geometry class which takes input in the form of two geometries. Setting the GeometryCombineMode to Intersect gives a geometry which is the intersection of the two. At the otherside of the WPF realm, we have <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.geometry.getwidenedpathgeometry.aspx">Geometry.GetWidenedPathGeometry()</a>. This method basically converts/strokes path lines to an approximate geometry. Combining these two concepts, we can produce an intersection from two widened path geometries (the two paths which we want the intersection of).</p>
<pre class="brush: csharp; title: ; notranslate">
public static Point[] GetIntersectionPoints(Geometry g1, Geometry g2)
{
Geometry og1 = g1.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));
Geometry og2 = g2.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));

CombinedGeometry cg = new CombinedGeometry(GeometryCombineMode.Intersect, og1, og2);

PathGeometry pg = cg.GetFlattenedPathGeometry();
Point[] result = new Point[pg.Figures.Count];
for (int i = 0; i &lt; pg.Figures.Count; i++)
{
Rect fig = new PathGeometry(new PathFigure[] { pg.Figures[i] }).Bounds;
result[i] = new Point(fig.Left + fig.Width / 2.0, fig.Top + fig.Height / 2.0);
}
return result;
}
</pre>
<p>The function will return an array of zero or more points of intersection. To test it</p>
<pre class="brush: csharp; title: ; notranslate">
sg1 = StreamGeometry.Parse(&quot;M0,0 L100,100&quot;);
sg2 = StreamGeometry.Parse(&quot;M0,100 L100,0&quot;);
Point[] pts = GetIntersectionPoints(sg1, sg2);
// pts[0] is {50,50}
</pre>
<p>Hope this helps someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://siphon9.net/loune/2009/08/getting-the-intersection-points-of-two-path-geometries-in-wpf/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

