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







<< PreviousNext >>

Code for Detecting the Region Demarcated by a Diagonal Line in Python



Checking the Sides of a Diagonal Line in Python

Consider the moving body (circle) from the previous demonstration (lesson).
We could put a straight diagonal line in front of the ball (circle) and perform a certain action (like changing the colour of the ball) just after the ball has crossed the diagonal line moving from left to right.

Ball crosses line


Code to Detect Crossing (Trespass) over Straight Line Region in Python

To check for when our ball crosses the straight line, we will continually check the x position (mid-point) of the ball against the x position gotten using the equation of the straight line at the same y position to as that of the ball.

If we designate the coordinates of the ball as (xb, yb), and those of the line at the same height as the ball as (xl, yl),
then,
         xl = myd + c
Ball (mid-point) has crossed line when xd >= xl.

Ball crosses line

Create a new file; File, New File.
Call it StraightLineRegion.py.
Type out the adjoining Pythonbody travel code for detecting the instance a travelling body crosses the path of a straight line.









<< PreviousNext >>