🐶
Machine Vision

Draw a Sparkling Crystal Ball: Two-Color Particle Tutorial

By Jan on 02/23/2025

Learn to draw a mesmerizing crystal ball with this step-by-step tutorial, using a two-color particle effect to create a magical and mystical feel.

Draw a Sparkling Crystal Ball: Two-Color Particle Tutorial

Table of Contents

Introduction

This snippet demonstrates how to generate and visualize points uniformly distributed on the surface of a sphere using R. We'll employ spherical coordinates for point generation and then convert them to Cartesian coordinates for visualization with the 'rgl' package.

Step-by-Step Guide

  1. Generate spherical coordinates:

    phi <- runif(100, 0, pi)
    theta <- runif(100, 0, 2*pi)
  2. Convert to Cartesian coordinates:

    x <- sin(phi) * cos(theta)
    y <- sin(phi) * sin(theta)
    z <- cos(phi)
  3. Plot the points on a sphere:

    library(rgl)
    spheres3d(x, y, z, radius = 0.05)

Code Example

The code generates 100 random points in spherical coordinates, converts them to Cartesian coordinates, and then plots these points as small blue spheres on a 3D sphere using the rgl package in R.

# Generate spherical coordinates
phi <- runif(100, 0, pi)
theta <- runif(100, 0, 2*pi)

# Convert to Cartesian coordinates
x <- sin(phi) * cos(theta)
y <- sin(phi) * sin(theta)
z <- cos(phi)

# Plot the points on a sphere
library(rgl)
spheres3d(x, y, z, radius = 0.05, color = "blue")

Explanation:

  1. Generating Spherical Coordinates:

    • phi: Represents the polar angle (from the positive z-axis) and ranges from 0 to pi.
    • theta: Represents the azimuthal angle (from the positive x-axis) and ranges from 0 to 2*pi.
    • runif(100, 0, pi) generates 100 random values between 0 and pi for phi.
    • runif(100, 0, 2*pi) generates 100 random values between 0 and 2*pi for theta.
  2. Converting to Cartesian Coordinates:

    • The formulas used are standard spherical-to-Cartesian conversion equations.
  3. Plotting the Points:

    • library(rgl) loads the rgl package for 3D plotting.
    • spheres3d(x, y, z, radius = 0.05, color = "blue") creates a 3D plot with small spheres (radius 0.05) at the calculated (x, y, z) coordinates, representing points on the sphere. The spheres are colored blue.

This code will generate a 3D plot of a sphere with 100 randomly distributed points on its surface. You can interact with the plot by rotating, zooming, and panning to view the points from different angles.

Additional Notes

  • Uniform Distribution: The runif() function ensures that the generated points are uniformly distributed on the sphere's surface. This is crucial for applications requiring random sampling on a sphere, such as simulations or Monte Carlo methods.

  • Alternative Visualization: Instead of spheres3d(), you can use plot3d(x, y, z, type = "p", size = 3, col = "blue") for a simpler point-based visualization.

  • Sphere Parameters: The code assumes a unit sphere (radius = 1). You can easily adjust the sphere's radius by multiplying the x, y, and z coordinates by the desired radius.

  • Color Customization: The color argument in spheres3d() can accept a vector of colors, allowing you to assign different colors to each point based on some criteria.

  • Applications: This code snippet has applications in various fields, including:

    • Computer Graphics: Generating random points on a sphere for particle systems or object placement.
    • Physics Simulations: Simulating the behavior of particles interacting on a spherical surface.
    • Geospatial Analysis: Representing points on the Earth's surface for geographic data visualization.
  • Further Exploration:

    • Explore other 3D plotting functions in the rgl package for more advanced visualizations.
    • Investigate methods for generating non-uniform distributions on a sphere, such as using different probability distributions or rejection sampling.

Summary

Step Description R Code
1 Generate 100 random spherical coordinates (φ, θ) uniformly distributed over a unit sphere. phi <- runif(100, 0, pi)
theta <- runif(100, 0, 2*pi)
2 Convert the spherical coordinates to Cartesian coordinates (x, y, z). x <- sin(phi) * cos(theta)
y <- sin(phi) * sin(theta)
z <- cos(phi)
3 Plot the points on a 3D sphere using the rgl package. Each point is represented as a small sphere with radius 0.05. library(rgl)
spheres3d(x, y, z, radius = 0.05)

Summary: This R code generates 100 random points uniformly distributed on the surface of a unit sphere. It first generates spherical coordinates, then converts them to Cartesian coordinates, and finally visualizes the points in a 3D plot.

Conclusion

This code provides a practical illustration of generating and visualizing uniformly distributed points on a sphere's surface using R. By leveraging spherical coordinates and conversion to Cartesian coordinates, the process is simplified, enabling clear visualization with the 'rgl' package. This technique holds value in diverse fields, including computer graphics, simulations, and geospatial analysis, demonstrating its versatility for tasks requiring random point distribution on a spherical surface.

References

Before we start, let me explain briefly how particle accelerators work and …

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait