Last active
December 14, 2024 19:08
-
-
Save aolo2/3ec1227a983fc3a2ea6643765878da4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
abstract class Particle { | |
public String name; | |
public List<float> position; | |
public List<float> color; | |
public abstract float positionMassFreqValue(int i); | |
public abstract void update(Particle other); | |
} | |
class HeavyParticle : Particle { | |
public float mass; | |
public override float positionMassFreqValue(int i) { | |
return position[i] * mass; | |
} | |
public override void update(Particle other) { | |
for (int i = 0; i < position.Count; ++i) { | |
position[i] = other.positionMassFreqValue(i) - positionMassFreqValue(i); | |
} | |
} | |
} | |
class LightParticle : Particle { | |
public float frequency; | |
public float brightness; | |
public override float positionMassFreqValue(int i) { | |
return position[i] * frequency * brightness; | |
} | |
public override void update(Particle other) { | |
for (int i = 0; i < position.Count; ++i) { | |
position[i] = other.positionMassFreqValue(i) + positionMassFreqValue(i); | |
} | |
} | |
} | |
class HelloWorld { | |
static void Main() { | |
List<Particle> particles = new List<Particle>(); | |
int particleCount = 1000; | |
int iterationCount = 1000; | |
// Init | |
for (int i = 0; i < particleCount; ++i) { | |
List<float> particlePosition = new List<float>(); | |
particlePosition.Add(i * 1.0f); | |
particlePosition.Add(i + 1.0f); | |
List<float> particleColor = new List<float>(); | |
particleColor.Add(1.0f); | |
particleColor.Add(1.0f); | |
particleColor.Add(1.0f); | |
String particleName = "Particle" + i; | |
if (i % 2 == 0) { | |
particles.Add(new HeavyParticle() { | |
position = particlePosition, | |
color = particleColor, | |
name = particleName, | |
mass = i * 100.0f, | |
}); | |
} else { | |
particles.Add(new LightParticle() { | |
position = particlePosition, | |
color = particleColor, | |
name = particleName, | |
frequency = i / 100.0f, | |
brightness = i * 12.0f, | |
}); | |
} | |
} | |
for (int i = 0; i < iterationCount; ++i) { | |
// Main each-to-each simulation loop | |
for (int a = 0; a < particles.Count; ++a) { | |
for (int b = a + 1; b < particles.Count; ++b) { | |
particles[a].update(particles[b]); | |
} | |
} | |
// Bounding conditions so that we don't NaN :P | |
for (int a = 0; a < particles.Count; ++a) { | |
if (particles[a].position[0] > 10000) { | |
particles[a].position[0] = 10000; | |
} | |
if (particles[a].position[0] < -10000) { | |
particles[a].position[0] = -10000; | |
} | |
if (particles[a].position[1] > 10000) { | |
particles[a].position[1] = 10000; | |
} | |
if (particles[a].position[1] < -10000) { | |
particles[a].position[1] = -10000; | |
} | |
} | |
} | |
// Some very basic "result" output | |
float avg_x = 0.0f; | |
float avg_y = 0.0f; | |
for (int i = 0; i < particles.Count; ++i) { | |
avg_x += particles[i].position[0]; | |
avg_y += particles[i].position[1]; | |
} | |
System.Console.WriteLine(avg_x + ", " + avg_y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment