# Using Python to create a solar system
If anyone is looking for a fun exercise to flex their #Python #coding fingers…
Using just gravitational attraction between bodies, you can create your own 2D solar system with as many stars and planets as you want. Here's a binary star system with some relatively stable planets
Here's the article, including a detailed step-by-step tutorial, if you want to read more: **[Simulating Orbiting Planets in a Solar System Using Python](https://thepythoncodingbook.com/2021/09/29/simulating-orbiting-planets-in-a-solar-system-using-python-orbiting-planets-series-1/)**
#Python #simulation #animation
…and there's also a 3D version _(next post)_
Addendum (some sad news): Since taking those pictures for the article, that sole remaining espresso cup has suffered this fate
I was told I can't say Rest In Pieces
— 2 —
You press the "On" button—this is equivalent to calling the function.
You can almost see the similarity between the typical "On" button and the parentheses ( ) used to call a function in Python!
3/
As it's time for my morning coffee (the coffee not the biscuits), it's as good a time as any to share my one of my favourite #programming analogies
_(narrated from a #Python-viewpoint but general enough for #coding in general)_
**The Coffee Machine - Function analogy**
Let's make some coffee…
_[read on]_
/1
#Python is for serious stuff, sure, but one of the most fun modules is the `turtle` module, but…
I know, I can hear you say: "That's just for drawing simple, boring drawings, right"
Think again! Here's a great learning project that is not merely a "boring set of squares!"
You can follow the detailed step-by-step tutorial here [WARNING: game is addictive and may adversely affect your productivity!]
https://thepythoncodingbook.com/2022/04/24/python-lunar-landing-game-using-turtle-tutorial/
And therefore, you can reconstruct the image by adding all of those sinusoidal gratings together.
The more gratings you add, the closer the result is to the actual image
/13
Now, here's the "magical" part of #Fourier theory.
*Any* image is made up of lots of sinusoidal gratings. So, the 2D Fourier Transform of an image gives you thousands of pairs of dots, and each pair represent a sinusoidal grating.
/12
Now, if you have lots of gratings superimposed on each other, the #FourierTransform gives you a pair of dots for each of the components
/11
You can find the parameters of a sinusoidal grating by using the 2D #FourierTransform.
The dots shown contain the amplitude and phase of the grating. Their position from the centre gives the frequency, and their orientation represents the orientation of the grating.
/10
You can create a 2D sinusoidal grating in #Python using #NumPy and display it using #matplotlib
/8
There's one more parameter that defines a sinusoidal grating: the phase. Gratings with a different phase are shifted with respect to each other…
/7
It’s called a sinusoidal grating because the grayscale values vary according to the sine function.
If you plot the values along a horizontal line of the grating, you’ll get a plot of a sine function
/3
**Any** image can be reconstructed from a series of sinusoidal gratings.
A sinusoidal grating looks like this…
/2
• Rethinking how to teach programming – I prefer the friendly, relaxed approach when communicating about Python programming
• I write about Python on The Python Coding Book blog and on Real Python
• Former Physicist
• Expect posts on scientific and numerical programming –> NumPy, Matplotlib and friends!