using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Xml.Serialization; using System.IO; namespace Data { public class Sector { #region Attributes and structure public static float Ratio = Config.coordinatesRatio; private bool _isSelected = false; private string _Name; public Slice[] Slices; public struct Slice { public int Floor; public int Ceiling; public PointF[] Points; } #endregion #region Constructor public Sector(sectorsSector raw) { _Name = raw.name; Slices = new Slice[raw.slice.Length]; this._isSelected = false; 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++; } } #endregion #region Methods 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 override string ToString() { return _Name; } public void Draw(Graphics g, Boolean drawSectorsNames) { Pen drawLinesPen = new Pen(Config.sectorLinesColor, 0.5f); SolidBrush fillPolygonBrush = null; if (this.isSectorSelected) { fillPolygonBrush = new SolidBrush(Config.sectorSelectedFillColor); } else { fillPolygonBrush = new SolidBrush(Config.sectorFillColor); } //PointF p1, p2; PointF pCurr = new PointF(0, 0), pSuiv = new PointF(0, 0), pTransf = new PointF(0, 0); PointF[] tabPoints; int sliceCeiling; foreach (Slice s in Slices) { sliceCeiling = s.Ceiling; if (sliceCeiling > Config.sectorHighFloorValue) { if (drawSectorsNames) { PointF sectorNamePos = s.Points[0]; SolidBrush sectorNameBrush = new SolidBrush(Config.sectorNameColor); g.DrawString(this.Name, Config.sectorNameFont, sectorNameBrush, new PointF((float)PanAndZoomX(sectorNamePos.X / Ratio), (float)PanAndZoomY(sectorNamePos.Y / Ratio))); } tabPoints = new PointF[s.Points.Length-1 ]; 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)); pCurr = s.Points[i]; pSuiv = s.Points[i + 1]; g.DrawLine(drawLinesPen, (float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio), (float)PanAndZoomX(pSuiv.X / Ratio), (float)PanAndZoomY(pSuiv.Y / Ratio)); pTransf = new PointF((float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio)); tabPoints[i] = pTransf; } //pSuiv = s.Points[0]; //g.DrawLine(drawLinesPen, (float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio), (float)PanAndZoomX(pSuiv.X / Ratio), (float)PanAndZoomY(pSuiv.Y / Ratio)); g.FillPolygon(fillPolygonBrush, tabPoints, System.Drawing.Drawing2D.FillMode.Alternate); } } } #endregion #region Getters and Setters public bool isSectorSelected { get { return _isSelected; } set { _isSelected = value; } } public string Name { get { return _Name; } set { _Name = value; } } #endregion #region Delegate Pan And Zoom public delegate double PanAndZoom(double input); [NonSerialized] static public PanAndZoom PanAndZoomX; [NonSerialized] static public PanAndZoom PanAndZoomY; #endregion #region createList public static Sector[] LoadSectorsFile(string fileName) { XmlSerializer serializer = new XmlSerializer(typeof(sectors)); TextReader reader = new StreamReader(fileName); Sector[] Sectors = null; if (reader != null) { sectors sectorsXml = (sectors)serializer.Deserialize(reader); reader.Close(); //Sectors = sectorsXml.GetPointList(); Sectors = new Sector[sectorsXml.Items.Length]; int indexSec = 0; foreach (sectorsSector s in sectorsXml.Items) { Sectors[indexSec++] = new Sector(s); } } return Sectors; } #endregion } }