Journey through the stars

Hello folks!

Best enjoyed in the darkness of space.

Code
/*
 * Eye Candy
 * 
 * Author: glv
 * Date: 2026-04-26
 * 
 * Description:
 * It started out as a starfield.
 * It is now a network of threads,
 * connecting and binding them together
 * in an inexorable evolution,
 * leaving a path to follow between the stars.
 * Watch it grow.
 * Choose the right path.
 */

PVector[] starfield;

void settings()
  {
  size(500, 500, JAVA2D); // JAVA2D better performance than P2D
  }

void setup()
  {  
  background(255);
  surface.setLocation(10, 10);
  starfield = new PVector[1000];
  makeStarfield();  
  sortStarfield();
  colorMode(HSB, 360, 100, 100, 100);
  strokeWeight(3);
  }
  
void draw()
  {
  background(0);
  updateStarfield();
  sortStarfield();  
  float phase = (25 + frameCount*0.5 + 360)%360;
  
  for (int i = 0; i < starfield.length; i++)
    {      
    float xi = starfield[i].x;
    float yi = starfield[i].y;  
      
    for (int j = i + 1; j < starfield.length; j++)
      {   
      float xj = starfield[j].x;
      float yj = starfield[j].y;
              
      float dx = xj - xi;

      if (dx > 19)
        break;

      float dy = yj - yi;
      if (dy > 19 || dy < -19)
        continue;
      
      float dd = dx*dx + dy*dy;

      if (dd < 360)
        {
        // Complicated song and dance:                  
        float cl = 120 - dd/3;
        float hue = (phase-cl+360)%360;
        float alpha = cl*5.0/6;
        
        stroke(hue, 100, 100, alpha);
        line(xi, yi, xj, yj);
        } 
      } 
    }
  }

// Bubble sort x
void sortStarfield() 
  {
  PVector t = new PVector(0, 0);
  boolean done = false;
  
  while(!done) 
    {
    done = true; // Assume sorted unless a swap occurs
    
    for (int i = 0; i < starfield.length - 1; i++) 
      {
      if (starfield[i].x > starfield[i + 1].x) 
        {
        // Swap elements if they're out of order
        t = starfield[i];
        starfield[i] = starfield[i + 1];
        starfield[i + 1] = t;
        
        done = false; // Mark that a swap occurred, so continue sorting
        }
      }
    }
  }
  
void updateStarfield()
  {
  for (int i=0; i<starfield.length; i++)
    {
    starfield[i].x += random(-0.5, 0.5);
    starfield[i].y += random(-0.5, 0.5);
    }
  }

void makeStarfield()
  {
  for (int i=0; i<starfield.length; i++)
    {   
    float a  = random(TWO_PI);
    float r = sqrt(random(1)) * 200;
    float x  = 250 + r*cos(a);
    float y  = 250 + r*sin(a); 
    starfield[i] = new PVector(x, y);
    }  
  }

Suggested listening while running sketch:

David Bowie - Space Oddity (Official Video)

https://www.youtube.com/watch?v=iYYRH4apXDo

:)

2 Likes