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







<< PreviousNext >>

Code for Detecting the Region Demarcated by a Circle in Python



Checking the Boundaries of a Circle in Python

From the equation (x - a)2 + (y - b)2 = r2 ;
It can be deduced that y = b ± √(r2 - (x - a)2) ;
And conversely x = a ± √(r2 - (y - b)2).

Hence, the boundaries of any circle lie in the range
b - √(r2 - (xexternal - a)2) ≤ y ≤ b + √(r2 - (xexternal - a)2)
and
a - √(r2 - (yexternal - b)2) ≤ x ≤ a + √(r2 - (yexternal - b)2)



Code to Detect Entrance into Circular Region in Python

To check for when a second graphic enters the circle, we will continually use the x position of this second graphic in the circle equation to detect when its y position lies between the up and down limits at the x position in question:
y2nd_img(top) > b - √(r2 - (x2nd_img - a)2)
and y2nd_img(bottom) < b + √(r2 - (x2nd_img - a)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) > a - √(r2 - (y2nd_img - b)2)
and x2nd_img(right) < a + √(r2 - (y2nd_img - b)2)

Detect Region

Create a new file; File, New File.
Call it CircularRegion.py.
Type out the adjoining Python / Turtle code for detecting the instance a travelling body crosses the boundary of a circle.










<< PreviousNext >>