Exemplo n.º 1
0
        public MyPolygon CopyWithoutDrawing()
        {
            List <MyPoint> verticleList = new List <MyPoint>();
            MyPolygon      p            = new MyPolygon();

            p.Edges = new List <MyEdge>();
            //add to verticleList
            foreach (var edge in Edges)
            {
                verticleList.Add(edge.first.CopyWithoutDrawing());
            }
            //create edges list from verticleList
            for (int i = 0; i < verticleList.Count; i++)
            {
                var edge = new MyEdge(verticleList[i], verticleList[(i + 1) % verticleList.Count]);
                p.Edges.Add(edge);
            }

            //copying relations
            for (int i = 0; i < Edges.Count; i++)
            {
                p.Edges[i].relationType = Edges[i].relationType;
                if (Edges[i].relationType != RelationType.None)
                {
                    var index = Edges.FindIndex(x => Object.ReferenceEquals(x, Edges[i].relationEdge) == true);
                    p.Edges[i].relationEdge = p.Edges[index];
                }
            }
            return(p);
        }
Exemplo n.º 2
0
        private void ClearAll()
        {
            //delete currently drawing polygon
            CurrentlyDrawingPolygon?.DeleteDrawing();
            if (CurrentlyDrawingPolygon != null)
            {
                CurrentLine?.DeleteDrawing();
            }
            CurrentLine             = null;
            CurrentlyDrawingPolygon = null;
            PolygonDrawing          = false;

            //delete polygons
            foreach (var pol in Polygons)
            {
                pol.Value.DeleteDrawing();
            }

            //clear RelationIcon
            RelationIcon.RelationCounter.Clear();

            //clear variables
            ClearVariables();
            //set pointerbutton
            PointerButton.IsChecked = true;
        }
Exemplo n.º 3
0
        private void GenerateSamplePolygon()
        {
            //TODO:
            //generate sample polygon
            var pointsList = new List <MyPoint>
            {
                new MyPoint(626d, 57d),
                new MyPoint(856d, 191d),
                new MyPoint(744d, 301d),
                new MyPoint(851d, 532d),
                new MyPoint(460d, 502d),
                new MyPoint(367d, 408d),
                new MyPoint(427d, 149d)
            };

            MyPolygon polygon = new MyPolygon(pointsList.Last(), PolygonCanvas);

            foreach (var point in pointsList)
            {
                polygon.AddVerticleAndDraw(point);
            }

            Polygons.Add(PolygonNumber, polygon);


            polygon.Edges[0].relationEdge = polygon.Edges[3];
            polygon.Edges[3].relationEdge = polygon.Edges[0];
            polygon.Edges[0].relationType = RelationType.Perpendicular;
            polygon.Edges[3].relationType = RelationType.Perpendicular;

            polygon.ApplyRelationChanges(polygon.Edges[0]);

            polygon.Edges[0].relationIcon = new RelationIcon(polygon.Edges[0], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas);
            polygon.Edges[3].relationIcon = new RelationIcon(polygon.Edges[3], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas);

            polygon.Edges[1].relationEdge = polygon.Edges[6];
            polygon.Edges[6].relationEdge = polygon.Edges[1];
            polygon.Edges[1].relationType = RelationType.Equal;
            polygon.Edges[6].relationType = RelationType.Equal;

            polygon.ApplyRelationChanges(polygon.Edges[1]);

            polygon.Edges[1].relationIcon = new RelationIcon(polygon.Edges[1], RelationType.Equal, PolygonNumber, polygon, PolygonCanvas);
            polygon.Edges[6].relationIcon = new RelationIcon(polygon.Edges[6], RelationType.Equal, PolygonNumber, polygon, PolygonCanvas);

            polygon.Edges[2].relationEdge = polygon.Edges[5];
            polygon.Edges[5].relationEdge = polygon.Edges[2];
            polygon.Edges[2].relationType = RelationType.Perpendicular;
            polygon.Edges[5].relationType = RelationType.Perpendicular;

            polygon.ApplyRelationChanges(polygon.Edges[2]);

            polygon.Edges[2].relationIcon = new RelationIcon(polygon.Edges[2], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas);
            polygon.Edges[5].relationIcon = new RelationIcon(polygon.Edges[5], RelationType.Perpendicular, PolygonNumber, polygon, PolygonCanvas);

            PolygonNumber++;
        }
Exemplo n.º 4
0
 private void RedrawPolygon(MyPolygon polygon)
 {
     for (int i = 0; i < Edges.Count; i++)
     {
         var edge    = Edges[i];
         var newEdge = polygon.Edges[i];
         edge.first.Move(newEdge.first.X, newEdge.first.Y);
     }
     foreach (var edge in Edges)
     {
         edge.MoveWithPoints();
     }
 }
