Checking the Boundaries of a Quadratic Curve in Visual Basic
Remember that 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 and check for when
it enters the region of our curve.
Code to Detect Entrance into Quadratic Region in Visual Basic
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).
Create a class;
Call it and QuadraticRegion.
Type out the adjoining VB.Net code for detecting the instance a travelling
body crosses the boundary of a quadratic curve.
Dymetric class
Public Class Dymetric
Private quad_region As New QuadraticRegion
Private do_simulation = False
Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
If do_simulation And click_check Then
quad_region.play(sender, g)
do_simulation = False
Else
quad_region.prep(sender, g)
do_simulation = True
End If
End Sub
End Class
VB.Net code for QuadraticRegion class
Public Class QuadraticRegion
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)
Private xq_start, yq_start, xq_min, yq_min, xq_stop, x, y As Integer
Private xq_lb, xq_ub As Double
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)
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
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
xq_lb = (-b + discriminant) / (2 * a)
xq_ub = (-b - discriminant) / (2 * a)
Else
xq_lb = (-b - discriminant) / (2 * a)
xq_ub = (-b + discriminant) / (2 * a)
End If
For x = xq_start To xq_stop
y = CInt(Math.Round(a * x * x + b * x + c))
g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER)
Next x
If previous_x > 0 Then
g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
End If
g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER)
previous_x = x_ball
previous_y = y_ball
End Sub
Public Sub play(sender As Object, g As Graphics)
Do While x_ball < sender.Width - ballDIAMETER
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
ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Red)
ElseIf x_ball >= xq_lb And x_ball + ballDIAMETER <= xq_ub Then
ball_colour = New System.Drawing.SolidBrush(System.Drawing.Color.Green)
End If
g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER)
g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER)
previous_x = x_ball
x_ball += 5
Threading.Thread.Sleep(50)
Loop
End Sub
End Class