Coding Challenge #139: Calculating Digits of Pi with Collisions
TLDRIn this coding challenge, the creator reminisces about a 2019 video from 3Blue1Brown that demonstrated how two bouncing blocks could produce the digits of pi. Inspired, the creator attempts to replicate this phenomenon using JavaScript and object-oriented programming within the p5 Web Editor. The video documents the process of setting up blocks, implementing collision detection, and applying physics for elastic collisions. It explores the limitations of Euler integration and attempts to improve accuracy by increasing the number of time steps in the simulation. The result is a visually engaging and educational journey that successfully visualizes pi's digits through code.
Takeaways
- π The video script recalls January 2019 and the release of a video by 3Blue1Brown about a counting puzzle involving blocks bouncing and producing the digits of pi.
- π The video was educational, teaching math concepts through the unexpected appearance of pi in a physical collision scenario.
- π₯ 3Blue1Brown is a favored YouTube channel of the speaker for learning math and drawing inspiration.
- π€ The speaker attempts to recreate the 3Blue1Brown experiment using coding, specifically JavaScript, to simulate the collision of two blocks and generate digits of pi.
- π The script discusses the physics of elastic collisions, including the conservation of momentum and kinetic energy, which are key to understanding the block collisions.
- πΎ The coding process involves creating a Block class, setting velocities, and writing functions to check for collisions and bounce the blocks.
- π’ The script explores the concept of Euler integration and its limitations in accurately simulating physical movements over time.
- π΅ The video includes the addition of sound effects to enhance the visual simulation of the blocks 'clacking' upon collision.
- π The speaker experiments with different techniques, such as increasing the number of time steps in the simulation to improve accuracy.
- π The script highlights the challenge of achieving high precision in a JavaScript simulation, especially when attempting to calculate many digits of pi.
- π The video concludes with the successful demonstration of calculating 11 digits of pi using the simulation, showcasing the power of creative coding and math.
Q & A
What was the main topic of the video about?
-The main topic of the video was demonstrating how the digits of pi can appear from the number of times two blocks collide in a simulation, inspired by a video from the 3Blue1Brown YouTube channel.
What is the name of the YouTube channel that inspired the video?
-The YouTube channel that inspired the video is called 3Blue1Brown.
What concept from physics does the video touch upon?
-The video touches upon the concept of perfectly elastic collisions in physics.
What is the significance of the number pi in the video?
-The number pi is significant because the number of times the blocks collide in the simulation results in the digits of pi, showcasing a surprising connection between a mathematical constant and a physical phenomenon.
What is the role of Euler integration in the video?
-Euler integration is used in the video to simulate the motion of the blocks over time, but it is also discussed as a limitation due to its inaccuracy with larger time steps.
What is the issue with using Euler integration for the simulation?
-The issue with Euler integration in the simulation is that it can become inaccurate when using large time steps, leading to a less realistic representation of the physical phenomenon.
What is the 'boilerplate code' mentioned in the script?
-The 'boilerplate code' refers to the basic, reusable code that the creator uses in many of their examples to save time and avoid retyping common functionalities.
What does the video creator attempt to do in the coding challenge?
-The video creator attempts to code their own version of the 3Blue1Brown's collision simulation in JavaScript, aiming to replicate the appearance of pi's digits from the collisions.
What is the role of the 'Block' class in the code?
-The 'Block' class is used to define the properties and behaviors of the blocks in the simulation, including their position, size, velocity, and methods for updating their state and handling collisions.
How does the video address the problem of accuracy in the simulation?
-The video addresses the problem of accuracy by increasing the number of time steps in the simulation, which allows for smaller increments in time and a more precise representation of the blocks' motion.
What is the significance of the paper 'Playing Pool With Pi' mentioned in the video?
-The paper 'Playing Pool With Pi' is significant as it explores the concept of pi appearing from billiard ball collisions, providing a theoretical foundation for the phenomenon demonstrated in the video.
How does the video creator handle the collision with a wall?
-The video creator handles the collision with a wall by implementing a function that reverses the velocity of the block upon collision, simulating a perfectly elastic collision with an immovable object of infinite mass.
What is the final outcome of the coding challenge in the video?
-The final outcome of the coding challenge is successfully simulating the appearance of the first 11 digits of pi through the collisions of two blocks in a browser using JavaScript.
Outlines
π Reminiscing January 2019's Mathematical Marvel
The speaker fondly recalls January 2019 and a captivating video from the educational YouTube channel 3Blue1Brown that explained a unique counting puzzle involving two bouncing blocks. The video demonstrated how the number of times the blocks collided could reveal digits of pi, an intriguing concept that combined physics and mathematics. The speaker also references a 2003 paper by G. Galperin and a Numberphile video for further exploration of the phenomenon. The channel's focus on coding is then introduced, with the speaker's intention to code a simulation of the 3Blue1Brown video in JavaScript, aiming to replicate the appearance of pi through collisions.
π― Setting Up the Coding Challenge
The speaker begins the coding challenge by establishing a basic object-oriented system in JavaScript, defining a Block class with properties for position, size, and velocity. The initial setup includes drawing the blocks and loading a sound effect. The speaker then proceeds to write functions for updating block positions based on velocity and checks for collisions between the two blocks. A 'collide' function is implemented to determine if the blocks intersect, crucial for the simulation's core mechanics.
π Implementing Elastic Collision Dynamics
The coding challenge continues with the implementation of elastic collision physics between the two blocks. The speaker introduces the concept of mass and uses formulas from physics to calculate new velocities after a collision, ensuring the conservation of momentum and kinetic energy. The 'bounce' function is developed to update the velocities of the blocks based on their masses and initial velocities. The speaker also discusses the implications of equal versus unequal masses in the collision outcome.
ποΈ Adding Realism with Wall Collisions and Sound Effects
The simulation becomes more realistic as the speaker adds a 'hitWall' function to simulate collisions with an immovable object, treating it as an object with infinite mass. The block's velocity is reversed upon hitting the wall, mimicking a perfectly elastic collision. Additionally, sound effects are integrated to enhance the auditory experience of the simulation, with the clacking sound playing upon collisions.
π’ Counting Collisions to Reveal Pi
The speaker introduces a counting mechanism to tally the number of collisions, aiming to observe the digits of pi emerge from the simulation. The count is displayed on the screen, and the speaker experiments with adjusting the mass of the blocks to influence the count's progression. The speaker also contemplates the stopping criteria for the count and the limitations of Euler integration in the simulation's accuracy.
π οΈ Refining the Simulation with Smaller Time Steps
To improve the simulation's accuracy, the speaker implements a technique using smaller time steps, effectively increasing the number of updates per frame to more closely approximate continuous time. This approach helps in creating a more precise simulation, allowing for a better representation of the physical behavior of the blocks. The speaker also discusses the trade-off between accuracy and performance, especially as the number of time steps increases.
πΆ Achieving High-Precision Pi Digits with Sound Integration
The speaker successfully generates multiple digits of pi through the simulation and contemplates the integration of sound effects that play upon collisions. The challenge of managing the sound playback within the increased number of time steps is addressed, ensuring that the clacking sound enhances rather than distracts from the simulation. The speaker demonstrates achieving up to seven digits of pi with a high number of time steps, showcasing the effectiveness of the refined simulation.
π Pushing the Limits to Ten Million Time Steps
In an ambitious attempt, the speaker pushes the simulation to process up to ten million time steps per draw, resulting in a significant increase in computational demand but also in the accuracy of the simulation. The speaker successfully achieves 11 digits of pi through this method, demonstrating the potential of the approach despite the computational intensity and slowdown in the animation's frame rate.
π Reflecting on the Coding Challenge and Future Improvements
The speaker concludes the coding challenge by reflecting on the process and outcomes, acknowledging the successful generation of 11 digits of pi through elastic collisions. They also consider alternative methods and optimizations for the simulation, such as using BigDecimal for higher precision or exploring different integration techniques. The speaker invites the audience to share their thoughts and solutions on thecodingtrain.com and hints at potential future live streams and coding attempts.
π Celebrating Pi Day with a Creative Coding Challenge
The speaker ends the video with a light-hearted celebration of Pi Day, expressing excitement about the successful coding challenge and the creative approach to generating digits of pi. They humorously mention Tau Day and the arrival of summer, signing off with a cheerful tune that encapsulates the joy of mathematical discovery and the fun of coding.
Mindmap
Keywords
π‘January 2019
π‘3Blue1Brown
π‘Elastic Collision
π‘Pi (Ο)
π‘Coding Challenge
π‘Object-Oriented System
π‘Euler Integration
π‘Collision Detection
π‘Optics
π‘JavaScript
π‘p5.js
π‘Floating Point Numbers
π‘Time Steps
Highlights
In January 2019, a video from 3Blue1Brown on a counting puzzle involving blocks and pi was released.
The video demonstrated an unexpected connection between the number of times two blocks clacked and the digits of pi.
The concept of the video was not new, with a 2003 paper by G. Galperin titled 'Playing Pool With Pi' discussing a similar phenomenon.
The speaker plans to code a simulation of the 3Blue1Brown video in JavaScript to explore the pi digits through collisions.
A basic object-oriented system is established with a Block class and properties for position, size, and velocity.
A collision detection function is implemented to determine when two blocks intersect.
The bounce function is created to simulate perfectly elastic collisions between blocks with different masses.
A hitWall function is introduced to handle collisions with an immovable object, simulating a wall.
The speaker attempts to count the number of collisions to generate digits of pi, facing challenges with Euler integration.
To improve accuracy, smaller time steps are introduced in the simulation, reducing the inaccuracies of Euler integration.
The speaker successfully generates multiple digits of pi using the simulation, highlighting the power of small time steps.
The use of sound in the simulation adds to the user experience, with the clacking sound playing upon collisions.
The speaker discusses the limitations of Euler integration and considers alternative integration techniques for more accurate simulations.
The speaker successfully achieves 10 digits of pi through the simulation, demonstrating the potential of the method.
The video concludes with the speaker reflecting on the challenge and inviting the audience to share their thoughts on optimizing the simulation.
Links to related resources, including the 3Blue1Brown video series and other attempts at the challenge, are provided for further exploration.
The speaker humorously acknowledges the upcoming Tau Day and the passing of time, ending the video on a light-hearted note.
Transcripts
Browse More Related Video
5.0 / 5 (0 votes)
Thanks for rating: