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)
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.
Python code for EllipticalRegion file
import math
from Facet import Template
def prep():
global sq_colour, x_square, y_square, h, k, a, b, turtle_radius
scene.button.onrelease(play)
turtle.penup()
turtle.setposition(scene.button.xcor(), scene.button.ycor()-10)
turtle.pendown()
turtle.write("Move", align="center", font=("Arial",16,"bold"))
diameter = 5
sq_colour = "#ffff00"
x_square = 50 - scene.wnd_width/2; y_square = 0
h = 0
k = 0
a = scene.wnd_width / 3
b = scene.wnd_height / 3
x_dot = h - a; y_dot = k
turtle.penup()
turtle.color("red")
turtle.setposition(x_dot, y_dot)
turtle.pendown()
while x_dot < h + a:
y_dot = k - (b/a)*math.sqrt(math.pow(a, 2) - math.pow((x_dot - h), 2))
turtle.setposition(x_dot, y_dot)
y_dot = k + (b/a)*math.sqrt(math.pow(a, 2) - math.pow((x_dot - h), 2))
turtle.setposition(x_dot, y_dot)
x_dot += 1
screen.delay(20)
turtle.penup()
turtle.setposition(x_square, y_square)
turtle.setheading(0)
turtle.shape("turtle")
turtle.shapesize(diameter, diameter)
turtle.color(sq_colour, sq_colour)
turtle_radius = 10*turtle.shapesize()[1]
def play(x, y):
global sq_colour, x_square, y_square, h, k, a, b, turtle_radius
if x_square < scene.wnd_width/2 - turtle_radius:
square_left = x_square - turtle_radius
square_right = x_square + turtle_radius
square_top = y_square + turtle_radius
square_bottom = y_square - turtle_radius
x_left_det = (b/a)*math.sqrt(abs(math.pow(a, 2) - math.pow((square_left - h), 2)))
x_right_det = (b/a)*math.sqrt(abs(math.pow(a, 2) - math.pow((square_right - h), 2)))
y_up_det = (a/b)*math.sqrt(abs(math.pow(b, 2) - math.pow((square_top - k), 2)))
y_down_det = (a/b)*math.sqrt(abs(math.pow(b, 2) - math.pow((square_bottom - k), 2)))
sq_colour = "#ffff00"
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:
sq_colour = "#00ff00"
turtle.color(sq_colour, sq_colour)
turtle.setposition(x_square, y_square)
x_square += 10
screen.ontimer(play(0,0), 500)
else:
x_square = 50 - scene.wnd_width/2; y_square = 0
screen.mainloop()
scene = Template();
turtle = scene.controlButtons()
screen = scene.screen
prep()
Important: When trying to click on the button to get things
started, you might need to click away from the button text.