C# 如何检查要删除的点?
我有两个类型为C# 如何检查要删除的点?,c#,C#,我有两个类型为int的列表s,其中我存储的点坐标称为点X和点Y 这是新类中的delete方法: public void DeletePoint(int x, int y) { for (int i = 0; i < Point_X.Count; i++) { if ((i == x) && (i == y) || y == -1 || x == -1) { Point_X.RemoveAt(i);
int
的列表
s,其中我存储的点坐标称为点X
和点Y
这是新类中的delete方法:
public void DeletePoint(int x, int y)
{
for (int i = 0; i < Point_X.Count; i++)
{
if ((i == x) && (i == y) || y == -1 || x == -1)
{
Point_X.RemoveAt(i);
Point_Y.RemoveAt(i);
}
else
{
}
}
}
public float GetIndexByXY( int x , int y , float tol)
{
for (idx = 0; idx < Point_X.Count; ++idx)
{
float dx = Point_X[idx] - x;
float dy = Point_Y[idx] - y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
if (dist < tol) return idx;
}
return -1;
}
这是我在pictureBox1
的MouseDown
事件中单击要选择的点的代码:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);
if (t == -1)
{
button3.Enabled = false;
}
else
{
button3.Enabled = true;
{
selectedIndex = t;
mouseMove = true;
OriginalX = wireObject1._point_X[(int)selectedIndex];
OriginalY = wireObject1._point_Y[(int)selectedIndex];
if (cyclicSelectedIndex.Count() == 2)
{
cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
currentCyclicIndex++;
if (currentCyclicIndex == 2)
{
currentCyclicIndex = 0;
}
if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
{
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] && wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart[i] == cyclicSelectedIndex[1] && wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
{
button2.Enabled = false;
}
}
label13.Text = selectedIndex.ToString();
label13.Visible = true;
label14.Visible = true;
listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
}
}
}
}
}
例如,如果我有一个点,我点击它,那么变量(t)=0
然后列表CycleCselectedIndex在[0]中有两个单元格/位置,在[1]中我有0,在[1]中我有-1,CurrentCycleCindex现在是1,selectedIndex是0,它在鼠标按下事件中的格式为1。鼠标按下事件只是为了标记我要删除的哪个点
在Form1按钮中,单击我单击的位置以删除该点:
private void button3_Click(object sender, EventArgs e)
{
wireObject1.DeletePoint(cyclicSelectedIndex[0],cyclicSelectedIndex[1]);
button3.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
wireObject1.DeletePoint(cyclicSelectedIndex[0],cyclicSelectedIndex[1]);
button3.Enabled = false;
pictureBox1.Invalidate();
}
cyclicSelectedIndex[0]=0和cyclicSelectedIndex[1]=1
因此,在DeletePoint函数的新类中:
public void DeletePoint(int x, int y)
{
for (int i = 0; i < Point_X.Count; i++)
{
if ((Point_X[i] == x) && (Point_Y[i] == y) || y == -1 || x == -1)
{
Point_X.RemoveAt(i);
Point_Y.RemoveAt(i);
}
else
{
}
}
}
public void DeletePoint(int x,int y)
{
对于(int i=0;i
x=0,y=-1
现在我需要从列表中删除索引0和-1:点x和点Y,此列表中的每个索引都包含我要删除的点的坐标。最有可能的是要将元素的值与x/Y进行比较,而不是索引:
if ((Point_X[i] == x) && (Point_Y[i] == y) || y == -1 || x == -1)
注意,最好将类(struct,如果您知道/有充分的理由)与X/Y属性一起使用,并将它们存储在一个列表中:
class MyPoint { public int X;public int Y;}
List<MyPoint> point = new List<MyPoint>();
类MyPoint{public int X;public int Y;}
列表点=新列表();
您最可能希望将元素的值与x/y(而不是索引)进行比较:
if ((Point_X[i] == x) && (Point_Y[i] == y) || y == -1 || x == -1)
注意,最好将类(struct,如果您知道/有充分的理由)与X/Y属性一起使用,并将它们存储在一个列表中:
class MyPoint { public int X;public int Y;}
List<MyPoint> point = new List<MyPoint>();
类MyPoint{public int X;public int Y;}
列表点=新列表();
请举例说明该列表中的“点的两个索引,例如x为0,y为-1”可能看起来像?O.R Mapper-例如,现在在Point_X列表中,我在0中有两个单元格/位置,我有337.0,在1中我有319.0,在Point_Y列表中,我有204.0,在1中我有206.0,现在X是1,Y是0,在这种情况下,该点将不会删除。@user1477444,旁注:感谢您更新问题以回应我的评论。对于将来的问题-无论何时您对评论进行编辑-也要回答评论(使用类似@PersonName的内容-请参阅更新的问题
),以便通知发表评论的人更新。也就是说,在这种情况下,我注意到更新只是因为你接受了答案。你能提供一个例子,说明该列表中的“点的两个索引,例如x为0,y为-1”可能看起来像?O.R Mapper-例如,现在在Point_X列表中,我在0中有两个单元格/位置,我有337.0,在1中我有319.0,在Point_Y列表中,我有204.0,在1中我有206.0,现在X是1,Y是0,在这种情况下,该点将不会删除。@user1477444,旁注:感谢您更新问题以回应我的评论。对于将来的问题-无论何时您对评论进行编辑-也要回答评论(使用类似@PersonName的内容-请参阅更新的问题
),以便通知发表评论的人更新。也就是说,在这种情况下,我注意到更新只是因为你接受了答案。Alexei我用if((Point_X[I]……等等……它也不起作用。例如,我现在添加了4个点,因此在列表点_X中,我现在有4个位置,4个单元格,每个单元格都有另一个坐标,例如,我单击了点编号3,因此在第一个Interion变量(i)中,现在X是2,y是3是0,所以它会将0放在列表中,但它不起作用。即使我单击了几次,它也不会删除我单击的点。+1用于建议点列表,而不是两个坐标列表。@user1477444,我不明白你想做什么……请用类似“point_X={12,34546},point_Y”的内容更新你的问题={43,54,65},X=?,Y=?;期望删除索引为2的元素?Alexei我尝试使用if((点X[i]……等等……它也不起作用。例如,我现在添加了4个点,因此在列表点_X中,我现在有4个位置,4个单元格,每个单元格都有另一个坐标,例如,我单击了点编号3,因此在第一个Interion变量(i)中,现在X是2,y是3是0,所以它会将0放在列表中,但它不起作用。即使我单击了几次,它也不会删除我单击的点。+1用于建议点列表,而不是两个坐标列表。@user1477444,我不明白你想做什么……请用类似“point_X={12,34546},point_Y”的内容更新你的问题={43,54,65},X=?Y=?;应删除索引为2的元素“?