TheSwamp

Code Red => .NET => Topic started by: nobody on July 24, 2016, 05:14:00 AM

Title: Point Inside Polyline
Post by: nobody on July 24, 2016, 05:14:00 AM
I'm using this to check if a point is inside a closed polyline.  It fails in some cased though. Anyone know how I might improve it, or has anything better they might share?

Code - C#: [Select]
  1.         public static bool IsPointInPolyline(Point2d[] polypointarray, Point2d mycurrentpoint)
  2.         {
  3.             var calc = polypointarray.Skip(1).Select((p, i) => (mycurrentpoint.Y - polypointarray[i].Y) * (p.X - polypointarray[i].X) - (mycurrentpoint.X - polypointarray[i].X) * (p.Y - polypointarray[i].Y)).ToList();
  4.  
  5.             if (calc.Any(p => p == 0))
  6.             {
  7.                 return true;
  8.             }
  9.  
  10.             for (int i = 1; i < calc.Count(); i++)
  11.             {
  12.                 if (calc[i] * calc[i - 1] < 0)
  13.                 {
  14.                     return false;
  15.                 }
  16.             }
  17.             return true;
  18.         }
  19.  
Title: Re: Point Inside Polyline
Post by: nobody on July 24, 2016, 05:20:56 AM
Oy...nevermind... just needed a few minutes more :/ Found it here...

https://www.theswamp.org/index.php?topic=39031.msg515049#msg515049



Title: Re: Point Inside Polyline
Post by: kdub_nz on July 24, 2016, 05:27:01 AM
:)

I just put that link on my clipboard to post here.
Gilles does some nice work !
Title: Re: Point Inside Polyline
Post by: nobody on July 24, 2016, 05:51:06 AM
:)

I just put that link on my clipboard to post here.
Gilles does some nice work !

Most definitely agree