usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Detecting Circular Regions in VB.Net | VB.Net Windows Form Tutorial



Using the Circle Equation for Region Detection

In this tutorial, you'll learn how to detect a circular region in VB.Net using the circle equation. The equation of a circle, (x - a)² + (y - b)² = r², defines all points (x, y) that are exactly r units away from the center (a, b). This formula helps determine whether a point or moving object lies inside or outside a circular region on an VB.Net Windows Form. Understanding how to check whether a point or object lies inside a circle region is useful in VB.Net geometry programming, especially for animations, canvas graphics, and collision detection.


Understanding the Circle Equation | Maths Explanation for VB.Net Kids

As already explained extensively in the How to Draw and Animate a Circle in VB.Net tutorial, the equation of a circle with centre (a, b) and radius (r) is: (x - a)2 + (y - b)2 = r2 ;
It can be deduced that y = b ± √(r2 - (x - a)2) ;
And conversely x = a ± √(r2 - (y - b)2).

Hence, the boundaries of any circle lie in the range
b - √(r2 - (xexternal - a)2) ≤ y ≤ b + √(r2 - (xexternal - a)2)
and
a - √(r2 - (yexternal - b)2) ≤ x ≤ a + √(r2 - (yexternal - b)2)

In other words,
* If (x, y) satisfies this equation, the point lies on the circle.
* If (x - a)^2 + (y - b)^2 < r^2, the point is inside the circular region.
* If (x - a)^2 + (y - b)^2 > r^2, the point is outside the circle.


Algorithm to Detect Entrance into Circular Region in VB.Net

To detect when a second shape enters the circle, we use its coordinates in the circle equation to check if they fall within the upper, lower, left, and right boundaries:
That is, whether the y position of the second body lies between the top and bottom limits of the circle boundary at the x position of the second body:
y2nd_img(top) > b - √(r2 - (x2nd_img - a)2)
and y2nd_img(bottom) < b + √(r2 - (x2nd_img - a)2)
;
And at the same time, whether the x position of the second body lies between the left and right limits of the circle boundary at the y position of the second body:
x2nd_img(left) > a - √(r2 - (y2nd_img - b)2)
and x2nd_img(right) < a + √(r2 - (y2nd_img - b)2)

VB.Net circle region detection example on VB.Net windows form
Figure: VB.Net circle region detection example on VB.Net windows form

Create a new Visual Basic Windows Forms Application project ; call it Dymetric_VB.
Create 3 new VB.Net classes;
Call them Facet, Dymetric and CircularRegion.
Type out the adjoining VB.Net code for detecting the instance a travelling body crosses the boundary of a circle.


How the VB.Net Circular Region Detection Code Works

The code compares the distance of a point from the circle's centre with the radius. If the distance is smaller than or equal to the radius, the point is inside the circular region.

🟢 A green point shows it's inside the circular region.
🔴 A red point shows it's outside.

The code above demonstrates VB.Net circle collision detection, a common concept in canvas-based animations and game design. This example shows how maths meets programming - turning the circle equation into real-time VB.Net geometry detection.

Key Takeaways on Circular Region Detection in VB.Net

In this tutorial, you've learned that:

  • The circle equation defines a circular region mathematically.
  • With a few lines of VB.Net code, you can detect whether a point is inside or outside the circle.
  • This principle links senior secondary maths and practical VB.Net applications, preparing you for real-world coding projects.

With just a few lines of VB.Net, you've been able to check when a point enters or leaves a circular boundary - a technique useful in games, animations, and simulations. The tutorial also features a VB.Net canvas example that visualizes circle region detection in real time.


FAQs: Circle Equation and VB.Net

What is a circular region in VB.Net?

A circular region refers to the area within a circle defined by its radius on the VB.Net windows form. In VB.Net, you can detect whether a point or shape lies inside it using the circle equation.

How do you detect a circle boundary in VB.Net?

You can calculate the distance between a point and the circle's center and compare it to the radius - if the distance is less than the radius, the point is inside the circle.

Can this be used for games or simulations?

Yes! Circle region detection is common in VB.Net game development, collision detection, and animations.

Summary: Visualizing Circular Region in VB.Net

In this lesson, you've learnt how to detect a circular region in VB.Net using the circle equation from coordinate geometry: (x - a)² + (y - b)² = r².

This powerful formula helps determine whether a point or object is inside, on, or outside a circle. It connects senior secondary mathematics with VB.Net geometry programming through step-by-step examples and code.

By combining mathematics and VB.Net coding, you can easily detect when objects cross a circular boundary. This exercise strengthens your understanding of circle equations and introduces essential concepts in VB.Net graphics programming.



So! VB.Net Fun Practice Exercise - Detect Circular Region

As a fun practice exercise, try changing the values of (a), (b), (r), (x), and (y) to test different points and circle sizes. You can also extend this idea to moving body detection inside a circle, or collision detection in small games and interactive animations.







VB.Net Circular Boundary Window Display Code Stub