Exemplo n.º 5
0
        private void ClearVariables()
        {
            Polygons.Clear();
            ProgramMode = Mode.Pointer;
            //Pointer Variables
            IsDraggingOn          = false;
            CurrentDragObjectType = DragObjectType.Nothing;
            DragStartingPoint     = new Point();
            DragPolygonId         = -1;
            DragObject            = null;

            //Drawing Variables
            PolygonDrawing          = false;
            CurrentlyDrawingPolygon = null;
            PolygonNumber           = 0;
            CurrentLine             = null;

            //Adding Relation Variables
            RelationPolygonId    = -1;
            RelationSelectedEdge = null;
        }
Exemplo n.º 6
0
 private bool ClearUnfinishedPolygon()
 {
     if (CurrentlyDrawingPolygon != null)
     {
         MessageBoxResult ans = MessageBox.Show("If you change mode during drawing,\n" +
                                                "currently drawn polygon will be deleted!\n" +
                                                "Do you want to continue drawing?", Globals.WindowName, MessageBoxButton.YesNo, MessageBoxImage.Question);
         if (ans == MessageBoxResult.Yes)
         {
             return(false);
         }
         else
         {
             CurrentlyDrawingPolygon.DeleteDrawing();
             CurrentLine?.DeleteDrawing();
             CurrentLine             = null;
             CurrentlyDrawingPolygon = null;
             PolygonDrawing          = false;
         }
     }
     return(true);
 }
