Mathematics of 2D Shapes: Line and Rectangle SDFs Unveiled

Exploring the Signed Distance Functions for Lines and Rectangles in 2D Geometry

In our previous exploration of Signed Distance Functions (SDFs), we demystified their role in representing and manipulating geometric shapes. Now, let’s delve deeper into the mathematical intricacies of SDFs, focusing specifically on lines and rectangles in 2D geometry.

The Significance of Line SDFs

A Line Signed Distance Function (SDF) assigns a real number to each point in space, representing the distance from that point to the nearest point on a specified line. The sign of this distance indicates whether the point lies to the left or right of the line. Mathematically, the signed distance from a point $ P(x,y) $ to a line $L$ defined by two points $ (x_1, y_1) $ and $ (x_2, y_2) $ on it can be calculated in three steps:

  1. Vector Representation:

    • Define vector $ \mathbf{a} $ as the direction vector of the line: $$ \mathbf{a} = \begin{bmatrix} x_2 - x_1 \ y_2 - y_1 \end{bmatrix} $$
    • Normalize $ \mathbf{a} $ to obtain a unit vector $ \hat{\mathbf{a}} $: $$ \hat{\mathbf{a}} = \frac{\mathbf{a}}{|\mathbf{a}|} $$
  2. Position Vector:

    • Define vector $ \mathbf{b} $ as the position vector from any point on the line $L$ to point $ P $: $$ \mathbf{b} = \begin{bmatrix} x - x_1 \ y - y_1 \end{bmatrix} $$
  3. Dot Product:

    • Calculate the dot product of $ \mathbf{b} $ and a vector perpendicular to $ \mathbf{a} $: $$ d = \mathbf{b} \cdot \begin{bmatrix} a_y \ -a_x \end{bmatrix} $$

Understanding Line SDF Values

  • Positive SDF: Points to the right of the line (when facing along the line’s direction) will have a positive SDF value.
  • Negative SDF: Points to the left of the line will have a negative SDF value.
  • Zero SDF: Points lying on the line will have an SDF value of zero.

The visualization of line SDF is available at line_sdf.py using py5 module.

Unraveling Rectangle SDFs

Continuing our exploration of signed distance functions (SDFs), let’s delve into rectangles. A rectangle can be defined by its bottom-left corner coordinates $(x_1, y_1)$ and top-right corner coordinates $(x_2, y_2)$. The signed distance function $d_{\text{Rectangle}}$ for a point $P = (x, y)$ to this rectangle is expressed as:

$$ d_{\text{Rectangle}}(P) = \max\left(x_1 - x, x - x_2, y_1 - y, y - y_2, 0\right) $$

The function accounts for the distances to each side of the rectangle, considering negative distances for points inside the rectangle and positive distances for points outside. The overall signed distance is determined by taking the maximum among these distances.

Directionality Matters

For rectangles, the order of corner points matters, affecting the orientation of the SDF. Ensure consistency in the order to maintain a coherent representation.

The visualization of rectangle SDF is available at rectangle_sdf.py using py5 module.

Other 2D Shapes with Known SDFs

In addition to circles, lines and rectangles, various other 2D shapes have well-defined SDFs. While we won’t delve deep into them in this post, some noteworthy shapes include ellipses, triangles, and regular polygons.

By understanding the mathematics behind SDFs for lines and rectangles, we pave the way for more advanced explorations into 2D geometry and computational modeling. Stay tuned for future posts as we continue our journey through the captivating world of implicit geometry.

Feel free to experiment with the provided mathematical expressions and incorporate visual illustrations to enhance your understanding. Mathematics becomes a powerful tool when coupled with visualization.

Happy exploring!

This article is part of a series:

Seyed Sadjad Abedi-Shahri
Seyed Sadjad Abedi-Shahri
PhD in Biomechanics | Adjunct Lecturer

My research interests include Numerical Methods in Biomechanics, Scientific Computation, and Computational Geometry.