如何制作包含更多点的图形路径?

时间:2014-05-26 18:20:25

标签: c# curve

我创建一个后面跟一个矩形的路径。我想用更多" tick"点,因为在每个计时器刻度线上我将它绘制在制作曲线上的下一个点上。当它到达曲线时,它会顺利进行,因为曲线上有更多的点,但是在路径的直线部分,它会跳过很多距离以到达列表中的下一个点。如何使用更多积分制作PathPoints?或者是否有更好的方法来解决我想要做的事情?我使用的方法,我在互联网上找到了它。

    public Form1()
    {
        InitializeComponent();

        //avoid flickering
        this.DoubleBuffered = true;

        //make a bitmap to display
        _bmp = new Bitmap(100, 100);
        using (Graphics g = Graphics.FromImage(_bmp))
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            g.FillRectangle(Brushes.Aquamarine, new Rectangle(_bmp.Width / 2 - 2, _bmp.Height / 2 - 2, 10, 10));
        }

        //cleanup
        this.FormClosing += delegate { if (_bmp != null) _bmp.Dispose(); if (_gPath != null) _gPath.Dispose(); };

        //setup a path and add some random values
        _gPath = new System.Drawing.Drawing2D.GraphicsPath();

        List<Point> fList = new List<Point>();

        //add points that will let the picturebox be fully visible inside the form
        Point middle = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
        fList.Add(new Point(this.ClientSize.Width / 2, this.ClientSize.Height));
        fList.Add(new Point(middle.X - 20, middle.Y + 20));
        fList.Add(new Point(0, this.ClientSize.Height / 2));
        //fList.Add(new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2));

        //add a curve by these values
        _gPath.AddCurve(fList.ToArray());

        //flatten, to make the path a Path of lines and points
        _gPath.Flatten();

        //get these points as Array to locate the picturebox
        this._points = _gPath.PathPoints;

        //add the handler for the paint-event
        this.Paint += new PaintEventHandler(Form1_Paint);

        //start the timer
        t.Tick += new EventHandler(t_Tick);
        t.Interval = 100;
        t.Start();
        Console.WriteLine(_points.Length);
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {            
        if (_gPath != null && _bmp != null)
        {
            //draw the image
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.DrawPath(Pens.Black, _gPath);
            e.Graphics.DrawImage(_bmp, (int)_points[_i].X - (_bmp.Width / 2), (int)_points[_i].Y - (_bmp.Height / 2));
        }
    }

    void t_Tick(object sender, EventArgs e)
    {
        t.Stop();

        //redraw
        this.Invalidate();

        _i++;
        if (_i >= _points.Length)
            _i = 0;

        t.Start();
    }
}

1 个答案:

答案 0 :(得分:1)

要获得更多分数,你应该为Flatten方法重载:

_gPath.Flatten(null, (float) 0.1);

另外我建议组织动画循环,间隔为40ms。所以它将是每秒25帧。 (1000s / 25 = 40 ms)

t.Interval = 40;