Pentagram Snowflake

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

<!

DOCTYPE html>
<html>
<head>
<title>Pentagram snowflake</title>
<script>
function generateRandomNumbers() {
// Create a list of the numbers to choose from
var numbers = [1, 2, 3, 4, 5];

// Initialize variables
var previousVertex = null;
var previousMidpoint = null;
var results = [];

// Get the canvas and context elements


var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

// Clear the canvas


context.clearRect(0, 0, canvas.width, canvas.height);

// Counter to keep track of number of dots drawn


var dotCounter = 0;

// Repeat the process 1000000 times, with a delay of 1ms between each dot
var interval = setInterval(function() {
if (dotCounter >= 100000) {
clearInterval(interval);
// Display the results on the webpage
var resultsElement = document.getElementById('results');
resultsElement.textContent = results.join(', ');
} else {
// Remove the previous number from the list of choices
var choices = numbers.filter(function(n) { return n !==
previousVertex; });
// Choose a random number from the remaining choices
var vertexNumber = choices[Math.floor(Math.random() * choices.length)];
// Add the chosen number to the results
results.push(vertexNumber);

// Get the vertex corresponding to the chosen number


var vertex = getVertex(vertexNumber, canvas.width, canvas.height);

// Calculate the midpoint between the previous midpoint and the current
vertex
if (previousMidpoint) {
var midX = (previousMidpoint.x + vertex.x) / 2;
var midY = (previousMidpoint.y + vertex.y) / 2;

// Draw a dot at the midpoint


context.beginPath();
context.arc(midX, midY, 1, 0, Math.PI * 2);
context.fill();

// Set the midpoint as the previous midpoint for the next iteration
previousMidpoint = { x: midX, y: midY };
} else {
// If this is the first midpoint, calculate it as the midpoint between
the first vertex and the current vertex
var midX = (getVertex(1, canvas.width, canvas.height).x + vertex.x) /
2;
var midY = (getVertex(1, canvas.width, canvas.height).y + vertex.y) /
2;

// Draw a dot at the midpoint


context.beginPath();
context.arc(midX, midY, 1, 0, Math.PI * 2);
context.fill();

// Set the midpoint as the previous midpoint for the next iteration
previousMidpoint = { x: midX, y: midY };
}

// Set the chosen number as the previous number for the next iteration
previousVertex = vertexNumber;

// Increment the dot counter


dotCounter++;
}
}, 0.001);
}

function getVertex(number, width, height) {


// Get the angle for the vertex based on the number
var angle = (number - 1) * Math.PI * 2 / 5;

// Calculate the coordinates of the vertex


var radius = Math.min(width, height) * 0.4;
var x = width / 2 + radius * Math.sin(angle);
var y = height / 2 - radius * Math.cos(angle);
return { x: x, y: y };
}
</script>
</head>
<body>
<h1>Pentagram Snowflake</h1>
<p>Click the button to generate a "pentagram snowflake" .</p>
<button onclick="generateRandomNumbers()">Generate</button>
<canvas id="canvas" width="400" height="400"></canvas>
<p>Results: <span id="results"></span></p>
</body>
</html>

You might also like