summaryrefslogtreecommitdiff
path: root/SimpleRadar_old/Sector.cs
blob: 770611c48b3a580775ee508b9d954047b2b1a5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SimpleRadar
{
    public class Sector
    {
        public static float Ratio = (float)(8);

        static public bool PointInPolygon(PointF p, Sector sect)
        {
            PointF p1, p2;

            bool inside = false;

            foreach (Slice slice in sect.Slices)
            {
                PointF[] poly = slice.Points;

                if (poly.Length < 3)
                {
                    return inside;
                }

                PointF oldPoint = new PointF(

                poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

                for (int i = 0; i < poly.Length; i++)
                {
                    PointF newPoint = new PointF(poly[i].X, poly[i].Y);

                    if (newPoint.X > oldPoint.X)
                    {
                        p1 = oldPoint;
                        p2 = newPoint;
                    }
                    else
                    {
                        p1 = newPoint;
                        p2 = oldPoint;
                    }

                    if ((newPoint.X < p.X) == (p.X <= oldPoint.X) && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X)
                                < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
                    {
                        inside = !inside;
                    }
                    oldPoint = newPoint;
                }
                if (inside == true) break;
            }
            return inside;
        }


        public bool IsSelected = false;

        public struct Slice
        {
            public int Floor;
            public int Ceiling;
            public PointF[] Points;
        }
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public Slice[] Slices;

        public override string ToString()
        {
            return _Name;
        }

        public void Draw(Graphics g, Pen p)
        {
            if (IsSelected)
                p = new Pen(Brushes.Red, 5.0f);

            PointF p1, p2;
            foreach (Slice s in Slices)
            {
                for (int i = 0; i < (s.Points.Length - 1); i++)
                {
                    p1 = s.Points[i] ;
                    p2 = s.Points[i + 1];
                    g.DrawLine(p, (float)PanAndZoomX(p1.X / Ratio), (float)PanAndZoomY(p1.Y / Ratio),
                        (float)PanAndZoomX(p2.X / Ratio), (float)PanAndZoomY(p2.Y / Ratio));
                }
            }
        }

        public Sector(sectorsSector raw)
        {
            _Name = raw.name;
            Slices = new Slice[raw.slice.Length];
            int indexSlice = 0;
            foreach (sectorsSectorSlice s in raw.slice)
            {
                Slices[indexSlice].Floor = s.floor;
                Slices[indexSlice].Ceiling = s.ceiling;

                Slices[indexSlice].Points = new PointF[s.vertex.Length];
                int indexVextex = 0;
                foreach (sectorsSectorSliceVertex v in s.vertex)
                {
                    Slices[indexSlice].Points[indexVextex] = MathCautra.ToCautra4(v.lat, v.lon);
                    indexVextex++;
                }
                indexSlice++;
            }
        }

        #region Delegate Pan And Zoom

        public delegate double PanAndZoom(double input);

        [NonSerialized]
        static public PanAndZoom PanAndZoomX;
        [NonSerialized]
        static public PanAndZoom PanAndZoomY;

        #endregion
    }
}