<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Finding midpoint of line in ArcGIS API for Silverlight Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25577#M647</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, it may take me a bit to fully understand the Zip method calls, but this is just what I need.&amp;nbsp; I have an identify tool that works very similar to the desktop tool, so when they click on the result in the tree it displays the locator lines from the four sides and I wanted it to hit the midpoint on lines which was giving me troubles.&amp;nbsp; Always enjoy learning some more linq&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;much appreciated&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-joe&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 14 Jun 2012 16:36:53 GMT</pubDate>
    <dc:creator>JoeHershman</dc:creator>
    <dc:date>2012-06-14T16:36:53Z</dc:date>
    <item>
      <title>Finding midpoint of line</title>
      <link>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25575#M645</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am setting up something what I want to draw a line to the middle point of another line.&amp;nbsp; I have gone about a few approaches to finding that Midpoint none of which are returning the proper result.&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Using GetCenter on the bounding envelope, works good with small lines, but for large lines works very poor as the envelope may not even be on the line&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Trying to just grab the middle point (i.e., polyline.Paths[0][polyline.Paths[0].Count/2]).&amp;nbsp; Better because at least it is always on the line, but with long lines usually not near the center because a curved section will have so many points compared to a straight section.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;SPAN&gt;I am stuck thinking I need to compute the length of the line and find the point closest to the center of that length.&amp;nbsp; Calling a GeometryService is out of the question because this needs to work quickly, responding to a user event immediately.&amp;nbsp; I am having fears of high school trigonometry as I think about calculating the length of each individual line segment and adding them all up.&amp;nbsp; Anyone have a simpler approach that I am missing? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Joe&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jun 2012 10:04:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25575#M645</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2012-06-14T10:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Finding midpoint of line</title>
      <link>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25576#M646</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am afraid you can't avoid calculating the length of each individual line segment if you want to get the real middle point of a polyline:).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But once the length of a segment is calculated it's just a LINQ matter to aggregate for a multi paths polyline.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is an extension class allowing to get the midpoint of a polyline by code like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;var point = polyline.Midpoint();&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's based on the calculation of the length and the calculation of a linear position along the line (M = length/2 being a particular case).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's a good LINQ exercise &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;public static class GeometryExtension { &amp;nbsp;&amp;nbsp;&amp;nbsp; public static MapPoint MidPoint(this Polyline polyline) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var length = polyline.Length(); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var m = length / 2.0; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return polyline.LinearPosition(m); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // length of a multipath polyline &amp;nbsp;&amp;nbsp;&amp;nbsp; public static double Length(this Polyline polyline) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return polyline.Paths.Sum((Func&amp;lt;PointCollection, double&amp;gt;)Length); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // length of one path &amp;nbsp;&amp;nbsp;&amp;nbsp; private static double Length(this PointCollection points) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return points.Zip(points.Skip(1), Length).Sum(); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // length of a segment &amp;nbsp;&amp;nbsp;&amp;nbsp; private static double Length(MapPoint point1, MapPoint point2) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2)); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // Get a M position on a multipath polyline &amp;nbsp;&amp;nbsp;&amp;nbsp; public static MapPoint LinearPosition(this Polyline polyline, double m) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PointCollection path1 = polyline.Paths.Where(path =&amp;gt; DecrementTotal(path.Length(), ref m)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .DefaultIfEmpty(polyline.Paths.Last()) // for the case where m &amp;gt; polyline length &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .First(); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return path1.LinearPosition(m); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // Get a M position on a polyline &amp;nbsp;&amp;nbsp;&amp;nbsp; private static MapPoint LinearPosition(this IEnumerable&amp;lt;MapPoint&amp;gt; points, double m) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var seg = points.Zip(points.Skip(1), (pt1, pt2) =&amp;gt; new Tuple&amp;lt;MapPoint, MapPoint&amp;gt;(pt1, pt2)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Where(segment =&amp;gt; DecrementTotal(Length(segment.Item1, segment.Item2), ref m)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .FirstOrDefault(); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return seg == null ? points.Last() : seg.LinearPosition(m); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; // Get a M position on a segment &amp;nbsp;&amp;nbsp;&amp;nbsp; private static MapPoint LinearPosition(this Tuple&amp;lt;MapPoint, MapPoint&amp;gt; segment, double m) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var ratio = m / Length(segment.Item1, segment.Item2); &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new MapPoint(segment.Item1.X + ratio * (segment.Item2.X - segment.Item1.X), segment.Item1.Y + ratio * (segment.Item2.Y - segment.Item1.Y)); &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; private static bool DecrementTotal(double l, ref double cumul) &amp;nbsp;&amp;nbsp;&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (l &amp;gt;= cumul) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return true; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cumul -= l; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false; &amp;nbsp;&amp;nbsp;&amp;nbsp; } }&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jun 2012 13:52:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25576#M646</guid>
      <dc:creator>DominiqueBroux</dc:creator>
      <dc:date>2012-06-14T13:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Finding midpoint of line</title>
      <link>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25577#M647</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, it may take me a bit to fully understand the Zip method calls, but this is just what I need.&amp;nbsp; I have an identify tool that works very similar to the desktop tool, so when they click on the result in the tree it displays the locator lines from the four sides and I wanted it to hit the midpoint on lines which was giving me troubles.&amp;nbsp; Always enjoy learning some more linq&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;much appreciated&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-joe&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jun 2012 16:36:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-silverlight-questions/finding-midpoint-of-line/m-p/25577#M647</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2012-06-14T16:36:53Z</dc:date>
    </item>
  </channel>
</rss>

