Skip to content

Instantly share code, notes, and snippets.

@WilliamLivingood
Created September 24, 2025 05:58
Show Gist options
  • Select an option

  • Save WilliamLivingood/2048c9aff397fe047af4cb999dbb889b to your computer and use it in GitHub Desktop.

Select an option

Save WilliamLivingood/2048c9aff397fe047af4cb999dbb889b to your computer and use it in GitHub Desktop.
Code for Computer Generation of a Galaxy
import Turtle.*;
Turtle turtle;
void setup() {
size(900, 900);
noLoop();
colorMode(HSB, 360, 100, 100);
background(0);
turtle = new Turtle(this);
strokeCap(ROUND);
// starry backdrop
drawDotStars(350);
int totalPatterns = 28;
for (int i = 0; i < totalPatterns; i++) {
float px = random(40, width - 40);
float py = random(40, height - 40);
float[] angles = {87, 89.5, 100, 135, 137.5};
float turn = angles[int(random(angles.length))];
int segments = int(random(60, 160));
float step0 = random(1.5, 3.5);
float grow = random(0.5, 1.4);
float h = random(360);
float s = random(40, 90);
float b = random(80, 100);
stroke(color(h, s, b));
strokeWeight(random(0.6, 1.2));
drawGrowingPattern(px, py, segments, step0, grow, turn);
}
}
void drawGrowingPattern(float cx, float cy, int segments,
float step0, float grow, float turnDeg) {
turtle.penUp();
turtle.goToPoint(cx, cy);
turtle.setHeading(random(360));
turtle.penDown();
float step = step0;
for (int k = 0; k < segments; k++) {
turtle.forward(step);
turtle.right(turnDeg);
step += grow;
}
turtle.penUp();
}
void drawDotStars(int count) {
noStroke();
for (int i = 0; i < count; i++) {
float x = random(width);
float y = random(height);
float sz = random(1, 2.5);
float bri = random(70, 100);
fill(60, 0, bri);
circle(x, y, sz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment