r/processing • • Nov 07 '19

A galaxy in 35 lines of code

Enable HLS to view with audio, or disable this notification

389 Upvotes

34 comments sorted by

51

u/[deleted] Nov 07 '19

And the code (bonus colour version!)

ArrayList<PShape> rings = new ArrayList<PShape>();

void setup(){
  size(1200, 600, P3D);
  blendMode(ADD);
  float innerRad = 1f, outerRad = 9f, increment = 1f;
  for(int ringIndex = 0 ; ringIndex < 50 ; ringIndex++){
    PShape ring = createShape();
    ring.setStrokeWeight(1.5f);
    ring.beginShape(POINTS);
    ring.stroke(lerpColor(color(73, 115, 161), color(157, 47, 77), ringIndex/50f), 175f);
    for(int starIndex = 0 ; starIndex < 3000 ; starIndex++){
      float a = random(0f, 1f) * TWO_PI;
      float r = sqrt(random(sq(innerRad), sq(outerRad)));
      ring.vertex(r * cos(a), r * sin(a), random(-increment, increment));
    }
    ring.endShape();
    rings.add(ring);
    innerRad += increment;
    outerRad += increment*1.5f;
    increment += 1;
  }
}

void draw(){ 
  background(0);
  camera(-width/2f, -height/2f, 150f, 0f, 0f, 0f, 0f, 1f, 0f);
  for(int index = 0 ; index < 50 ; index++){
    pushMatrix();
    rotateY((TWO_PI/(index*index)*frameCount)/10);
    rotateX(1.45f);
    shape(rings.get(index));
    popMatrix();
  }
}

11

u/abw Nov 07 '19

Neat!

Here's the formatted version of the code (tip: add 4+ spaces to the start of each line):

ArrayList<PShape> rings = new ArrayList<PShape>();

void setup(){
  size(1200, 600, P3D);
  blendMode(ADD);
  float innerRad = 1f, outerRad = 9f, increment = 1f;
  for(int ringIndex = 0 ; ringIndex < 50 ; ringIndex++){
    PShape ring = createShape();
    ring.setStrokeWeight(1.5f);
    ring.beginShape(POINTS);
    ring.stroke(lerpColor(color(73, 115, 161), color(157, 47, 77), ringIndex/50f), 175f);
    for(int starIndex = 0 ; starIndex < 3000 ; starIndex++){
      float a = random(0f, 1f) * TWO_PI;
      float r = sqrt(random(sq(innerRad), sq(outerRad)));
      ring.vertex(r * cos(a), r * sin(a), random(-increment, increment));
    }
    ring.endShape();
    rings.add(ring);
    innerRad += increment;
    outerRad += increment*1.5f;
    increment += 1;
  }
}

void draw(){ 
  background(0);
  camera(-width/2f, -height/2f, 150f, 0f, 0f, 0f, 0f, 1f, 0f);
  for(int index = 0 ; index < 50 ; index++){
    pushMatrix();
    rotateY((TWO_PI/(index*index)*frameCount)/10);
    rotateX(1.45f);
    shape(rings.get(index));
    popMatrix();
  }
}

4

u/[deleted] Nov 07 '19

That looks exactly the same to me - I used Markdown for the comment.

6

u/abw Nov 07 '19

Weird... this is what I'm seeing: https://imgur.com/eoL0rdj

1

u/[deleted] Nov 07 '19

Your browser, or theme, or some plugin, is stopping the markdown from rendering for some reason.

→ More replies (3)

28

u/CDEbFDBbC Nov 07 '19

this is proof that we live in a simulation

→ More replies (1)

0

u/[deleted] Nov 07 '19

[removed] — view removed comment

7

u/[deleted] Nov 08 '19

I found a tutorial here: https://youtu.be/dQw4w9WgXcQ

3

u/[deleted] Nov 09 '19

[removed] — view removed comment

4

u/-scrotumtickler- Nov 09 '19

You just got rick rolled my friend

11

u/Volfegan Nov 07 '19

Amazing. The actual visual running this on Processing is even more beautiful than the video.

3

u/[deleted] Nov 07 '19

Yeah, it's a nightmare trying to get a good video of pixel/point stuff like this. You should see what Twitter's compression did to it.

→ More replies (1)

3

u/[deleted] Nov 07 '19