Exemplo n.º 7
0
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas  currentCanvas        = sender as Canvas;
            Point   CurrentMousePosition = e.GetPosition(currentCanvas);
            MyPoint p = new MyPoint(CurrentMousePosition.X, CurrentMousePosition.Y);

            switch (ProgramMode)
            {
            case Mode.Pointer:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if point hit
                        if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true)
                        {
                            CurrentDragObjectType = DragObjectType.Verticle;
                            IsDraggingOn          = true;
                            DragPolygonId         = pol.Key;
                            DragObject            = edge.first;
                            return;
                        }
                    }
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            CurrentDragObjectType = DragObjectType.Edge;
                            IsDraggingOn          = true;
                            DragPolygonId         = pol.Key;
                            DragObject            = edge;
                            DragStartingPoint     = CurrentMousePosition;
                            return;
                        }
                    }
                    //check if inside Polygon
                    if (pol.Value.IsPointInside(CurrentMousePosition) == true)
                    {
                        CurrentDragObjectType = DragObjectType.Polygon;
                        IsDraggingOn          = true;
                        DragPolygonId         = pol.Key;
                        DragObject            = pol.Value;
                        DragStartingPoint     = CurrentMousePosition;
                        return;
                    }
                }
                CurrentDragObjectType = DragObjectType.Nothing;
            }
            break;

            case Mode.Draw:
            {
                if (PolygonDrawing == false)
                {
                    MyPolygon polygon = new MyPolygon(p, currentCanvas);
                    CurrentlyDrawingPolygon = polygon;
                    PolygonDrawing          = true;
                    CurrentLine?.DeleteDrawing();
                    CurrentLine = Draw.SimpleEdge(CurrentMousePosition, CurrentMousePosition, currentCanvas);
                }
                else
                {
                    var DrawResult = CurrentlyDrawingPolygon.AddVerticleAndDraw(p);
                    switch (DrawResult)
                    {
                    case PolygonDrawResult.DrawFinished:
                        CurrentLine.SetFirstPoint(CurrentMousePosition);

                        Polygons.Add(PolygonNumber, CurrentlyDrawingPolygon);

                        CurrentLine.DeleteDrawing();
                        PolygonDrawing          = false;
                        CurrentlyDrawingPolygon = null;
                        PolygonNumber++;
                        break;

                    case PolygonDrawResult.NotEnoughEdges:
                        MessageBox.Show("Not enough edges to finish polygon", Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        break;

                    case PolygonDrawResult.DrawInProgress:
                        CurrentLine.SetFirstPoint(CurrentMousePosition);
                        break;

                    default:
                        break;
                    }
                }
            }
            break;

            case Mode.AddMiddleVerticle:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            pol.Value.AddMiddleVerticleOnEdge(edge);
                            return;
                        }
                    }
                }
            }
            break;

            case Mode.AddEqualRelation:
            case Mode.AddPerpendicularRelation:
            {
                RelationType relationType;
                if (ProgramMode == Mode.AddEqualRelation)
                {
                    relationType = RelationType.Equal;
                }
                else
                {
                    relationType = RelationType.Perpendicular;
                }
                if (RelationPolygonId == -1)
                {
                    foreach (var pol in Polygons)
                    {
                        foreach (var edge in pol.Value.Edges)
                        {
                            //check if edge hit
                            if (edge.relationType == RelationType.None)
                            {
                                if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                                {
                                    edge.SelectEdge();
                                    RelationSelectedEdge = edge;
                                    RelationPolygonId    = pol.Key;
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (var edge in Polygons[RelationPolygonId].Edges)
                    {
                        //check if edge hit
                        if (edge.relationType == RelationType.None)
                        {
                            if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                            {
                                if (Object.ReferenceEquals(edge, RelationSelectedEdge) == false)
                                {
                                    edge.relationEdge = RelationSelectedEdge;
                                    RelationSelectedEdge.relationEdge = edge;
                                    edge.relationType = relationType;
                                    RelationSelectedEdge.relationType = relationType;
                                    var result = Polygons[RelationPolygonId].ApplyRelationChanges(edge);
                                    if (result == true)
                                    {
                                        edge.relationIcon = new RelationIcon(edge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas);
                                        RelationSelectedEdge.relationIcon = new RelationIcon(RelationSelectedEdge, relationType, RelationPolygonId, Polygons[RelationPolygonId], currentCanvas);
                                    }
                                    else
                                    {
                                        edge.relationEdge = null;
                                        RelationSelectedEdge.relationEdge = null;
                                        edge.relationType = RelationType.None;
                                        RelationSelectedEdge.relationType = RelationType.None;
                                    }
                                }
                                RelationSelectedEdge.UnselectEdge();
                                RelationSelectedEdge = null;
                                RelationPolygonId    = -1;
                                return;
                            }
                        }
                    }
                }
            }
            break;

            case Mode.DeleteVerticleOrRelation:
            {
                foreach (var pol in Polygons)
                {
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if point hit
                        if (MyPoint.AreNear(edge.first, CurrentMousePosition, Globals.VerticleClickRadiusSize) == true)
                        {
                            if (Polygons[pol.Key].DeleteVerticle(edge.first) == true)
                            {
                                Polygons[pol.Key] = null;
                                Polygons.Remove(pol.Key);
                            }
                            return;
                        }
                    }
                    foreach (var edge in pol.Value.Edges)
                    {
                        //check if edge hit
                        if (edge.IsNearPoint(CurrentMousePosition, Globals.LineClickDistance) == true)
                        {
                            edge.DeleteRelation();
                            return;
                        }
                    }
                }
            }
            break;

            default:
                break;
            }
            return;
        }
Exemplo n.º 8
0
        private (bool success, MyPolygon changedPolygon) FixRelationsMovingVerticle(MyEdge startingEdge)
        {
            MyPolygon copyPolygon       = this.CopyWithoutDrawing();
            var       startingEdgeIndex = copyPolygon.Edges.FindIndex(x => x == startingEdge);
            bool      endLoop           = false;
            bool      firstSuccess      = false;
            bool      secondSuccess     = false;

            //going right (list order)
            for (int i = 0; i < copyPolygon.Edges.Count && endLoop == false; i++)
            {
                var edge = copyPolygon.Edges[(i + startingEdgeIndex) % copyPolygon.Edges.Count];
                switch (edge.relationType)
                {
                case RelationType.Equal:
                    RelationFixer.FixEqualRelation(edge.first, edge.second, edge, ref endLoop, ref firstSuccess);
                    break;

                case RelationType.Perpendicular:
                    RelationFixer.FixPerpendicularRelation(edge.first, edge.second, edge.relationEdge.first, edge.relationEdge.second);
                    break;

                case RelationType.None:
                    endLoop      = true;
                    firstSuccess = true;
                    break;

                default:
                    break;
                }
            }
            if (firstSuccess == false)
            {
                return(false, null);
            }
            endLoop = false;
            //going left (no list order)
            for (int i = copyPolygon.Edges.Count - 1; i >= 0 && endLoop == false; i--)
            {
                var edge = copyPolygon.Edges[(i + startingEdgeIndex) % copyPolygon.Edges.Count];
                switch (edge.relationType)
                {
                case RelationType.Equal:
                    RelationFixer.FixEqualRelation(edge.second, edge.first, edge, ref endLoop, ref secondSuccess);
                    break;

                case RelationType.Perpendicular:
                    RelationFixer.FixPerpendicularRelation(edge.second, edge.first, edge.relationEdge.second, edge.relationEdge.first);
                    break;

                case RelationType.None:
                    endLoop       = true;
                    secondSuccess = true;
                    break;

                default:
                    break;
                }
            }
            if (secondSuccess == false)
            {
                return(false, null);
            }
            return(true, copyPolygon);
        }