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







<< PreviousNext >>

Code for Detecting the Region Demarcated by a Quadratic Curve in Python



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.

Ball crosses line


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).

Detect Region

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.









<< PreviousNext >>