Checking the Boundaries of an Ellipse in Visual Basic
From the equation
(x - h)2 |
+ |
(y - k)2 |
= 1 |
a2 |
b2 |
It can be deduced that
y = k ± b/a√(a2 - (x - h)2)
;
And conversely
x = h ± a/b√(b2 - (y - k)2)
Hence, the boundaries of any circle lie in the range
y ≥ k - b/a√(a2 - (xexternal - h)2);
y ≤ k + b/a√(a2 - (xexternal - h)2)
and
x ≥ h - a/b√(b2 - (yexternal - k)2);
x ≤ h + a/b√(b2 - (yexternal - k)2)
Code to Detect Entrance into an Elliptical Region in Visual Basic
To check for when a second graphic enters the ellipse,
we will continually use the x position
of this second graphic in the ellipse equation to detect
when its y position lies between the up and down
limits at the x position in question:
y2nd_img(top) >
k - b/a√(a2 - (x2nd_img - h)2)
and y2nd_img(bottom) <
k + b/a√(a2 - (x2nd_img - h)2)
;
At the same time, we will use the y position
of the second graphic in the circle equation to detect
when its x position lies between the left and
right limits at the y position in question:
x2nd_img(left) >
h - a/b√(b2 - (y2nd_img - k)2)
and x2nd_img(right) <
h + a/b√(b2 - (y2nd_img - k)2)
Create a new class;
Call it EllipticalRegion.
Type out the adjoining VB.Net code for detecting the instance a travelling
body crosses the boundary of an ellipse.
By The Way: Notice how the equations for a circle
are similar to those of an ellipse;
No surprise there!
A circle is just an ellipse in its simplest form.
Dymetric class
Public Class Dymetric
Private ellipse_zone As New EllipticalRegion
Private do_simulation = False
Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
If do_simulation And click_check Then
ellipse_zone.play(sender, g)
do_simulation = False
Else
ellipse_zone.prep(sender, g)
do_simulation = True
End If
End Sub
End Class
VB.Net code for EllipticalRegion class
Public Class EllipticalRegion
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)
Private h, k, a, b As Integer
Private Const dotDIAMETER = 10
Dim bg_colour As New System.Drawing.SolidBrush(System.Drawing.Color.LightGray)
Public Sub prep(sender As Object, g As Graphics)
x_square = 10
y_square = Math.Round(sender.Height / 2)
square_pen.Width = 5
h = Math.Round(sender.Width / 2)
k = Math.Round(sender.Height / 2)
a = sender.Width / 3
b = sender.Height / 3
g.DrawEllipse(Pens.Black, h - a, k - b, 2 * a, 2 * b)
If previous_x > 0 Then
g.FillRectangle(bg_colour, previous_x - 5, previous_y - 5, squareLENGTH + 10, squareLENGTH + 10)
End If
g.DrawRectangle(square_pen, x_square, y_square, squareLENGTH, squareLENGTH)
previous_x = x_square
previous_y = y_square
End Sub
Public Sub play(sender As Object, g As Graphics)
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
Dim x_left_det = Math.Round((b / a) * Math.Sqrt(Math.Pow(a, 2) - Math.Pow((square_left - h), 2)))
Dim x_right_det = Math.Round((b / a) * Math.Sqrt(Math.Pow(a, 2) - Math.Pow((square_right - h), 2)))
Dim y_up_det = Math.Round((a / b) * Math.Sqrt(Math.Pow(b, 2) - Math.Pow((square_top - k), 2)))
Dim y_down_det = Math.Round((a / b) * Math.Sqrt(Math.Pow(b, 2) - Math.Pow((square_bottom - k), 2)))
square_pen = New System.Drawing.Pen(System.Drawing.Color.Yellow)
If square_top > k - x_left_det And square_bottom < k + x_left_det _
And square_top > k - x_right_det And square_bottom < k + x_right_det _
And square_left > h - y_up_det And square_right < h + y_up_det _
And square_left > h - y_down_det And square_right < h + y_down_det Then
square_pen = New System.Drawing.Pen(System.Drawing.Color.Green)
End If
square_pen.Width = 5
g.FillRectangle(bg_colour, previous_x - 5, previous_y - 5, squareLENGTH + 10, squareLENGTH + 10)
g.DrawRectangle(square_pen, x_square, y_square, squareLENGTH, squareLENGTH)
previous_x = x_square
x_square += 10
Threading.Thread.Sleep(50)
Loop
End Sub
End Class