The Moon’s surface is covered with thousands of craters. These are caused by asteroids and meteorites colliding with the lunar surface. In this challenge, we will use Python Turtle to create a drawing of the Moon with a random selection of craters.
This challenge consists of randomly positioning craters of various size (radius) on the surface of the Moon. Generating random (x,y) coordinates is fairly straight forward, however the challenge is to ensure that the craters fit within the disc representing the Moon:
We have started the code for you. You can complete the code in the first tab below. Alternatively you can compare two different approaches used to solve this challenge:
- Solution 1: Using polar coordinates and trigonometric formulas (SOHCAHTOA)
- Solution 2: Using Cartesian coordinates and Pythagoras’ Theorem
Lunar Craters ChallengeSolution #1 - Trigonometric FormulasSolution #2 - Pythagoras' Theorem
We have started the code for you, but with this code, the craters do not all fit inside the disc representing the Moon
. Your task is to adapt this code to make sure that all the craters fit inside the disc representing the surface of the Moon.
With this solution we will randomly generate polar coordinates
(Distance from the centre, angle). We will then apply trigonometric formulas (SOHCAHTOA) to convert these polar coordinates into Cartesian coordinates
Though this approach works, it tends to generate a higher density of craters towards the centre of the disc representing the Moon.
Our second solution, fully based on Cartesian coordinates solves this problem.
This solution consists of generating a set of random Cartesian coordinates (x,y)
. Before using these coordinates to draw a crater we calculate the distance between the centre of the disc representing the Moon and the randomly generated (x,y) coordinates. To do so we use Pythagoras’ Theorem.
If the distance is lower than (Radius of the Moon – Radius of Crater) then we keep these coordinates to draw our crater, otherwise we generate a new set of coordinates.
The solution for this challenge is available to full members!
Find out how to become a member:➤ Members Area