Skip to content

Commit

Permalink
random spirals
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaUtah committed Jan 19, 2025
1 parent 3b3d702 commit 7b635af
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
6 changes: 5 additions & 1 deletion capture/capture.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@

// Generate a random path
function generatePath() {
path = spiral(10, 10, canvas.width-10, canvas.height-10, 15, 10, 1, 'clockwise').map(([x, y]) => ({ x, y}))
const corner = Math.floor(Math.random() * 4)
const direction = Math.floor(Math.random() * 2)
const steps = Math.floor(Math.random() * 10)
path = spiral(10, 10, canvas.width-10, canvas.height-10, 10+steps, 5+steps, corner, direction)
.map(([x, y]) => ({ x, y}))
crosshairIndex = 0;
drawFrame();
}
Expand Down
77 changes: 64 additions & 13 deletions capture/spiral.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,96 @@

function spiral(xmin, ymin, xmax, ymax, xsteps, ysteps) {
function spiral(xmin, ymin, xmax, ymax, xsteps, ysteps, startCorner = 0, direction = 0) {
const pointsList = [];
let x = 0, y = 0;
let x, y;
let dir;
const numPoints = (xsteps + 1) * (ysteps + 1);
let dir = 'right';
let x0 = 0, y0 = 0, x1 = xsteps, y1 = ysteps;
startCorner = {
0: 'top-left',
1: 'top-right',
2: 'bottom-right',
3: 'bottom-left',
}[startCorner]

// Determine initial position and direction based on starting corner and direction
switch (startCorner) {
case 'top-left':
x = 0;
y = 0;
dir = direction === 0 ? 'right' : 'down';
break;
case 'top-right':
x = xsteps;
y = 0;
dir = direction === 0 ? 'down' : 'left';
break;
case 'bottom-right':
x = xsteps;
y = ysteps;
dir = direction === 0 ? 'left' : 'up';
break;
case 'bottom-left':
x = 0;
y = ysteps;
dir = direction === 0 ? 'up' : 'right';
break;
default:
throw new Error('Invalid startCorner. Must be "top-left", "top-right", "bottom-left", or "bottom-right".');
}

// Generate the spiral
while (pointsList.length < numPoints) {
pointsList.push([x, y]);
if (dir === 'right') {
x += 1;
if (x === x1) {
y0 += 1;
dir = 'down';
if (direction === 0) {
y0 += 1; // Update bounds clockwise
} else {
y1 -= 1; // Update bounds counterclockwise
}
dir = direction === 0 ? 'down' : 'up';
}
continue;
}
if (dir === 'down') {
y += 1;
if (y === y1) {
x1 -= 1;
dir = 'left';
if (direction === 0) {
x1 -= 1; // Update bounds clockwise
} else {
x0 += 1; // Update bounds counterclockwise
}
dir = direction === 0 ? 'left' : 'right';
}
continue;
}
if (dir === 'left') {
x -= 1;
if (x === x0) {
y1 -= 1;
dir = 'up';
if (direction === 0) {
y1 -= 1; // Update bounds clockwise
} else {
y0 += 1; // Update bounds counterclockwise
}
dir = direction === 0 ? 'up' : 'down';
}
continue;
}
if (dir === 'up') {
y -= 1;
if (y === y0) {
x0 += 1;
dir = 'right';
if (direction === 0) {
x0 += 1; // Update bounds clockwise
} else {
x1 -= 1; // Update bounds counterclockwise
}
dir = direction === 0 ? 'right' : 'left';
}
continue;
}
}

// Scale points to the given xmin, ymin, xmax, ymax
const dx = (xmax - xmin) / xsteps;
const dy = (ymax - ymin) / ysteps;
const points = pointsList.map(([px, py]) => [
Expand All @@ -52,4 +101,6 @@ function spiral(xmin, ymin, xmax, ymax, xsteps, ysteps) {
return points;
}

window.spiral = spiral
// Example usage:
window.spiral = spiral;

0 comments on commit 7b635af

Please sign in to comment.