예제 #1
0
        public static IPolygon GetConvexHull(IPolygon polygon)
        {
            List <Point> newVerticesPositons = new List <Point>();

            Point[] points = polygon.Vertices.Select(v => v.Position).ToArray();

            int ymin = points[0].Y, min = 0;

            for (int i = 1; i < points.Length; i++)
            {
                int y = points[i].Y;

                if ((y < ymin) || (ymin == y &&
                                   points[i].X < points[min].X))
                {
                    ymin = points[i].Y;
                    min  = i;
                }
            }

            Point temp = points[0];

            points[0]   = points[min];
            points[min] = temp;

            Array.Sort(points, 1, points.Length - 1, new ConvexHullComparer(points[0]));

            int m = 1;

            for (int i = 1; i < points.Length; i++)
            {
                while (i < points.Length - 1 && Orientation(points[0], points[i], points[i + 1]) == 0)
                {
                    i++;
                }

                points[m] = points[i];
                m++;
            }

            if (m < 3)
            {
                return(null);
            }

            Stack <Point> S = new Stack <Point>();

            S.Push(points[0]);
            S.Push(points[1]);
            S.Push(points[2]);

            for (int i = 3; i < m; i++)
            {
                while (Orientation(NextToTop(S), S.Peek(), points[i]) != 2)
                {
                    S.Pop();
                }

                S.Push(points[i]);
            }

            while (S.Count != 0)
            {
                newVerticesPositons.Add(S.Pop());
            }

            List <EdgeType> edgetypes = new List <EdgeType>();

            for (int i = 0; i < newVerticesPositons.Count; i++)
            {
                edgetypes.Add(EdgeType.Normal);
            }

            Polygon p = PolygonCreator.CreatePolygon(newVerticesPositons, edgetypes);

            foreach (var edge in polygon.Edges)
            {
                Edge ed = p.Edges.Find(e =>
                                       (e.Endpoints[0].Position == edge.Endpoints[0].Position && e.Endpoints[1].Position == edge.Endpoints[1].Position) ||
                                       (e.Endpoints[1].Position == edge.Endpoints[0].Position && e.Endpoints[0].Position == edge.Endpoints[1].Position));
                if (ed != null)
                {
                    p.ChangeEdgeType(ed, edge.Type, edge.Length);
                }
            }

            return(p);
        }
예제 #2
0
 private void CreateRectangle(object sender, EventArgs e)
 {
     polygons.Add(PolygonCreator.GetSampleRectangle());
     FakeButton.Focus();
     drawingArea.Refresh();
 }
예제 #3
0
 private void CreateCustomPolygon(object sender, EventArgs e)
 {
     polygons.Add(PolygonCreator.GetCustom());
     FakeButton.Focus();
     drawingArea.Refresh();
 }
예제 #4
0
        public PolygonEditor()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;

            polygons = new List <IPolygon> {
                PolygonCreator.GetCustom()
            };
            Vertice.ClickRadius = 10;
            Edge.ClickDistance  = 5;

            inputHandler = GetStandardInputHandler();

            standardPolygonDrawer = new PolygonDrawer
            {
                EdgeColor              = Color.LightGray,
                EdgeThickness          = 2,
                VerticeBorderColor     = Color.LightGray,
                VerticeInsideColor     = Color.FromArgb(28, 28, 28),
                VerticeBorderThickness = 1,
                VerticeRadius          = 10,
                IconLineThickness      = 1,
                IconRadius             = 12,
                IconColor              = Color.Yellow,
                IconFontName           = "Arial",
                IconFontSize           = 12
            };

            selectedElementDrawer = new PolygonDrawer
            {
                EdgeColor              = Color.Red,
                EdgeThickness          = standardPolygonDrawer.EdgeThickness,
                VerticeBorderColor     = Color.Red,
                VerticeInsideColor     = standardPolygonDrawer.VerticeInsideColor,
                VerticeBorderThickness = standardPolygonDrawer.VerticeBorderThickness,
                VerticeRadius          = standardPolygonDrawer.VerticeRadius,
                IconLineThickness      = standardPolygonDrawer.IconLineThickness,
                IconRadius             = standardPolygonDrawer.IconRadius,
                IconColor              = standardPolygonDrawer.IconColor,
                IconFontName           = standardPolygonDrawer.IconFontName,
                IconFontSize           = standardPolygonDrawer.IconFontSize
            };

            #region Events pinning

            drawingArea.Paint += Draw;
            AttachInputHandlerEventsToDrawingArea();

            MakeVerticalButton.Click      += ChangeSelectedEdgeToVertical;
            MakeHorizontalButton.Click    += ChangeSelectedEdgeToHorizontal;
            MakeFixedLengthButton.Click   += ChangeSelectedEdgeToFixedLength;
            ChangeLengthButton.Click      += ChangeSelectedEdgeLength;
            RemoveConstraintsButton.Click += RemoveConstraintsFromSelectedEdge;
            DeleteVerticeButton.Click     += DeleteSelectedVertice;
            SplitButton.Click             += SplitSelectedEdge;

            CreateTriangleButton.Click  += CreateTriangle;
            CreateRectangleButton.Click += CreateRectangle;
            CreateCustomButton.Click    += CreateCustomPolygon;
            DeletePolygonButton.Click   += DeleteSelectedPolygon;
            StartDrawingButton.Click    += StartDrawingPolygon;
            StopDrawingButton.Click     += StopDrawingPolygon;
            ConvexHullButton.Click      += ChangeCurrentPolygonToConvexHull;

            #endregion

            UpdateButtons();
        }