在C#中绘制半径增加的圆圈

时间:2018-03-31 07:02:48

标签: c#

我使用以下代码绘制半径增加的圆圈(从csv文件读取中心)。半径的增加是每圈5个单位。

namespace MATLAB_file
 {
   public partial class Form1 : Form
     {
    string[] read;
    float th; 
    int c = 0;
    int r;

    public List<PointF> circleCoordinates = new List<PointF>();

    int rl;
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
        Graphics grphx = this.CreateGraphics();
        grphx.Clear(this.BackColor);

        foreach (PointF point in this.circleCoordinates)
        {
            Pen redPen1 = new Pen(Color.Red, 100);
            e.Graphics.DrawArc(Pens.Red, point.X, point.Y, 1, 1, 0, 120F);
        }
        linePen.Dispose();

        base.OnPaint(e);
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        double xx, yy;

        int i;
        int n = 0;
        float[] centre1 = new float[1000];
        System.IO.StreamReader sr;
        sr = new System.IO.StreamReader("centers.txt", true);

        char[] seperators = { ',' };
        string data = sr.ReadLine();
        read = data.Split(new Char[] { ',' });
        rl = read.Length;
        int a1 = rl / 2;

        for (c = 0; c < rl; c++)
        {


            centre1[c] = float.Parse(read[c]);

        }

        while (r < 200)

        {
            for (i = 0; i < a1; i++)

            {
                while (th < 360)
                {
                    xx = r * Math.Cos(th) + centre1[2 * i] + 100;
                    xx1 = (float)xx;

                    yy = r * Math.Sin(th) + centre1[2 * i + 1] + 100;
                    yy1 = (float)yy;
                    this.circleCoordinates.Add(new PointF(xx1, yy1));
                    this.Invalidate();

                    th = th + .360F;
                }

                th = 0;

            }
            r = r + 5;
        }

        }
    }

}

上面的代码显示了所有的圆圈,但我不希望所有圆圈都显示在画布上,而是只显示一个圆圈,半径逐渐增加 请建议如何在绘制新的圆圈时删除之前绘制的圆圈。有没有其他方法可以做到这一点,如果我以后使用包括删除基于“th”值的某些圈子?

1 个答案:

答案 0 :(得分:0)

如果您在Clear内移动foreach来电,您将只看到最后绘制的圈子,但这也可以通过仅绘制Last circleCoordinates来实现。

    foreach (PointF point in this.circleCoordinates)
    {
        grphx.Clear(this.BackColor);
        Pen redPen1 = new Pen(Color.Red, 100);
        e.Graphics.DrawArc(Pens.Red, point.X, point.Y, 1, 1, 0, 120F);
    }

另一种解释:

您想要生成onPaint事件的动画(通过计时器刻度或用户输入),然后增加计数器以选择要绘制的圆。

为此,您需要在程序中添加新成员,例如int Index;,然后您可以根据此选择圈子,使用以下代码段(假设您总是至少有1 circleCoordinates },动画完成后重新启动):

PointF point = this.circleCoordinates[Index % circleCoordinates.Length];

或(动画后最后一个圆圈保留在屏幕上)

PointF point = this.circleCoordinates[Math.Min(Index, circleCoordinates.Length - 1)];
相关问题