Exemple #1
0
        //private void MakeVertical(Edge edge)
        //{
        //    int index = MyPolygon.ListOfEdges.FindIndex(e => e.Equals(edge));
        //    if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is VerticalEdge ||
        //        MyPolygon.ListOfEdges[index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1] is VerticalEdge)
        //        return;
        //    if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is NormalEdge)
        //    {
        //        VerticalEdge vEdge = new VerticalEdge(edge.Start.Location, edge.End.Location);
        //        MyPolygon.ListOfEdges.RemoveAt(index);
        //        MyPolygon.ListOfEdges.Insert(index, vEdge);
        //        MyPolygon.ListOfVertexes.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
        //        MyPolygon.ListOfVertexes.Insert(index + 1, vEdge.End);
        //        NormalEdge nEdge = new NormalEdge(vEdge.End.Location, MyPolygon.ListOfVertexes[index + 2 >= MyPolygon.ListOfVertexes.Count ? index + 2 - MyPolygon.ListOfVertexes.Count : index + 2].Location);
        //        MyPolygon.ListOfEdges.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
        //        MyPolygon.ListOfEdges.Insert(index + 1, nEdge);
        //    }
        //    if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is HorizontalEdge)
        //    {
        //        VerticalEdge vEdge = new VerticalEdge(edge.Start.Location, edge.End.Location);
        //        MyPolygon.ListOfEdges.RemoveAt(index);
        //        MyPolygon.ListOfEdges.Insert(index, vEdge);
        //        MyPolygon.ListOfVertexes.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
        //        MyPolygon.ListOfVertexes.Insert(index + 1, vEdge.End);
        //        HorizontalEdge hEdge = new HorizontalEdge(vEdge.End.Location, MyPolygon.ListOfVertexes[index + 2 >= MyPolygon.ListOfVertexes.Count ? index + 2 - MyPolygon.ListOfVertexes.Count : index + 2].Location);
        //        MyPolygon.ListOfEdges.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
        //        MyPolygon.ListOfEdges.Insert(index + 1, vEdge);
        //    }
        //}

        private void MakeHorizontal(Edge edge)
        {
            int index = MyPolygon.ListOfEdges.FindIndex(e => e.Equals(edge));

            if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is HorizontalEdge ||
                MyPolygon.ListOfEdges[index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1] is HorizontalEdge)
            {
                return;
            }
            if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is NormalEdge)
            {
                HorizontalEdge hEdge = new HorizontalEdge(edge.Start.Location, edge.End.Location);
                MyPolygon.ListOfEdges.RemoveAt(index);
                MyPolygon.ListOfEdges.Insert(index, hEdge);
                MyPolygon.ListOfVertexes.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
                MyPolygon.ListOfVertexes.Insert(index + 1, hEdge.End);
                NormalEdge nEdge = new NormalEdge(hEdge.End.Location, MyPolygon.ListOfVertexes[index + 2 >= MyPolygon.ListOfVertexes.Count ? index + 2 - MyPolygon.ListOfVertexes.Count : index + 2].Location);
                MyPolygon.ListOfEdges.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
                MyPolygon.ListOfEdges.Insert(index + 1, nEdge);
            }
            if (MyPolygon.ListOfEdges[index + 1 == MyPolygon.ListOfEdges.Count ? 0 : index + 1] is VerticalEdge)
            {
                HorizontalEdge hEdge = new HorizontalEdge(edge.Start.Location, edge.End.Location);
                MyPolygon.ListOfEdges.RemoveAt(index);
                MyPolygon.ListOfEdges.Insert(index, hEdge);
                MyPolygon.ListOfVertexes.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
                MyPolygon.ListOfVertexes.Insert(index + 1, hEdge.End);
                VerticalEdge vEdge = new VerticalEdge(hEdge.End.Location, MyPolygon.ListOfVertexes[index + 2 >= MyPolygon.ListOfVertexes.Count ? index + 2 - MyPolygon.ListOfVertexes.Count : index + 2].Location);
                MyPolygon.ListOfEdges.RemoveAt(index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1);
                MyPolygon.ListOfEdges.Insert(index + 1, vEdge);
            }
        }
