namespace TelpoPush.Fence.Worker.Models.Fence { public static class RayCastingAlgorithm { public static bool IsWithin(GpsFencePoint pt, IList polygon, bool noneZeroMode) { int ptNum = polygon.Count(); if (ptNum < 3) { return false; } int j = ptNum - 1; bool oddNodes = false; int zeroState = 0; for (int k = 0; k < ptNum; k++) { var ptK = polygon[k]; var ptJ = polygon[j]; if (ptK.Latitude > pt.Latitude != ptJ.Latitude > pt.Latitude && pt.Longitude < (ptJ.Longitude - ptK.Longitude) * (pt.Latitude - ptK.Latitude) / (ptJ.Latitude - ptK.Latitude) + ptK.Longitude) { oddNodes = !oddNodes; if (ptK.Latitude > ptJ.Latitude) { zeroState++; } else { zeroState--; } } j = k; } return noneZeroMode ? zeroState != 0 : oddNodes; } } }