๐ Euclidean Distance Grid using NumPy
This tutorial demonstrates how to compute a 2D grid where each value represents the Euclidean distance from the origin (0, 0). This is a foundational concept used in mathematics, physics, and computer graphics.
๐ Table of Contents
- Concept of Distance
- Mathematical Formula
- Step-by-Step Implementation
- Code Example
- Output
- Deep Explanation
- Applications
๐ What is Euclidean Distance?
Euclidean distance is the straight-line distance between two points in space. It is the most intuitive notion of distance.
๐ Mathematical Formula
The Euclidean distance from the origin is:
\[ d = \sqrt{x^2 + y^2} \]
This formula comes from the Pythagorean theorem.
๐ Expand: Why this formula works
If you imagine a right triangle:
- Horizontal side = x
- Vertical side = y
- Hypotenuse = distance
\[ d^2 = x^2 + y^2 \]
Taking square root gives the distance.
๐ง Step-by-Step Approach
- Define a function that computes distance
- Use NumPy to generate coordinates automatically
- Apply the function to every grid point
๐ป Code Example
import numpy as np
def euclidean_distance(x, y):
return np.sqrt(x**2 + y**2)
distance_grid = np.fromfunction(euclidean_distance, (5, 5))
print(distance_grid)
๐ฅ Output
[[0. 1. 2. 3. 4. ] [1. 1.41421356 2.23606798 3.16227766 4.12310563] [2. 2.23606798 2.82842712 3.60555128 4.47213595] [3. 3.16227766 3.60555128 4.24264069 5. ] [4. 4.12310563 4.47213595 5. 5.65685425]]
๐ Deep Explanation
1. How np.fromfunction Works
It creates arrays by calling a function with coordinate indices.
Internally:
\[ (x, y) = (0,0), (0,1), (0,2), ... \]
Each coordinate is passed into the function.
2. Vectorization
NumPy computes all values at once using vectorized operations.
\[ \text{Result} = \sqrt{X^2 + Y^2} \]
3. Grid Interpretation
Each value represents radial distance from center.
๐ Expand: Visualization Insight
If plotted as an image:
- Center = dark (low values)
- Edges = bright (high values)
๐ Additional Math Insights
Distance in 3D
\[ d = \sqrt{x^2 + y^2 + z^2} \]
General n-Dimensional Distance
\[ d = \sqrt{\sum_{i=1}^{n} x_i^2} \]
Gradient of Distance Field
\[ \nabla d = \left(\frac{x}{\sqrt{x^2+y^2}}, \frac{y}{\sqrt{x^2+y^2}}\right) \]
This is useful in physics and graphics.
๐ Real-Life Applications
- Geography: Distance heatmaps
- Physics: Potential fields
- Computer Graphics: Distance fields
- Machine Learning: Feature distances
๐ Expand: Distance Fields in Graphics
Distance fields are used for:
- Font rendering
- Shadow effects
- Collision detection
๐ฏ Key Takeaways
- Euclidean distance measures straight-line distance
- NumPy enables efficient computation
- fromfunction maps math directly to grids
- Used in science, graphics, and AI
๐ Conclusion
This example demonstrates how mathematical concepts can be translated directly into code using NumPy. Understanding this bridge between math and programming is essential for fields like data science, physics, and computer vision.
Once you grasp this, you can extend it to more complex spatial problems and multidimensional computations.
No comments:
Post a Comment