Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewstellman/0879e8a58a2dc2dbe712b8b8c46100d1 to your computer and use it in GitHub Desktop.
Save andrewstellman/0879e8a58a2dc2dbe712b8b8c46100d1 to your computer and use it in GitHub Desktop.
Particle simulation oscillating pattern in a C# console app
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++) // More particles for better visibility
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(70); // Moderate frame rate with smoother movement
}
/// <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 / 1000.0;
double oscillation = Math.Sin(time * 0.8); // Oscillation over time
double factor = (oscillation * 0.1); // Force factor oscillates between attraction and repulsion
p.VelX += (centroidX - p.X) * factor;
p.VelY += (centroidY - p.Y) * factor;
p.VelX *= 0.97; // Moderate damping
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;
// Lower initial velocity for smoother movement
VelX = (Random.Shared.NextDouble() * 0.8 - 0.4);
VelY = (Random.Shared.NextDouble() * 0.8 - 0.4);
Symbol = "●★♦■◆▲"[Random.Shared.Next(6)]; // Larger, more visible symbols
Color = (ConsoleColor)Random.Shared.Next(9, 15); // Bright colors only
}
}
@andrewstellman
Copy link
Author

particles.mov

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