示例#1
0
        public void Connect(CGraphEdge edge)
        {
            Pen          pen = new Pen(Brushes.Black, 2);
            int          z;
            CGraphVertex top1 = edge.start;
            CGraphVertex top2 = edge.end;

            if (top1.X < top2.X)
            {
                z = 1;
            }
            else
            {
                z = -1;
            }
            double x1 = z * 11 / Math.Sqrt(1 + Math.Pow((top2.Y - top1.Y), 2) / Math.Pow((top2.X - top1.X), 2)) +
                        top1.X;
            double x2 = (-z) * 11 / Math.Sqrt(1 + Math.Pow((top2.Y - top1.Y), 2) / Math.Pow((top2.X - top1.X), 2)) +
                        top2.X;
            double y1 = ((x1 - top1.X) * (top2.Y - top1.Y)) / (top2.X - top1.X) + top1.Y;
            double y2 = ((x2 - top2.X) * (top2.Y - top1.Y)) / (top2.X - top1.X) + top2.Y;

            edge.X = new Point((int)x1, (int)y1);
            edge.Y = new Point((int)x2, (int)y2);

            if (edge.EdgeColor != Color.Transparent)
            {
                g.Graphics.DrawLine(new Pen(edge.EdgeColor, 6), new PointF((int)x1, (int)y1),
                                    new PointF((int)x2, (int)y2));
            }

            if (edge.Highlight)
            {
                Pen    pen2 = new Pen(Color.Red, 2);
                double len  = Math.Sqrt((y1 - y2) * (y1 - y2) + (x2 - x1) * (x2 - x1));
                double nx   = 5 * (y1 - y2) / (len); //вектор нормали с длинной 3
                double ny   = 5 * (x2 - x1) / (len);
                g.Graphics.DrawLine(pen2, new PointF((int)(x1 + nx), (int)(y1 + ny)),
                                    new PointF((int)(x2 + nx), (int)(y2 + ny)));
                g.Graphics.DrawLine(pen2, new PointF((int)(x1 - nx), (int)(y1 - ny)),
                                    new PointF((int)(x2 - nx), (int)(y2 - ny)));
                //g.Graphics.DrawLine(pen2, new PointF((int) x1, (int) y1), new PointF((int) x2, (int) y2));
            }

            g.Graphics.DrawLine(pen, new PointF((int)x1, (int)y1), new PointF((int)x2, (int)y2));
            if (isDirectedGraph)
            {
                var angle = Math.Atan2(y2 - y1, x2 - x1);
                var p2    = new Pen(pen.Color, 6)
                {
                    EndCap = LineCap.ArrowAnchor
                };
                g.Graphics.DrawLine(p2, (float)x2, (float)y2, (float)x2 + (float)(Math.Cos(angle)),
                                    (float)y2 + (float)(Math.Sin(angle)));
            }

            panel1.Invalidate();
        }
示例#2
0
 private void DeleteEdge(CGraphEdge edge)
 {
     for (int i = 0; i < graph.Edges.Count; i++)
     {
         if (graph.Edges[i] == edge)
         {
             graph.Edges.RemoveAt(i);
         }
     }
 }
示例#3
0
        public CGraphVertex FindEndTop(CGraphEdge edge)
        {
            foreach (var t in graph.Tops)
            {
                if (Math.Abs(edge.end.X - t.X) < 1E-6 && Math.Abs(edge.end.Y - t.Y) < 1E-6)
                {
                    return(t);
                }
            }

            return(null);
        }
示例#4
0
        public CGraphEdge FindMinEdge(List <CGraphEdge> extraEdges)
        {
            CGraphEdge minEdge = null;

            for (int i = 0; i < extraEdges.Count; i++)
            {
                if (minEdge == null)
                {
                    minEdge = extraEdges[i];
                }
            }
            return(minEdge);
        }
示例#5
0
 public void Clear()
 {
     numberOfAction             = 0;
     chosenEdges                = new List <CGraphEdge>();
     chosenTops                 = new List <CGraphVertex>();
     queue                      = new Queue <CGraphVertex>();
     tGraph                     = new CGraph();
     stack                      = new Stack <CGraphVertex>();
     order                      = new Stack <CGraphVertex>();
     connectetComponentsCount   = 0;
     toolStripStatusLabel3.Text = "";
     toolStripStatusLabel4.Text = "";
     extraEdges                 = null;
     toAdd                      = null;
     startTop                   = null;
 }
示例#6
0
        public CGraphVertex OtherEnd(List <CGraphVertex> tops, CGraphEdge edge)
        {
            for (int i = 0; i < tops.Count; i++)
            {
                if (tops[i] == edge.start)
                {
                    return(edge.end);
                }
                if (tops[i] == edge.end)
                {
                    return(edge.start);
                }
            }

            return(null);
        }
示例#7
0
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (ifDown)
                {
                    isMoving = false;
                    ifDown   = false;
                    move.X   = e.X;
                    move.Y   = e.Y;
                }
            }
            else if (lineToCursor != null && checkBox1.Enabled && e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < graph.Tops.Count; i++)
                {
                    if (e.X >= graph.Tops[i].X - rad && e.X <= graph.Tops[i].X + rad &&
                        e.Y >= graph.Tops[i].Y - rad &&
                        e.Y <= graph.Tops[i].Y + rad)
                    {
                        if (!IfEdgeExist(connect, graph.Tops[i]) && connect != graph.Tops[i])
                        {
                            graph.AddEdge(lineToCursor.start, graph.Tops[i]);
                        }

                        panel1.Invalidate();
                        break;
                    }
                }

                graph.Edges.Remove(lineToCursor);
                if (connect != null)
                {
                    connect.Highlight = false;
                }
                connect      = null;
                lineToCursor = null;
            }
        }
示例#8
0
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                for (int i = 0; i < graph.Tops.Count; i++)
                {
                    if (e.X >= graph.Tops[i].X - rad && e.X <= graph.Tops[i].X + rad && e.Y >= graph.Tops[i].Y - rad &&
                        e.Y <= graph.Tops[i].Y + rad)
                    {
                        move   = graph.Tops[i];
                        ifDown = true;
                        break;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right && checkBox1.Enabled)
            {
                for (int i = 0; i < graph.Tops.Count; i++)
                {
                    if (e.X >= graph.Tops[i].X - rad && e.X <= graph.Tops[i].X + rad &&
                        e.Y >= graph.Tops[i].Y - rad &&
                        e.Y <= graph.Tops[i].Y + rad)
                    {
                        connect           = graph.Tops[i];
                        connect.Highlight = true;
                        cursorPoint       = new CGraphVertex(e.X, e.Y);

                        lineToCursor = new CGraphEdge(connect, cursorPoint);

                        graph.Edges.Add(lineToCursor);
                        panel1.Invalidate();
                        break;
                    }
                }
            }
        }
示例#9
0
 public void AddEdge(List <CGraphEdge> list, CGraphEdge edge)
 {
     list.Add(edge);
     DeleteHighlight();
     panel1.Invalidate();
 }
示例#10
0
 private void HighlightEdge(CGraphEdge edge)
 {
     edge.Highlight = !edge.Highlight;
 }