我是一个有用的助手,可以为您翻译文本。
我有一个二维数组,里面充满了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
此外,数组必须能够拥有多个正方形。