How to calculate the center of a rectangle in VB.NET

1 Answer

0 votes
Imports System

Class RectangleCenter
    Class Point
        Public x, y As Double

        Public Sub New(ByVal x As Double, ByVal y As Double)
            Me.x = x
            Me.y = y
        End Sub
    End Class

    Class Rectangle
        Public topLeft As Point
        Public bottomRight As Point

        Public Sub New(ByVal topLeft As Point, ByVal bottomRight As Point)
            Me.topLeft = topLeft
            Me.bottomRight = bottomRight
        End Sub
    End Class

    Private Shared Function GetCenter(ByVal rect As Rectangle) As Point
        Dim centerX As Double = (rect.topLeft.x + rect.bottomRight.x) / 2.0
        Dim centerY As Double = (rect.topLeft.y + rect.bottomRight.y) / 2.0
		
        Return New Point(centerX, centerY)
    End Function

    Public Shared Sub Main()
        Dim rect As Rectangle = New Rectangle(New Point(10.0, 20.0), New Point(110.0, 70.0))
        
		Dim center As Point = GetCenter(rect)
        
		Console.WriteLine("Center of the rectangle: ({0:F2}, {1:F2})", center.x, center.y)
    End Sub
End Class


' run:
'
' Center of the rectangle: (60.00, 45.00)
'

 



answered Jun 22, 2025 by avibootz

Related questions

1 answer 148 views
1 answer 121 views
1 answer 3,524 views
1 answer 92 views
1 answer 68 views
...