【问题标题】:Intersecting rectangles from array数组中的相交矩形
【发布时间】:2016-05-23 18:11:40
【问题描述】:
private void Form1_Load(object sender, EventArgs e)
{ 
    for (int i = 0; i < pala.cantLetras; i++)
    {   guiones[i] = new Label();
        guiones[i].Text = "_";
        guiones[i].Font = new Font("Berlin Sans FB Demi", 25);
        guiones[i].Size = new Size(18, 37);
        guiones[i].Name = "guion" + i;
        guiones[i].ForeColor = Color.Black;
        guiones[i].BackColor = Color.Transparent;
        guiones[i].Location = new Point(x, 341);
        this.Controls.Add(guiones[i]);

        recguion[i] = guiones[i].Bounds;
        recguion[i] = new Rectangle();
        //recguion[i].Location = new Point(x, 341);
        x = x + 50;
    }

    for (int i = 0; i < pala.cantLetras; i++)
    {
        labels[i] = new Label();
        labels[i].Size = new Size(25, 55);
        labels[i].Name = "label" + i;
        labels[i].Text = pala.palabra[i].ToString();
        labels[i].Font = new Font("Berlin Sans FB Demi", 20);
        labels[i].ForeColor = Color.Black;
        labels[i].BackColor = Color.Transparent;
        labels[i].Location = new Point(y, 165);
        this.Controls.Add(labels[i]);
        posRandom[i] = y;

        reclabel[i] = labels[i].Bounds;
        reclabel[i] = new Rectangle();

       // reclabel[i].Location = new Point(y, 165);

        y = y + 40;

    }

}

我需要知道 reclabel[] 何时与对应于该数字的 recguion[] 相交。 例如:reclabel[1] 与 recguion[1] 相交,但只有那个相交,如果它与另一个相交,则必须说它是错误的。 矩形内部(或者这就是我正在尝试的)标签 [] 和 guiones [] 这是我尝试过的,但它不起作用。

private void intersecta()
{
    int cont = 0;

    for (int i = 0; i < pala.cantLetras; i++)
    {
        for (int j = 0; j < pala.cantLetras; j++)
        {
            if (i==j)
            {
                Rectangle intersect = Rectangle.Intersect(reclabel[i],         recguion[j]);
                if (intersect != Rectangle.Empty)
                {
                    MessageBox.Show("Intersection!");
                    cont++;
                }

            }
            if (cont != 0)
            {
                i = pala.cantLetras - 1;
                j = pala.cantLetras - 1;
            }

        }

    }

}

谢谢!

【问题讨论】:

  • 不需要嵌套循环。只需遍历一个数组并使用 .IntersectsWith 检查该索引处的两个矩形。
  • 您设置了recguion[i] = guiones[i].Bounds;,然后立即用recguion[i] = new Rectangle(); 覆盖它,这似乎不对。

标签: c# forms intersection rectangles intersect


【解决方案1】:

不需要嵌套循环。只需遍历一个数组并使用.IntersectsWith 检查该索引处的两个矩形。如果有任何语法错误,我深表歉意,我目前无法访问 Visual Studio。

For(int i = 0; i < Array1.Length; i++)
{
    if(Array1[i].IntersectsWith(Array2[i]))
    {
        //Intersected
    }
}

而且,正如 Andrew 所指出的,您在这里遇到了一个严重的问题:

reclabel[i] = new Rectangle();

您只是用一个新实例(不同类型!)覆盖所有数据。

【讨论】:

  • 我试过这样做,但它不起作用我不知道还能做什么。非常感谢你
猜你喜欢
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 2013-03-31
  • 2023-03-12
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
相关资源
最近更新 更多