How to check if a point is inside a rectangle in VB.NET

1 Answer

0 votes
Imports System

Class Program
    Structure Point
        Public X, Y As Double

        Public Sub New(ByVal x As Double, ByVal y As Double)
            X = x
            Y = y
        End Sub
    End Structure

    Structure Rectangle
        Public TopLeft As Point
        Public BottomRight As Point

        Public Sub New(ByVal topLeft As Point, ByVal bottomRight As Point)
            TopLeft = topLeft
            BottomRight = bottomRight
        End Sub
    End Structure

    Private Shared Function IsPointInsideRectangle(ByVal p As Point, ByVal rect As Rectangle) As Boolean
        Return p.X >= rect.TopLeft.X AndAlso p.X <= rect.BottomRight.X AndAlso p.Y >= rect.TopLeft.Y AndAlso p.Y <= rect.BottomRight.Y
    End Function

    Public Shared Sub Main()
        Dim rect As Rectangle = New Rectangle(New Point(0.0, 0.0), New Point(7.0, 7.0))
        Dim p As Point = New Point(3.0, 2.0)

        If IsPointInsideRectangle(p, rect) Then
            Console.WriteLine("The point is inside the rectangle.")
        Else
            Console.WriteLine("The point is outside the rectangle.")
        End If
    End Sub
End Class



' run:
'
' The point is inside the rectangle.
'

 



answered Jun 22, 2025 by avibootz
...