Public Class Form1

    Private form_details As New Facet
    Private action_class As New Dymetric

    Private Sub Form1_Load(sender As Object, e As EventArgsHandles MyBase.Load
        ' Fill in Form - Put button on form
        form_details.formFeatures(sender)
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgsHandles Me.Paint
        ' Colour button area
        form_details.decorateButtonArea(sender, e)

        ' Call MovingBody class into action
        action_class.decideAction(sender, Me.CreateGraphics(), form_details.CLICK_OCCURRED)

        ' Reset click variable
        form_details.CLICK_OCCURRED = False
    End Sub
End Class

VB.Net Circular Boundary Facet Window Code Stub

Public Class Facet

    Dim screen_rect As Rectangle
    Public CLICK_OCCURRED As Boolean = False

    Public Sub formFeatures(sender As Object)
        'Set window position, width and height
        screen_rect = Screen.PrimaryScreen.Bounds
        sender.SetDesktopBounds(0, 0, screen_rect.Width, screen_rect.Height)

        ' Set a display text
        sender.Text = "useOfMaths.com"

        ' Set a background colour
        sender.BackColor = System.Drawing.Color.LightGray

        ' Set an icon image
        Dim path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
        path = New Uri(path).LocalPath
        Try
            sender.Icon = New Icon(path & "\useOfMaths.ico")
        Catch ex As System.IO.FileNotFoundException
            ' Well, just go on and use default pic
        End Try

        '
        'create a button - response_btn
        '
        Dim response_btn As New Button()
        response_btn.BackColor = System.Drawing.Color.Magenta
        response_btn.ForeColor = System.Drawing.Color.Blue
        response_btn.Name = "response_btn"
        response_btn.SetBounds(CInt(Math.Round(screen_rect.Width / 2)) - 50, 5, 100, 40)
        response_btn.Text = "Move"
        sender.Controls.Add(response_btn)
        AddHandler response_btn.Click, AddressOf response_btn_Click
    End Sub

    Public Sub decorateButtonArea(sender As Object, e As PaintEventArgs)
        ' Draw a dotted line
        Dim pencil As New System.Drawing.Pen(System.Drawing.Color.Black)
        pencil.DashStyle = Drawing2D.DashStyle.DashDot
        pencil.Width = 5
        e.Graphics.DrawLine(pencil, 0, 50, sender.Width, 50)
        pencil.Dispose()

        ' Colour region
        Dim paint_brush As New System.Drawing.SolidBrush(System.Drawing.Color.Pink)
        e.Graphics.FillRectangle(paint_brush, 0, 0, sender.Width, 50)
        paint_brush.Dispose()
    End Sub

    Public Sub response_btn_Click(sender As Object, e As EventArgs)
        ' turn this on on every button click
        CLICK_OCCURRED = True
        sender.Refresh()
    End Sub
End Class

VB.Net Circular Boundary Code for Dymetric Class

Public Class Dymetric
    Private cycle_zone As New CircularRegion
    Private do_simulation = False

    ' decide what course of action to take
    Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
        If do_simulation And click_check Then
            ' do animation
            cycle_zone.play(sender, g)
            do_simulation = False
        Else
            ' Put ball on screen
            cycle_zone.prep(sender, g)
            do_simulation = True
        End If
    End Sub
End Class

VB.Net Animation Code for Circular Region Class

Public Class CircularRegion

    ' square coordinates
    Private x_square, y_square As Integer
    Private previous_x As Integer = 0
    Private previous_y As Integer = 0
    Private Const squareLENGTH = 100
    Dim square_pen As New System.Drawing.Pen(System.Drawing.Color.Yellow)

    ' circle variables
    Private a, b, r As Integer
    Private Const dotDIAMETER = 10

    Dim bg_colour As New System.Drawing.SolidBrush(System.Drawing.Color.LightGray)

    ' draw first appearance of square on the screen
    Public Sub prep(sender As Object, g As Graphics)
        x_square = 10
        y_square = Math.Round(sender.Height / 2)
        square_pen.Width = 5

        ' circle centre coordinates
        a = Math.Round(sender.Width / 2)
        b = Math.Round(sender.Height / 2)
        ' circle radius
        r = sender.Height / 3

        ' draw a circle
        g.DrawEllipse(Pens.Black, a - r, b - r, 2 * r, 2 * r)

        If previous_x > 0 Then
            ' clear previous square using background colour
            g.FillRectangle(bg_colour, previous_x - 5, previous_y - 5, squareLENGTH + 10, squareLENGTH + 10)
        End If
        ' draw square
        g.DrawRectangle(square_pen, x_square, y_square, squareLENGTH, squareLENGTH)
        previous_x = x_square
        previous_y = y_square
    End Sub

    ' repetitively clear and draw square on the screen - Simulate motion
    Public Sub play(sender As Object, g As Graphics)
        ' condition for continuing motion
        Do While x_square < sender.Width - squareLENGTH

            Dim square_left = x_square
            Dim square_right = x_square + squareLENGTH
            Dim square_top = y_square
            Dim square_bottom = y_square + squareLENGTH
            ' determinants for each side of the square
            Dim x_left_det = Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_left - a), 2)))
            Dim x_right_det = Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_right - a), 2)))
            Dim y_up_det = Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_top - b), 2)))
            Dim y_down_det = Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_bottom - b), 2)))

            ' check the bounds of the circle
            square_pen.Dispose()
            ' yellow outside the circle
            square_pen = New System.Drawing.Pen(System.Drawing.Color.Yellow)
            If square_top > b - x_left_det And square_bottom < b + x_left_det _
                And square_top > b - x_right_det And square_bottom < b + x_right_det _
                And square_left > a - y_up_det And square_right < a + y_up_det _
                And square_left > a - y_down_det And square_right < a + y_down_det Then
                ' green inside the circle
                square_pen = New System.Drawing.Pen(System.Drawing.Color.Green)
            End If
            square_pen.Width = 5

            ' clear previous square using background colour
            g.FillRectangle(bg_colour, previous_x - 5, previous_y - 5, squareLENGTH + 10, squareLENGTH + 10)
            ' redraw square
            g.DrawRectangle(square_pen, x_square, y_square, squareLENGTH, squareLENGTH)

            previous_x = x_square
            x_square += 10
            ' take a time pause
            Threading.Thread.Sleep(50)
        Loop
    End Sub
End Class





<< PreviousNext >>