如何在整数数组中存储鼠标的每次单击

时间:2012-05-13 11:51:51

标签: c#

我有一个bresenham算法,我在课堂上写了它 我可以绘制线条现在我想绘制多边形,所以我写了它的函数(void Polygon) 我应该将每次点击的坐标存储在一个数组中,然后我的函数应该得到它们 我不知道如何存储每次点击
Radiobutton1用于绘制线,radiobutton2用于绘制多边形

private void panel1_MouseClick(object sender, MouseEventArgs e)
        {           
           if(radioButton1.Checked)
            if (firstClick)
            {
                firstX = e.X;
                firstY = e.Y;
                firstClick = false;
            }
            else
            {
              Line l = new Line(firstX, firstY, e.X, e.Y, panel1.CreateGraphics(), Convert.ToInt32(textBox1.Text));

              firstClick = true;

            }
           if(radioButton2.Checked)
            {
       //how to write here so as to store each click in array

                }
            }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(textBox2.Text);

            Polygon(n, coor);
        }
   void Polygon(int n,int[] coordinates)
     {
       if(n>=2)
      {
         Line l=new Line(coordinates[0],coordinates[1],coordinates[2],coordinates[3],panel1.CreateGraphics(), Convert.ToInt32(textBox1.Text));
         for(int count=1;count<(n-1);count++)
             l=new Line(coordinates[(count*2)],coordinates[((count*2)+1)],coordinates[((count+1)*2)],coordinates[(((count+1)*2)+1)],panel1.CreateGraphics(), Convert.ToInt32(textBox1.Text));
      }

1 个答案:

答案 0 :(得分:1)

您可以指出点击坐标:

Point p = new Point(e.x, e.y);

将您获得的积分保存在列表中:

// Declaration:
List<Point> myPoints = new List<Point>();

// in the method: 
if (radioButton2.Checked) {
   myPoints.Add(new Point(e.x, e.y));
}

数组不是一个好主意,因为你通常不知道会有多少次点击。 List具有可变长度,因此在这种情况下很有用。

相关问题