1 /*
2  *  This file is part of the "GKMap".
3  *  GKMap project borrowed from GMap.NET (by radioman).
4  *
5  *  Copyright (C) 2009-2018 by radioman (email@radioman.lt).
6  *  This program is licensed under the FLAT EARTH License.
7  */
8 
9 using System.Collections.Generic;
10 
11 namespace GKMap.MapObjects
12 {
13     /// <summary>
14     /// GKMap polygon.
15     /// </summary>
16     public abstract class MapPolygon : MapFigure
17     {
MapPolygon(string name, IEnumerable<PointLatLng> points)18         protected MapPolygon(string name, IEnumerable<PointLatLng> points)
19             : base(name, points)
20         {
21             fVisible = true;
22             IsHitTestVisible = false;
23         }
24 
UpdateLocalPosition()25         protected override void UpdateLocalPosition()
26         {
27             if (fVisible) {
28                 Overlay.Control.Core.UpdatePolygonLocalPosition(this);
29             } else {
30                 if (Overlay.Control.IsMouseOverPolygon) {
31                     Overlay.Control.IsMouseOverPolygon = false;
32                     Overlay.Control.RestoreCursorOnLeave();
33                 }
34             }
35         }
36 
37         /// <summary>
38         /// checks if point is inside the polygon,
39         /// </summary>
40         /// <param name="p"></param>
41         /// <returns></returns>
IsInsideLatLng(PointLatLng p)42         public bool IsInsideLatLng(PointLatLng p)
43         {
44             int count = Points.Count;
45 
46             if (count < 3) {
47                 return false;
48             }
49 
50             bool result = false;
51 
52             for (int i = 0, j = count - 1; i < count; i++) {
53                 var p1 = Points[i];
54                 var p2 = Points[j];
55 
56                 if (p1.Lat < p.Lat && p2.Lat >= p.Lat || p2.Lat < p.Lat && p1.Lat >= p.Lat) {
57                     if (p1.Lng + (p.Lat - p1.Lat) / (p2.Lat - p1.Lat) * (p2.Lng - p1.Lng) < p.Lng) {
58                         result = !result;
59                     }
60                 }
61                 j = i;
62             }
63             return result;
64         }
65     }
66 }
67