usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< PreviousNext >>

Code for Detecting the Region Demarcated by an Ellipse in Python



Checking the Boundaries of an Ellipse in Python

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 Python

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)

Detect Region

Create a new file; File, New File.
Call it EllipticalRegion.py.
Type out the adjoining Python / Turtle 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.









<< PreviousNext >>