Ejemplo n.º 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
            {
            }
        }
Ejemplo n.º 2
0
        public void DisplaySort()
        {
            if (Reversed)
            {
                numbers.Reverse();
            }

            try
            {
                if (Type != VisualTypes.Bars)
                {
                    bmp = new Bitmap(mainForm.pbox_sorts.Width * 2, mainForm.pbox_sorts.Height * 2);
                }
                else
                {
                    bmp = new Bitmap(1100, 1100);
                }
                Graphics g = Graphics.FromImage(bmp);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                g.Clear(Color.White);
                float space = 1;
                Int16 drawn = 0;
                switch (Type)
                {
                case VisualTypes.Bars:
                    space = (float)bmp.Size.Width / size;
                    break;

                case VisualTypes.Spiral:
                    space = (float)360 / size;
                    break;

                case VisualTypes.Pyramid:
                    space = (float)bmp.Size.Height / size;
                    break;
                }
                SolidBrush brsh = new SolidBrush(Color.Blue);
                for (int j = 0; j < numbers.Count; j++)
                {
                    Int16 i = numbers[j];
                    if (numbers.isSelected(j))
                    {
                        brsh.Color = Color.Red;
                    }
                    else if (Colored)
                    {
                        int value = (int)ExtraMath.MapNumberToRange <float>((float)i, (float)0, (float)maxValue, (float)0, (float)255);
                        brsh.Color = Color.FromArgb(0, 0, 255 - value);
                    }
                    else
                    {
                        brsh.Color = Color.Blue;
                    }
                    float      mapped;
                    RectangleF rect;
                    switch (Type)
                    {
                    case VisualTypes.Bars:
                        mapped = ExtraMath.MapNumberToRange <float>((float)i, (float)0, (float)maxValue, (float)0, (float)bmp.Height);
                        rect   = new RectangleF((float)space * drawn, bmp.Height - mapped, (float)space - 0, mapped);
                        g.FillRectangle(brsh, rect);
                        break;

                    case VisualTypes.Spiral:
                        mapped = ExtraMath.MapNumberToRange <float>((float)i, (float)minValue, (float)maxValue, (float)0, (float)Math.Min(bmp.Width / 2, bmp.Height / 2));
                        PointF   Point1 = ExtraMath.GetPointAlongTrajectory(new PointF(bmp.Width / 2, bmp.Height / 2), ExtraMath.ToRadians(drawn * space), 10);
                        PointF   Point2 = ExtraMath.GetPointAlongTrajectory(new PointF(bmp.Width / 2, bmp.Height / 2), ExtraMath.ToRadians(drawn * (space)), mapped + 10);
                        PointF   Point3 = ExtraMath.GetPointAlongTrajectory(new PointF(bmp.Width / 2, bmp.Height / 2), ExtraMath.ToRadians((drawn + 1) * (space) - 0.1), mapped + 10);
                        PointF[] Points = { Point1, Point2, Point3 };
                        g.FillPolygon(brsh, Points);
                        break;

                    case VisualTypes.Pyramid:
                        mapped = ExtraMath.MapNumberToRange <float>((float)i, (float)0, (float)maxValue, (float)0, (float)bmp.Width / 2);
                        rect   = new RectangleF(mapped, (drawn + 1) * space, bmp.Width - mapped - mapped, space);
                        g.FillRectangle(brsh, rect);
                        break;
                    }

                    drawn++;
                }
                try
                {
                    mainForm.pbox_sorts.Image = bmp;
                    mainForm.pbox_sorts.Update();
                }
                catch
                { }
                System.Threading.Thread.Sleep(20);
            }
            catch { }
            if (Reversed)
            {
                numbers.Reverse();
            }
            GC.Collect();
        }