very basically like this
let dt = 0.01; // stepsize let t0 = random(0, TWO_PI); // random starting angle let r; // star-positions in polar coordinates let t; // as in: r * exp(t) let x0 = width/2; // center of the galaxy let y0 = height/2; let scale = 40; // bigger numbers draw a bigger galaxy let numberOfStars = 1000; // stars in one arm of the galaxy // draw one spiral arm for (let i = 0; i < numberOfStars; i++) { // calculate polar coordinates of the next star to draw t = t0 + i * dt; let r = (t - t0) * scale; let x = x0 + r * cos(t); // convert to cartesian coordinates let y = y0 + r * sin(t); let starRadius = 10; DrawCircleAt(x, y, starRadius); }And then I have a for loop or whatever around this codeblock to draw as many spiral arms as I want.
I also add some random noise to pretty much every calculation, just to make things seem a little more organic.
You can also squish the galaxy by deciding on some squishX and squishY factors before drawing any arms and then in your cartesian coordinate conversion you have
let x = x0 + (r/xSquish) * cos(t); let y = y0 + (r/ySquish) * sin(t);To make it more colorful, I pick a starting color and every time after I draw a star, I slightly change the color - I do the same after every spiral arm.
Hope that helps and thank you for checking it out and commenting:)