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







<< PreviousNext >>

Detect a Point Inside a Quadratic Region Using VB.Net | Senior Secondary Maths Tutorial



Understanding the Quadratic Region Concept | Maths Explanation for VB.Net Kids

In this tutorial, you'll learn how to detect the region under a quadratic curve using VB.Net. The curve is defined by the equation y = a x² + b x + c, and we'll use the discriminant method to find when a point or object lies within the quadratic region. This concept helps students connect algebraic reasoning with programming and visualization using the VB.Net windows form.

What is a Quadratic Region? | Maths Explanation for VB.Net Kids

A quadratic region in VB.Net represents the area bounded by a quadratic curve. Every quadratic equation has two x-values (roots) for any given y - except at its turning point (maximum or minimum).
We can use these roots as boundaries for region detection.

More technically, a quadratic region is the area defined by a quadratic inequality such as y ≤ ax² + bx + c.
This concept is useful in computer graphics, physics simulations, and quadratic curve collision detection (JS) projects.


Checking the Boundaries of a Quadratic Curve in VB.Net

To visualize the region under a quadratic curve, we'll use VB.Net to calculate the upper and lower limits dynamically. This makes it possible to detect when an object (like a moving ball) enters or exits the quadratic region.

As discussed in the Animating along a Straight Line in VB.Net tutorial, any quadratic equation always have two roots for any value of y (except at it's maximum or minimum point).
All we need to do is use these two roots (x values) as boundaries for our check.
y = ax2 + bx + c
ax2 + bx + (c-y) = 0

x   =    -b ± √(b2 - 4a(c-y))
2a

Our range will then be:

-b - √(b2 - 4a(c-y))   ≤   x   ≤   -b + √(b2 - 4a(c-y))
2a 2a

where a, b, and c are constants.
We will reuse the moving ball graphic from the Solving and Animating a Quadratic Equation in VB.Net tutorial and check for when it enters the region of our curve.

visualizing quadratic curve region in VB.Net and ball trajectory crossing into it on VB.Net windows form
Figure: Visualizing quadratic curve region in VB.Net and an object trajectory passing through it on VB.Net windows form.

VB.Net Code Example: Detecting Entrance into a Quadratic Region

To check for when our ball enters the quadratic curve, we will continually check the x position of the ball against the x position gotten using the quadratic equation at the same y position as that of the ball.

We'll designate the coordinates of the ball as (xb, yb), and those of the curve as (xq, yq).

Canvas quadratic region detection VB.Net example
Figure: Detecting and visualizing the quadratic region on a VB.Net windows form using VB.Net.

To detect a point inside a parabola using VB.Net, you can compare its coordinates to the quadratic curve. We'll determine whether a moving ball lies within this region by solving for x using the quadratic formula. If y is less than or equal to the value of the quadratic equation, the point lies within the region.

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


Summary: Detecting Quadratic Boundaries with VB.Net

In this senior secondary VB.Net math tutorial, you've learnt how to identify whether a moving point lies inside a quadratic region. We've used simple algebra and the VB.Net canvas to visualize and draw the quadratic region bounded by a parabolic curve.

Formula Recap:

The general form of a quadratic equation is y = a x² + b x + c. To find the region under the curve, we can rearrange this equation to get a x² + b x + (c - y) = 0 and use the discriminant D = b² - 4a(c - y).

For any given y-value, if D is positive, the quadratic crosses that y-level at two x-values. The region between these two x-values represents the quadratic region.

y = ax² + bx + c
⟹ ax² + bx + (c - y) = 0
⟹ x = (-b ± √(b² - 4a(c - y))) / 2a

Thus, the quadratic region boundaries are:
(-b - √(b² - 4a(c - y))) / 2a ≤ x ≤ (-b + √(b² - 4a(c - y))) / 2a

Understanding how to compute and visualize quadratic regions in VB.Net bridges mathematical theory and practical coding. It helps students apply concepts from coordinate geometry in a real-world programming context.

Applying the Line Region Detection Logic in VB.Net

This tutorial teaches you to:

  • Compute the region under a quadratic function in VB.Net
  • Use real-time region detection to track an object's position
  • Apply mathematical concepts like discriminants and boundaries in interactive graphics

To determine if a point lies inside a quadratic region, we've used a VB.Net quadratic region detection function. This approach is often used in interactive canvas demos and collision detection algorithms.



So! VB.Net Fun Practice Exercise - Detect Quadratic curve Boundary

As a fun practice exercise, try experimenting with different coefficients (a, b, and c) to see how the quadratic region changes shape. You can also animate a point moving across the screen to test when it enters or exits the region on the VB.Net windows form. Experiment with different equations and visualize how region boundaries change dynamically in VB.Net. This is a great way to explore the relationship between algebra and geometry in senior secondary mathematics.







VB.Net Quadratic Curve 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 Quadratic Curve 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 Quadratic Curve Boundary Code for Dymetric Class

Public Class Dymetric
    Private quad_region As New QuadraticRegion
    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
            quad_region.play(sender, g)
            do_simulation = False
        Else
            ' Put ball on screen
            quad_region.prep(sender, g)
            do_simulation = True
        End If
    End Sub
End Class

VB.Net Animation Code for Quadratic Region Class

Public Class QuadraticRegion

    ' ball coordinates
    Private x_ball, y_ball As Integer
    Private previous_x As Integer = 0
    Private previous_y As Integer = 0
    Private Const ballDIAMETER = 80
    Dim ball_colour As New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)

    ' quadratic variables
    Private xq_start, yq_start, xq_min, yq_min, xq_stop, x, y As Integer
    Private xq_lb, xq_ub As Double ' curve lower and upper boundary
    Private a, b, c, discriminant As Double
    Private Const dotDIAMETER = 5
    Dim dot_colour As New System.Drawing.SolidBrush(System.Drawing.Color.Black)

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

    ' draw first appearance of ball on the screen
    Public Sub prep(sender As Object, g As Graphics)
        x_ball = 10
        y_ball = Math.Round(sender.Height / 2)
        ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)

        xq_start = Math.Round(sender.Width / 2) - 200
        yq_start = 70
        xq_min = Math.Round(sender.Width / 2)
        yq_min = sender.Height - 70
        xq_stop = Math.Round(sender.Width / 2) + 200

        ' constants
        a = (yq_start - yq_min) / Math.Pow((xq_start - xq_min), 2)
        b = -2 * a * xq_min
        c = yq_min + a * Math.Pow(xq_min, 2)

        discriminant = Math.Sqrt(b * b - 4 * a * (c - (y_ball - ballDIAMETER / 2)))
        If a < 0 Then ' a is negative
            xq_lb = (-b + discriminant) / (2 * a) ' lower boundary
            xq_ub = (-b - discriminant) / (2 * a) ' upper boundary
        Else
            xq_lb = (-b - discriminant) / (2 * a) ' lower boundary
            xq_ub = (-b + discriminant) / (2 * a) ' upper boundary
        End If

        ' draw quadratic curve
        For x = xq_start To xq_stop
            y = CInt(Math.Round(a * x * x + b * x + c))
            ' redraw dot
            g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER)
        Next x

        If previous_x > 0 Then
            ' clear previous ball using background colour
            g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
        End If
        ' draw ball
        g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER)
        previous_x = x_ball
        previous_y = y_ball
    End Sub

    ' repetitively clear and draw ball on the screen - Simulate motion
    Public Sub play(sender As Object, g As Graphics)
        ' condition for continuing motion
        Do While x_ball < sender.Width - ballDIAMETER
            ' yellow outside the quadratic region
            ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)
            If (x_ball <= xq_lb And x_ball + ballDIAMETER >= xq_lb) _
                Or (x_ball <= xq_ub And x_ball + ballDIAMETER >= xq_ub) Then
                ' red on the quadratic outline
                ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Red)
            ElseIf x_ball >= xq_lb And x_ball + ballDIAMETER <= xq_ub Then
                ' green inside the quadratic region
                ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Green)
            End If

            ' clear previous ball using background colour
            g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
            ' redraw ball
            g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER)

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





<< PreviousNext >>