c# - 我如何检查要删除的点?

标签 c#

我有两个类型为 intList,我在其中存储名为 Point_XPoint_Y 的点坐标。

这是新类中的删除方法:

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
        {
        }
    }
}

xy 中,我得到了我点击的点的索引。如果我添加两个点并单击其中之一,那么我将获得该点的两个索引,例如x 将为 0,y 将为 -1。

现在我正在遍历 Point_X 列表,我需要检查所有情况并将我获得的索引与列表 Point_X 中的索引进行比较,并且Point_Y 然后它应该删除我点击的点。

这个 if 似乎不起作用:if ((i == x) && (i == y) || y == -1 || x == -1)。 如果我在 pictureBox1 中有两个点,然后单击第 2 个点并尝试将其删除,它不会在第一时间被删除。不过,我第二次点击时它确实被删除了。

这是在 Form1 中点击删除点的按钮的代码:

private void button3_Click(object sender, EventArgs e)
{           
    wireObject1.DeletePoint(cyclicSelectedIndex[0],cyclicSelectedIndex[1]);
    button3.Enabled = false;
}

这是我在 pictureBox1MouseDown 事件中点击要选择的点的代码:

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();
                }
            }
        }
    }
}

问题是新类中的 DeletePoint 方法不会删除我点击的点,除非我点击它两次。


这是合乎逻辑的 if ((i == x) && (i == y)) 因为在某些情况下 x 是 1 而 y 为 0,因此变量 (i) 永远不会同时为 0 和 1,所以我被困在这里。

我不知道如何检查所有情况 - 例如x 是 1,y 是 0。可能还有其他情况要涵盖,但我认为问题出在这个 if 语句中。


x = 1 , y = 1

Point_X >>> [0] = 331.0 , [1] = 212.0

Y 点 >>> [0] = 213.0 , [1] = 212.0

我= 0

当我有两个点并且我点击了第二个点时就是这种情况。 它没有在新类的函数中执行任何操作,也没有删除该点。

同样,第一个点没有被移除,它没有进入移除区域。 这是我使用 Alexei Levenkov idea sample 进行 If 检查的时候。

现在在 Form1 中,当我单击 pictureBox1 中的一个点时,在鼠标按下事件中我有一个变量(t),我计算新类中该点的索引:

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;
        }

例如,如果我有一个点并单击它,则 variable(t) = 0

然后列表 cyclicSelectedIndex 在 [0] 中有两个单元格/位置,我有 0,在 [1] 中有 -1,currentCyclicIndex 现在是 1,selectedIndex 是 0,在鼠标按下事件中的 Form1 中。鼠标按下事件只是标记我要删除的点。

在 Form1 按钮中单击我单击的位置以删除点:

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
                {
                }
            }



        }

x = 0 和 y = -1

现在我需要从列表中删除索引 0 和 -1:Point_x 和 Point_Y 并且此列表中的每个索引都包含我要删除的 hte 点的坐标。

最佳答案

您很可能想将元素的 与 x/y 进行比较,而不是索引:

if ((Point_X[i] == x) && (Point_Y[i] == y) || y == -1 || x == -1)  

请注意,最好使用具有 X/Y 属性的类(如果您知道/有充分的理由,则为结构)并将它们存储在一个列表中:

class MyPoint { public int X;public int Y;}
List<MyPoint> point = new List<MyPoint>();

关于c# - 我如何检查要删除的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11173646/

相关文章:

C# for 循环在 html MVC 中

c# - 使用.Net(C#)调用Java函数

c# - 从字符数组中删除一个字母

c# - 如何编写自定义依赖属性和数据绑定(bind)到自定义类型?

c# - 一起计算 sin 和 cos 的最快方法是什么?

c# - 在代码中使用千位分隔符

c# - Visual C++ vs Visual C#,哪个最好学?

c# - 有没有办法告诉编译器将始终执行特定代码块?

c# - 白色 Blob 检测

c# - 无法确定外键的复合外键顺序