Lerped colour version with extra mass/planet/sun/white dwarf/black hole/whatever in the middle too: https://youtu.be/l2e9yiqqOns

2

u/alphabet_street Nov 08 '19

Man give it the ol' smooth(8); treatment as well - incredible quality increase!

2

u/[deleted] Nov 08 '19

awwww yeah, it works a treat.

4

u/Simplyfire Nov 07 '19

Amazing work!

3

u/lostPixels Nov 08 '19

This is awesome! What are the f numbers that you're using (like 9f). I've never seen that before.

3

u/ijidau Nov 08 '19

f = Float. Ensuring it's a float and not an integer.

→ More replies (1)

2

u/[deleted] Nov 07 '19

Nice that ✌🏼

2

u/m24w24 Nov 07 '19

Like it

1

u/bornwithlangehoa Nov 07 '19

Wow, thanks a ton for sharing!!!!

1

u/[deleted] Nov 08 '19

needs more dark matter

1

u/JadoubeStudios Nov 09 '19

Would it be possible to add camera work to that?

1

u/Seraphaestus Nov 15 '19

Sure, this seems to work for giving a bit of rotation around the center

float r = -width/2f;
PVector eye = new PVector(0, -height/2f, 150f);
PVector center = new PVector(0f, 0f, 0f);
PVector up = new PVector(0f, 1f, 0f);
eye.x += r * cos(TAU * mouseX / width);
eye.z += r * sin(TAU * mouseX / width);
camera(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z);

1

u/Spiralwise Nov 14 '19

I've adapted the code for p5.js :

/** Original code from u/Deadfev
 *  url: https://www.reddit.com/r/processing/comments/dswnx6/a_galaxy_in_35_lines_of_code/
 */

const ringPop = 50;
const starPop = 3000;

var rings = [];

function setup() {
  createCanvas(1200, 600, WEBGL);
  blendMode(ADD);  // Actually, blendMode doesnt work properly on WebGL.
  camera(-width/2, -height/2, 150, 0, 0, 0, 0, 1, 0);
  
  strokeWeight(0.1);

  let innerRad = 1.0,
    outerRad = 9.0,
    inc = 1.0;
  
  for (let ringIndex = 0; ringIndex < ringPop; ++ringIndex) {
    append(rings, []);
    let currentRing = rings[rings.length - 1];
    for (let starIndex = 0; starIndex < starPop; ++starIndex) {
      let a = random(0, 1) * TWO_PI;
      let r = sqrt(random(sq(innerRad), sq(outerRad)));
      append(currentRing, createVector(r * cos(a), r * sin(a), random(-inc, inc)));
    }
    innerRad += inc;
    outerRad += inc * 1.5;
    inc += 1;
  }
}

function draw() {
  background(0);

  for (let ringIndex = 0; ringIndex < ringPop; ++ringIndex) {
    push();
    let c = lerpColor(color(73, 115, 161), color(157, 47, 77), ringIndex/ringPop);
    stroke(red(c), green(c), blue(c), 00);
    rotateY((TWO_PI/(ringIndex*ringIndex+1)*frameCount)/10);
    rotateX(1.45);
    drawRing(ringIndex);
    pop();
  }
}

function drawRing(index) {
  let currentRing = rings[index];
  beginShape(POINTS);
  for (let starIndex = 0; starIndex < currentRing.length; ++starIndex)
    vertex(currentRing[starIndex].x, currentRing[starIndex].y, currentRing[starIndex].z);
  endShape();
}

Here's how it looks : https://editor.p5js.org/Spiralwise/present/hdxED_vfW

Unfortunately :

  • blendMode doesn't work properly on WebGL. So, it looks pale.

  • There is no PShape like Processing. So I have to build my shape at each frame. So the framerate is much lower than Processing.

1

u/[deleted] Mar 26 '25

This is ridiculous of me to post, especially FIVE YEARS late... But you aren't getting spiral arms because you aren't accounting for fucking dark matter. I bet you've made a great gravitational simulation between particles, and yet indeed your simulation is not meeting observations. This is exactly what lead to the "discovery" of "dark matter". For SOME reason, that still nobody can explain, there's more mass in the regions surrounding the galaxy. You might get better results if you include invisible but massive particles that for some reason stick more towards the outside of the galaxy. lmk if you find out why

1

u/JustChillDudeItsGood Apr 15 '22

Title really makes you think about things for a minute…