// Common.cs

namespace ReverseGeocode;

public class Common
{
	public static bool IsCoordInsideArrayOfCoords(List<double[]> polygon, double[] p)
	{
		int counter = 0;
		int i, N = polygon.Count;
		double xinters;
		double[] p1, p2;
		const int x = 0, y = 1;

		p1 = polygon[0];
		for (i = 1; i <= N; i++)
		{
			p2 = polygon[i % N];
			if (p[y] > Math.Min(p1[y], p2[y]))
			{
				if (p[y] <= Math.Max(p1[y], p2[y]))
				{
					if (p[x] <= Math.Max(p1[x], p2[x]))
					{
						if (p1[y] != p2[y])
						{
							xinters = (p[y] - p1[y]) * (p2[x] - p1[x]) / (p2[y] - p1[y]) + p1[x];
							if (p1[x] == p2[x] || p[x] <= xinters)
								counter++;
						}
					}
				}
			}
			p1 = p2;
		}

		return (counter % 2 != 0);
	}
}
