Checking the Boundaries of a Quadratic Curve in Python
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 Python
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 new file; File, New File.
Call it QuadraticRegion.py.
Type out the adjoining Python / Turtle code for detecting the instance a travelling
body crosses the boundary of a quadratic curve.
Python code for QuadraticRegion file
import math
from Facet import Template
def prep():
global ball_colour, x_ball, y_ball, xq_lb, xq_ub, 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
ball_colour = "#ffff00"
x_ball = 100 - scene.wnd_width/2; y_ball = 0
xq_start = -200; yq_start = scene.wnd_height/3
xq_min = 0; yq_min = -scene.wnd_height/3
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))
if a < 0:
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)
xq = xq_start; yq = yq_start
turtle.penup()
turtle.setposition(xq, yq)
turtle.pendown()
while xq <= -xq_start:
yq = a*xq*xq + b*xq + c
turtle.setposition(xq, yq)
xq += 1
screen.delay(20)
turtle.penup()
turtle.setposition(x_ball, y_ball)
turtle.setheading(0)
turtle.shape("turtle")
turtle.shapesize(diameter, diameter)
turtle.color(ball_colour, ball_colour)
turtle_radius = 10*turtle.shapesize()[1]
def play(x, y):
global x_ball, y_ball, ball_colour, xq_lb, xq_ub, turtle_radius
if x_ball < scene.wnd_width/2 - turtle_radius:
ball_colour = "#ffff00"
if (x_ball - turtle_radius <= xq_lb and x_ball + turtle_radius >= xq_lb) or\
(x_ball - turtle_radius <= xq_ub and x_ball + turtle_radius >= xq_ub):
ball_colour = "#ff0000"
elif x_ball - turtle_radius >= xq_lb and x_ball + turtle_radius <= xq_ub:
ball_colour = "#00ff00"
turtle.color(ball_colour, ball_colour)
turtle.setposition(x_ball, y_ball)
x_ball += 10
screen.ontimer(play(0,0), 500)
else:
x_ball = 100 - scene.wnd_width/2; y_ball = 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.