In this challenge you are required to let the user enter the coordinates of the centre of two circles and their respective radii.
You then have to calculate if the circles:
touch
do not touch
overlap
If we consider some simple examples then we could have the following:
For example:
Circle A has coordinates (1, 2) and a radius of 2
Circle B has coordinates (-1, -3) and a radius of 3
If we were to sketch this then we would have:
In this case we can cearly see that the 2 circles do not touch.
HINTS
This challenge involves the use of some mathematics. You will need to apply Pythagoras' Theorem.
You will need to calculate the distance between the centres of the 2 circles. In our example case we have the following:
What considerations will you need to give if the centres of the circles are vertically or horizontally opposite each other?
What tests will you need to make on the user inputs?
SAMPLES TO TRY
CIRCLE A | CIRCLE B | ||||
---|---|---|---|---|---|
CASE | COORDINATES | RADIUS | COORDINATES | RADIUS | RESULT |
1 | (2, 3) | 2 | (5, 7) | 3 | The circles touch |
2 | (-1, 0) | 1 | (5, 0) | 4 | The circles do not touch |
3 | (-4, 7) | 10 | (1, -5) | 5 | The circles overlap |
Would you like the solution to this challenge?
The full video solution together with downloadable code is available in the PHP for Beginners course.
This course is taught by Patrick Morrow, a national award-winning teacher, who is very active in the course and who provides full support.
To enrol in the course click here.