Created
July 26, 2020 07:07
-
-
Save ClaireBookworm/ebf3379ab6fb6840d5e6dfd70b05d62b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setup() { | |
createCanvas(800, 800); | |
angleMode(DEGREES); | |
rectMode(CENTER); | |
const ctx = drawingContext; | |
const x = width / 2; | |
const y = height / 2; | |
const squareSideDotsCount = 30; | |
stroke(0); | |
const squareVertices = []; | |
let startAngle = 45; | |
for (let i = 0; i < 4; i += 1) { | |
squareVertices.push({ | |
x: 400 * cos(startAngle), | |
y: 400 * sin(startAngle), | |
}); | |
startAngle += 360 / 4; | |
} | |
const square = []; | |
for (let i = 0; i < 4; i += 1) { | |
for (let j = 0; j < squareSideDotsCount; j += 1) { | |
const x = lerp( | |
squareVertices[i].x, | |
squareVertices[(i + 1) % squareVertices.length].x, | |
j / squareSideDotsCount, | |
); | |
const y = lerp( | |
squareVertices[i].y, | |
squareVertices[(i + 1) % squareVertices.length].y, | |
j / squareSideDotsCount, | |
); | |
square.push({ x, y }); | |
} | |
} | |
push(); | |
translate(x, y); | |
for (let i = 0; i < square.length; i += 1) { | |
push(); | |
noStroke(); | |
if (i % 2 === 0) { | |
fill(0); | |
} else { | |
fill(255); | |
} | |
beginShape(); | |
vertex(square[i].x, square[i].y); | |
vertex(0, 0); | |
vertex( | |
square[(i + 1) % square.length].x, | |
square[(i + 1) % square.length].y, | |
); | |
endShape(CLOSE); | |
pop(); | |
} | |
const innerRectSide = 520; | |
const cellCount = 7; | |
const grid = []; | |
const pointCount = cellCount ** 2; | |
const cellSide = innerRectSide / cellCount; | |
const startPoint = -(cellSide * (cellCount - 1)) / 2; | |
for (let rowIndex = 0; rowIndex < cellCount; rowIndex += 1) { | |
for (let colIndex = 0; colIndex < cellCount; colIndex += 1) { | |
grid.push({ | |
x: startPoint + colIndex * cellSide, | |
y: startPoint + rowIndex * cellSide, | |
}); | |
} | |
} | |
for (let rowIndex = 0; rowIndex < cellCount; rowIndex += 1) { | |
for (let colIndex = 0; colIndex < cellCount; colIndex += 1) { | |
const x = grid[rowIndex * cellCount + colIndex].x; | |
const y = grid[rowIndex * cellCount + colIndex].y; | |
rect(x, y, cellSide, cellSide) | |
} | |
} | |
pop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment