示例#1
0
        public static void Draw(int N, int max)
        {
            Graphics g = form1.panel1.CreateGraphics();

            //画坐标轴
            g.DrawLine(Pens.White, 210, 400, 410, 400);
            g.DrawLine(Pens.White, 210, 100, 210, 400);
            g.DrawLine(Pens.White, 460, 400, 660, 400);
            g.DrawLine(Pens.White, 460, 100, 460, 400);

            //画原点刻度
            g.DrawString("0", new Font("New Timer", 8), Brushes.White, new PointF(200, 405)); //原点刻度
            g.DrawString("0", new Font("New Timer", 8), Brushes.White, new PointF(450, 405)); //原点刻度
            int count   = Convert.ToInt32(Math.Sqrt(max / N)) + 1;                            //数据个数
            int section = 200 / Convert.ToInt32(Math.Pow(2, count - 1));                      //横刻度单位长度

            //计算结果
            double[] times = new double[count]; //记录T(N)
            double[] lg    = new double[count]; //记录lg(T(N))
            for (int i = N, j = 0; i <= max; i += i, j++)
            {
                times[j]           = DoublingTest.timeTrial(i);
                lg[j]              = Math.Log10(times[j]);
                form1.label3.Text += " " + i + "\n\n";
                form1.label4.Text += " " + times[j] + "\n\n";
                g.DrawString($"{(double)i / 1000}k", new Font("New Timer", 8), Brushes.White, new PointF(190 + i / N * section, 405));//图1横刻度
            }

            //根据结果画纵刻度
            int    timer = Convert.ToInt32(Math.Ceiling(times[count - 1] / 6));
            double lger  = lg[count - 1] / 6;

            for (int i = 1; i < 7; i++)
            {
                g.DrawString($"{i * timer}", new Font("New Timer", 8), Brushes.White, new PointF(185, 400 - i * 45));   //图1纵刻度
                g.DrawString($"{i * lger:F1}", new Font("New Timer", 8), Brushes.White, new PointF(435, 400 - i * 45)); //图2纵刻度
            }

            Point[] points1 = new Point[count]; //图1点
            Point[] points2 = new Point[count]; //图2点
            for (int i = 0; i < count; i++)
            {
                points1[i].X = (int)(190 + Math.Pow(2, i) * section);
                points1[i].Y = (int)(400 - times[i] / timer * 45);
                points2[i].X = 440 + (i + 1) * 200 / count;
                points2[i].Y = (int)(400 - lg[i] / lger * 45);
                g.FillRectangle(Brushes.Gray, points1[i].X, points1[i].Y, 3f, 3f);                                                     //画图1点
                g.FillRectangle(Brushes.Gray, points2[i].X, points2[i].Y, 3f, 3f);                                                     //画图2点
                g.DrawString($"{Math.Pow(2, i) * N / 1000}k", new Font("New Timer", 8), Brushes.White, new PointF(points2[i].X, 405)); //图2横刻度
            }
            g.DrawCurve(Pens.White, points1);
            g.DrawLines(Pens.White, points2);
            g.Dispose();
        }
        public static void Draw(int N, int max)
        {
            form1.gp.Reset();

            //画坐标轴
            form1.gp.AddLine(210, 400, 410, 400);
            form1.gp.StartFigure();
            form1.gp.AddLine(210, 100, 210, 400);
            form1.gp.StartFigure();
            form1.gp.AddLine(460, 400, 660, 400);
            form1.gp.StartFigure();
            form1.gp.AddLine(460, 100, 460, 400);

            int count   = Convert.ToInt32(Math.Sqrt(max / N)) + 1;       //数据个数
            int section = Convert.ToInt32(200 / Math.Pow(2, count - 1)); //横刻度单位长度

            //计算结果
            double[] times = new double[count]; //记录T(N)
            double[] lg    = new double[count]; //记录lg(T(N))
            for (int i = N, j = 0; i <= max; i += i, j++)
            {
                times[j]           = DoublingTest.TimeTrial(i);
                lg[j]              = Math.Log10(times[j]);
                form1.label3.Text += " " + i + "\n\n";
                form1.label4.Text += " " + times[j] + "\n\n";

                //图1横刻度
                Label heng1 = new Label
                {
                    Name      = "heng1" + i,
                    Text      = $"{(double)i / 1000}k",
                    Location  = new Point(190 + i / N * section, 405),
                    ForeColor = SystemColors.HighlightText,
                    AutoSize  = true,
                    BackColor = Color.Transparent,
                    Tag       = "scale"
                };
                form1.ControlsInfo.Add(heng1.Name, (heng1.Left + heng1.Width / 2) + "," + (heng1.Top + heng1.Height / 2) + "," + heng1.Width + "," + heng1.Height + "," + heng1.Font.Size);
                if (form1.scaleX != 1 || form1.scaleY != 1)
                {
                    heng1.Location = new Point((int)(heng1.Location.X * form1.scaleX), (int)(heng1.Location.Y * form1.scaleY));
                    heng1.Font     = new Font(heng1.Font.Name, float.Parse((heng1.Font.Size * Math.Min(form1.scaleX, form1.scaleY)).ToString()));//字体
                }
                form1.panel1.Controls.Add(heng1);
            }

            //根据结果画纵刻度
            int    timer = Convert.ToInt32(Math.Ceiling(times[count - 1] / 6));
            double lger  = lg[count - 1] / 6;

            for (int i = 1; i < 7; i++)
            {
                //图1纵刻度
                Label zong1 = new Label
                {
                    Name      = "zong1" + i,
                    Text      = $"{i * timer}",
                    Location  = new Point(185, 400 - i * 45),
                    ForeColor = SystemColors.HighlightText,
                    AutoSize  = true,
                    BackColor = Color.Transparent,
                    Tag       = "scale"
                };
                form1.ControlsInfo.Add(zong1.Name, (zong1.Left + zong1.Width / 2) + "," + (zong1.Top + zong1.Height / 2) + "," + zong1.Width + "," + zong1.Height + "," + zong1.Font.Size);
                if (form1.scaleX != 1 || form1.scaleY != 1)
                {
                    zong1.Location = new Point((int)(zong1.Location.X * form1.scaleX), (int)(zong1.Location.Y * form1.scaleY));
                    zong1.Font     = new Font(zong1.Font.Name, float.Parse((zong1.Font.Size * Math.Min(form1.scaleX, form1.scaleY)).ToString()));//字体
                }

                form1.panel1.Controls.Add(zong1);

                //图2纵刻度
                Label zong2 = new Label
                {
                    Name      = "zong2" + i,
                    Text      = $"{i * lger:F1}",
                    Location  = new Point(435, 400 - i * 45),
                    ForeColor = SystemColors.HighlightText,
                    AutoSize  = true,
                    BackColor = Color.Transparent,
                    Tag       = "scale"
                };
                form1.ControlsInfo.Add(zong2.Name, (zong2.Left + zong2.Width / 2) + "," + (zong2.Top + zong2.Height / 2) + "," + zong2.Width + "," + zong2.Height + "," + zong2.Font.Size);
                if (form1.scaleX != 1 || form1.scaleY != 1)
                {
                    zong2.Location = new Point((int)(zong2.Location.X * form1.scaleX), (int)(zong2.Location.Y * form1.scaleY));
                    zong2.Font     = new Font(zong2.Font.Name, float.Parse((zong2.Font.Size * Math.Min(form1.scaleX, form1.scaleY)).ToString()));//字体
                }
                form1.panel1.Controls.Add(zong2);
            }

            Point[] points1 = new Point[count]; //图1点
            Point[] points2 = new Point[count]; //图2点
            for (int i = 0; i < count; i++)
            {
                points1[i].X = (int)(190 + Math.Pow(2, i) * section);
                points1[i].Y = (int)(400 - times[i] / timer * 45);
                points2[i].X = 440 + (i + 1) * 200 / count;
                points2[i].Y = (int)(400 - lg[i] / lger * 45);
                //g.FillRectangle(Brushes.Gray, points1[i].X, points1[i].Y, 3f, 3f);//画图1点
                //g.FillRectangle(Brushes.Gray, points2[i].X, points2[i].Y, 3f, 3f);//画图2点

                //图2横刻度
                Label heng2 = new Label
                {
                    Name      = "heng2" + i,
                    Text      = $"{Math.Pow(2, i) * N / 1000}k",
                    Location  = new Point(points2[i].X, 405),
                    ForeColor = SystemColors.HighlightText,
                    AutoSize  = true,
                    BackColor = Color.Transparent,
                    Tag       = "scale"
                };
                form1.ControlsInfo.Add(heng2.Name, (heng2.Left + heng2.Width / 2) + "," + (heng2.Top + heng2.Height / 2) + "," + heng2.Width + "," + heng2.Height + "," + heng2.Font.Size);
                if (form1.scaleX != 1 || form1.scaleY != 1)
                {
                    heng2.Location = new Point((int)(heng2.Location.X * form1.scaleX), (int)(heng2.Location.Y * form1.scaleY));
                    heng2.Font     = new Font(heng2.Font.Name, float.Parse((heng2.Font.Size * Math.Min(form1.scaleX, form1.scaleY)).ToString()));//字体
                }
                form1.panel1.Controls.Add(heng2);
            }

            form1.gp.StartFigure();
            form1.gp.AddCurve(points1);
            form1.gp.StartFigure();
            form1.gp.AddLines(points2);
            Matrix m1 = new Matrix();

            m1.Scale(form1.scaleX, form1.scaleY);
            GraphicsPath gpCache = new GraphicsPath();

            gpCache = (GraphicsPath)form1.gp.Clone(); //创建图形副本
            gpCache.Transform(m1);
            form1.g.DrawPath(Pens.White, gpCache);    //画出变化后的副本
        }