Author Topic: Point Inside Polyline  (Read 1943 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Point Inside Polyline
« 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.  

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Point Inside Polyline
« Reply #1 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




kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Point Inside Polyline
« Reply #2 on: July 24, 2016, 05:27:01 AM »
:)

I just put that link on my clipboard to post here.
Gilles does some nice work !
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Point Inside Polyline
« Reply #3 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