Skip to content

Instantly share code, notes, and snippets.

@andrewstellman
Created March 13, 2025 17:49
Show Gist options
  • Save andrewstellman/288077b21848d3e0b22a5d51bf5b4345 to your computer and use it in GitHub Desktop.
Save andrewstellman/288077b21848d3e0b22a5d51bf5b4345 to your computer and use it in GitHub Desktop.
Breathing Particles: C# console visualization
List<Particle> particles = new List<Particle>();
int width = Math.Max(20, Console.WindowWidth - 1);
int height = Math.Max(10, Console.WindowHeight - 1);
Console.CursorVisible = false;
for (int i = 0; i < 40; i++)
particles.Add(new Particle(Random.Shared.Next(width), Random.Shared.Next(height)));
while (!Console.KeyAvailable)
{
Console.Clear();
foreach (var p in particles)
{
ApplyForces(p);
p.X = (p.X + p.VelX + width) % width;
p.Y = (p.Y + p.VelY + height) % height;
Console.SetCursorPosition((int)p.X, (int)p.Y);
Console.ForegroundColor = p.Color;
Console.Write(p.Symbol);
}
Thread.Sleep(15);
}
/// <summary>
/// Applies oscillating attraction/repulsion forces to create a pulsing effect
/// </summary>
/// <param name="p">The particle to apply forces to</param>
void ApplyForces(Particle p)
{
double centroidX = width / 2, centroidY = height / 2;
double dist = Math.Sqrt(Math.Pow(p.X - centroidX, 2) + Math.Pow(p.Y - centroidY, 2));
// Create oscillating force based on time
double time = Environment.TickCount / 1300.0;
double oscillation = Math.Sin(time * 0.56);
double factor = (oscillation * 0.07);
p.VelX += (centroidX - p.X) * factor;
p.VelY += (centroidY - p.Y) * factor;
p.VelX *= 0.97;
p.VelY *= 0.97;
}
/// <summary>
/// Represents a particle in the simulation with position, velocity, appearance, and color
/// </summary>
class Particle
{
public double X, Y, VelX, VelY;
public char Symbol;
public ConsoleColor Color;
/// <summary>
/// Creates a new particle at the specified position with random velocity and appearance
/// </summary>
/// <param name="x">Initial X coordinate</param>
/// <param name="y">Initial Y coordinate</param>
public Particle(double x, double y)
{
X = x; Y = y;
VelX = (Random.Shared.NextDouble() * 0.56 - 0.28);
VelY = (Random.Shared.NextDouble() * 0.56 - 0.28);
Symbol = "●★♦■◆▲"[Random.Shared.Next(6)];
Color = (ConsoleColor)Random.Shared.Next(9, 15);
}
}
@andrewstellman
Copy link
Author

andrewstellman commented Mar 13, 2025

Particles2.mov

Particle system that creates a hypnotic "breathing" effect as particles pulse toward and away from the center. This cross-platform console application uses 40 lines of C# and runs on Windows and macOS. The particles oscillate between attraction and repulsion states, creating patterns with minimal code.

Learn about:

  • Particle simulations
  • Sine wave oscillations
  • Emergent behavior
  • Console-based graphics

No external dependencies - just C# with top-level statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment