如何检查椭圆形是否接触任何其他椭圆形VB

时间:2015-12-18 15:52:06

标签: vb.net

所以我正在学习视觉基础,我正在尝试制作一个移动椭圆形的计时器,并检查是否接触任何其他椭圆形,它将反转整数。所以我有这个代码,这是有效的,但我想知道我是否可以缩短它。如果你能解释你的代码,因为我很新,这将是伟大的!提前致谢!

Private Sub Timer4_Tick(sender As System.Object, e As System.EventArgs) Handles Timer4.Tick, Timer4.Tick
    Dim Pos As Integer
    If bb.Bounds.IntersectsWith(OvalShape1.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape2.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape3.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape4.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape5.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape6.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape7.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape8.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape9.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape10.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape11.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape12.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape13.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape14.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape15.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape16.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape17.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape18.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape19.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape20.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape21.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape22.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape23.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape24.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape25.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    If bb.Bounds.IntersectsWith(OvalShape26.Bounds) Then
        Pos = -20
    Else
        Pos = 20
    End If
    bb.Top -= Pos
End Sub

1 个答案:

答案 0 :(得分:1)

您应该将重复的代码重构为一个方法,并为您的值创建常量以更好地解释它们的含义:

Shared Function GetIntersection(Shape first, Shape second)
    Const IntersectionValue = -20
    Const NonIntersectionValue = 20

    If first.Bounds.IntersectsWith(second.Bounds) Then
        return IntersectionValue 
    Else
        return NonIntersectionValue 
    End If
End Function

然后你会这样称呼它:

Dim Pos As Integer = GetIntersection(bb, OvalShape1)

这是良好软件设计的重要原则之一的示例:Don't Repeat Yourself (D.R.Y)

你的代码存在的问题 - 无论你是按自己的方式行事,还是按照我提出的方式行事,你都不会对这些值做任何事情 - 你随后会覆盖每个值操作。例如:

If bb.Bounds.IntersectsWith(OvalShape1.Bounds) Then
    Pos = -20
Else
    Pos = 20
End If

' you should at least do something with `Pos` assigned in the
' above if-else block before you overwrite it with the following if-else:

If bb.Bounds.IntersectsWith(OvalShape2.Bounds) Then
    Pos = -20
Else
    Pos = 20
End If

它的方式是,它只会采用最后的Pos的值 if-else block

If bb.Bounds.IntersectsWith(OvalShape26.Bounds) Then '...

那么之前的其他街区有什么意义呢?他们只是浪费在头顶上。