Example #1
0
        private void drawGraph()
        {
            try
            {
                Bitmap   bmpGraphs = new Bitmap(pbox.Width, pbox.Height);
                Graphics g         = Graphics.FromImage(bmpGraphs);
                g.Clear(Color.White);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                for (int i = 0; i < Edges.Count(); i++)
                {
                    for (int j = 0; j < Edges[i].Count(); j++)
                    {
                        if (!directed || !Edges[i][j].isOpposed)
                        {
                            Pen        linePen      = new Pen(Color.DarkBlue);
                            SolidBrush brushArrow   = new SolidBrush(Color.DarkBlue);
                            SolidBrush brushEllipse = new SolidBrush(Color.White);
                            SolidBrush brushString  = new SolidBrush(Color.Black);
                            linePen.Width = 5;
                            if (Edges[i][j].visited)
                            {
                                linePen.Color     = Color.LightSkyBlue;
                                brushArrow.Color  = Color.LightSkyBlue;
                                brushString.Color = Color.LightSkyBlue;
                            }
                            if (Edges[i][j].selected)
                            {
                                linePen.Color     = Color.DarkRed;
                                brushArrow.Color  = Color.DarkRed;
                                brushString.Color = Color.DarkRed;
                            }
                            PointF firstPoint   = Vertexes[i].position;
                            PointF secondPoint  = Vertexes[Edges[i][j].destination].position;
                            PointF middlePoint  = ExtraMath.GetMiddlePoint(firstPoint, secondPoint);
                            PointF quarterPoint = ExtraMath.GetMiddlePoint(middlePoint, secondPoint);
                            try
                            {
                                g.DrawLine(linePen, firstPoint, secondPoint);
                            }
                            catch { }
                            if (directed)
                            {
                                double   thetaLeft     = ExtraMath.ToRadians(150);
                                double   thetaRight    = ExtraMath.ToRadians(210);
                                PointF   rotationPoint = ExtraMath.GetPointAlongLine(quarterPoint, firstPoint, 25);
                                PointF   leftPart      = ExtraMath.RotateAroundPoint(quarterPoint, rotationPoint, thetaLeft);
                                PointF   rightPart     = ExtraMath.RotateAroundPoint(quarterPoint, rotationPoint, thetaRight);
                                PointF[] arrow         = { quarterPoint, leftPart, rightPart, quarterPoint };
                                try
                                {
                                    g.FillPolygon(brushArrow, arrow, System.Drawing.Drawing2D.FillMode.Alternate);
                                }
                                catch { }
                            }
                            if (weighted)
                            {
                                try
                                {
                                    g.FillEllipse(brushEllipse, middlePoint.X - 15, middlePoint.Y - 15, 30, 30);
                                    g.DrawString(Edges[i][j].weight.ToString(), new Font("Adobe Gothic Std B", 14),
                                                 brushString, middlePoint.X - 10, middlePoint.Y - 10);
                                }
                                catch { }
                            }
                        }
                    }
                }
                foreach (Vertex v in Vertexes)
                {
                    SolidBrush brushVertex = new SolidBrush(Color.Blue);
                    SolidBrush brushText   = new SolidBrush(Color.White);
                    if (v.visited)
                    {
                        brushVertex.Color = Color.LightSkyBlue;
                    }
                    if (v.selected)
                    {
                        brushVertex.Color = Color.DarkRed;
                    }
                    try
                    {
                        g.FillEllipse(brushVertex, v.position.X - VertexRadius, v.position.Y - VertexRadius, VertexRadius * 2, VertexRadius * 2);
                        g.DrawString(v.distance.ToString(), new Font("Adobe Gothic Std B", 15), brushText, v.position.X - VertexRadius + 10, v.position.Y - VertexRadius + 10);
                    }
                    catch { }
                }

                try
                {
                    pbox.Image = bmpGraphs;
                    pbox.Update();
                }
                catch
                {
                }
                GC.Collect();
                System.Threading.Thread.Sleep(10);
            }
            catch
            {
            }
        }