Night sky with starlink

produced for the art+code class

Log in to post a comment.

const nElems = 20; // min=10 , max=50, step=1,
const noise = 0; // min=0 , max=30, step=0.1,
const radius = 3; // min=0.1 , max=10, step=0.1,
const satelliteLineLength = 5; // min=1 , max=20, step=0.5,
const satelliteStarChance = 0.5; // min=0 , max=1, step=0.01,

Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(-50, -20);
turtle.pendown();

// x and y are the center of the star
function drawStar(x, y, r, points, rotation) {
    const angle = Math.PI / points; 
    const vertices = [];

    //Vertices calc
    for (let i = 0; i < 2 * points; i++) {
        const length = i % 2 === 0 ? r : r / 2; // Alternate between outer and inner points
        const theta = i * angle + (rotation * Math.PI / 180);
        const vx = x + length * Math.cos(theta);
        const vy = y + length * Math.sin(theta);
        vertices.push([vx, vy]);
    }

    turtle.jump(vertices[0][0], vertices[0][1]);
    turtle.pendown();
    for (let i = 1; i < vertices.length; i++) {
        turtle.goto(vertices[i][0], vertices[i][1]);
    }
    turtle.goto(vertices[0][0], vertices[0][1]); // Close the star
    turtle.penup();
}

function drawSatelliteStar(x, y, lineLength, points) {
    const angle = (2 * Math.PI) / points; // Angle between each line

    for (let i = 0; i < points; i++) {
        const theta = i * angle;

        const randomLength = lineLength + Math.random() * (lineLength * 0.3);

        const endX = x + randomLength * Math.cos(theta);
        const endY = y + randomLength * Math.sin(theta);

        turtle.jump(x, y);
        turtle.pendown();
        turtle.goto(endX, endY);
        turtle.penup();
    }
}

// The walk function will be called until it returns false.
function walk(i) {
    let d = 180 / nElems;

    // Add noise-based randomness to the starting position
    let x = (i % nElems) * d - 83 + (Math.random() - 0.5) * noise;
    let y = Math.floor(i / nElems) * d - 87 + (Math.random() - 0.5) * noise;

    let rotation = Math.random() * 360;

    // Randomly decide whether to draw a regular star or a satellite star
    if (Math.random() < satelliteStarChance) {
        drawSatelliteStar(x, y, satelliteLineLength, 20); // Draw satellite star with 20 lines
    } else {
        drawStar(x, y, radius, 7, rotation); 
    }

    return i < nElems * nElems - 1;
}