【发布时间】: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