Exemple #2
0
 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
 {
     if (DrawPolygonButton.Checked && !IsPolygonCreated)
     {
         if (MyPolygon == null)
         {
             return;
         }
         if (MyPolygon.ListOfVertexes.Count != 0)
         {
             NewMouseLocation = e.Location;
             DrawArea.Dispose();
             DrawArea = new Bitmap(pictureBox1.Width, pictureBox1.Height);
             DrawAll(Color.Black);
             DrawLine(new NormalEdge(MyPolygon.ListOfVertexes.Last().Location, NewMouseLocation));
         }
     }
     if (MovePolygonButton.Checked && IsPolygonCreated)
     {
         if (!MouseDown)
         {
             return;
         }
         NewMouseLocation = e.Location;
         int     xDiff = NewMouseLocation.X - MouseLocation.X;
         int     yDiff = NewMouseLocation.Y - MouseLocation.Y;
         Polygon p     = new Polygon();
         foreach (Vertex v in MyPolygon.ListOfVertexes)
         {
             try
             {
                 p.ListOfVertexes.Add(new Vertex(v.Location.X + xDiff, v.Location.Y + yDiff));
             }
             catch (ArgumentOutOfRangeException)
             {
             }
         }
         p.MovedEdges(xDiff, yDiff, MyPolygon.ListOfEdges);
         MyPolygon = p;
         DrawAll(Color.Black);
         MouseLocation = NewMouseLocation;
     }
     if (MoveVertexButton.Checked && IsPolygonCreated)
     {
         if (!MouseDown)
         {
             return;
         }
         Vertex v = MyPolygon.ClosestVertex(MouseLocation);
         if (v == null)
         {
             return;
         }
         NewMouseLocation = e.Location;
         int index = MyPolygon.ListOfVertexes.FindIndex(ver => ver.Equals(v));
         int xDiff = NewMouseLocation.X - MouseLocation.X;
         int yDiff = NewMouseLocation.Y - MouseLocation.Y;
         if (MyPolygon.ListOfEdges[index] is NormalEdge &&
             MyPolygon.ListOfEdges[index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1] is NormalEdge)
         {
             v.Location.X += xDiff;
             v.Location.Y += yDiff;
             Edge edge = new NormalEdge(
                 MyPolygon.ListOfVertexes[index - 1 < 0 ? MyPolygon.ListOfVertexes.Count - 1 : index - 1]
                 .Location, v.Location);
             MyPolygon.ListOfEdges.RemoveAt(index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1);
             MyPolygon.ListOfEdges.Insert(index - 1 < 0 ? MyPolygon.ListOfEdges.Count : index - 1, edge);
             edge = new NormalEdge(v.Location,
                                   MyPolygon.ListOfVertexes[index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1].Location);
             MyPolygon.ListOfEdges.RemoveAt(index);
             MyPolygon.ListOfEdges.Insert(index, edge);
         }
         else if (MyPolygon.ListOfEdges[index] is HorizontalEdge ||
                  MyPolygon.ListOfEdges[
                      index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1] is HorizontalEdge)
         {
             v.Location.X += xDiff;
             if (MyPolygon.ListOfEdges[index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1] is HorizontalEdge)
             {
                 HorizontalEdge hEdge = new HorizontalEdge(MyPolygon
                                                           .ListOfVertexes[index - 1 < 0 ? MyPolygon.ListOfVertexes.Count - 1 : index - 1]
                                                           .Location, v.Location);
                 MyPolygon.ListOfEdges.RemoveAt(index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1);
                 MyPolygon.ListOfEdges.Insert(index - 1 < 0 ? MyPolygon.ListOfEdges.Count : index - 1, hEdge);
                 Edge edge = new NormalEdge(v.Location,
                                            MyPolygon.ListOfVertexes[index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1]
                                            .Location);
                 MyPolygon.ListOfEdges.RemoveAt(index);
                 MyPolygon.ListOfEdges.Insert(index, edge);
             }
             if (MyPolygon.ListOfEdges[index] is HorizontalEdge)
             {
                 Edge edge = new NormalEdge(
                     MyPolygon.ListOfVertexes[index - 1 < 0 ? MyPolygon.ListOfVertexes.Count - 1 : index - 1]
                     .Location, v.Location);
                 MyPolygon.ListOfEdges.RemoveAt(index - 1 < 0 ? MyPolygon.ListOfEdges.Count - 1 : index - 1);
                 MyPolygon.ListOfEdges.Insert(index - 1 < 0 ? MyPolygon.ListOfEdges.Count : index - 1, edge);
                 HorizontalEdge hEdge = new HorizontalEdge(v.Location,
                                                           MyPolygon.ListOfVertexes[index + 1 == MyPolygon.ListOfVertexes.Count ? 0 : index + 1]
                                                           .Location);
                 MyPolygon.ListOfEdges.RemoveAt(index);
                 MyPolygon.ListOfEdges.Insert(index, hEdge);
             }
         }
         DrawAll(Color.Black);
         MouseLocation = NewMouseLocation;
     }
 }