【发布时间】:2010-10-09 00:47:53
【问题描述】:
我有一个充满 1 和 0 的二维数组,例如
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0
0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
你可以看到数组中有一个正方形。 我正在尝试制作一个基于正方形制作矩形或矩形列表的函数。 所以这个例子会返回一个像
这样的矩形rect.x = 2
rect.y = 1
rect.width = 7
rect.height = 5
这是我现在拥有的代码,但它没有返回任何内容
Dim rects As New List(Of Rectangle)
For imgWidth As Integer = 0 To bow.GetUpperBound(0)
For imgHeight As Integer = 0 To bow.GetUpperBound(1)
If bow(imgWidth, imgHeight) = 1 Then
If bow(imgWidth + 1, imgHeight) = 1 And
bow(imgWidth + 2, imgHeight) = 1 And
bow(imgWidth, imgHeight + 1) = 1 And
bow(imgWidth, imgHeight + 2) = 1 Then
Dim r As New Rectangle
With r
.X = imgWidth
.Y = imgHeight
End With
For rectWidth As Integer = imgWidth To bow.GetUpperBound(0)
If bow(rectWidth, imgHeight) = 0 Then
r.Width = bow(rectWidth - 1, imgHeight)
End If
Next
For rectHeight As Integer = imgHeight To bow.GetUpperBound(1)
If bow(imgWidth, rectHeight) = 0 Then
r.Height = bow(rectHeight - 1, imgHeight)
End If
Next
rects.Add(r)
End If
End If
Next
Next
此外,数组必须能够有多个正方形。
【问题讨论】:
-
除了“它只是不返回任何东西”之外,您能否提供更多关于您的问题的详细信息?你试过什么?
-
当我尝试添加断点时,找到宽度和高度的循环总是返回 0
-
@giodamelio:这是作业吗?如果不是,它的用途是什么?为什么?
-
如果二维数组(7x9)是满的怎么办?它应该返回 756 个可能的矩形列表吗?
-
@JoshD 不,由于某种原因,它不是家庭作业,它被自动标记了。该数组是从一个由不同程序创建的文件中读取的,该程序最初是通过拖放界面制作文件,以便在画布上放置正方形。