C# 如何将每次鼠标点击存储在整数数组中
我有一个bresenham算法,是我在课堂上写的 我现在可以画线了,我想画多边形,所以我写了它的函数(voidpolygon) 我应该将每次单击的坐标存储在一个数组中,然后我的函数应该获取它们 我不知道如何存储每次单击C# 如何将每次鼠标点击存储在整数数组中,c#,C#,我有一个bresenham算法,是我在课堂上写的 我现在可以画线了,我想画多边形,所以我写了它的函数(voidpolygon) 我应该将每次单击的坐标存储在一个数组中,然后我的函数应该获取它们 我不知道如何存储每次单击 Radiobutton1用于绘制线,radiobutton2用于绘制多边形 private void panel1_MouseClick(object sender, MouseEventArgs e) { if(rad
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));
}
private void panel1\u鼠标单击(对象发送器,鼠标指针)
{
如果(radioButton1.选中)
如果(首次单击)
{
firstX=e.X;
firstY=e.Y;
firstClick=false;
}
其他的
{
l行=新行(firstX,firstY,e.X,e.Y,panel1.CreateGraphics(),Convert.ToInt32(textBox1.Text));
firstClick=true;
}
如果(radioButton2.选中)
{
//如何在此处写入以便在数组中存储每次单击
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int n=Convert.ToInt32(textBox2.Text);
多边形(n,coor);
}
无效多边形(int n,int[]坐标)
{
如果(n>=2)
{
行l=新行(坐标[0],坐标[1],坐标[2],坐标[3],panel1.CreateGraphics(),Convert.ToInt32(textBox1.Text));
对于(int count=1;count,您可以创建单击坐标点:
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 myPoints=新列表();
//在方法中:
如果(radioButton2.选中){
添加(新点(e.x,e.y));
}
数组不是一个好主意,因为你通常不知道会有多少次点击。列表长度可变,因此在这种情况下很有用。那么我应该期待polygonI的另一种算法。不知道你想对点做什么,如果需要更多信息,请问另一个问题。请标记我的answer作为答案,如果是。