Skip to content

Instantly share code, notes, and snippets.

@carmolim
Last active October 30, 2018 19:40
Show Gist options
  • Save carmolim/a92eaecf0e698890713038704c912ebd to your computer and use it in GitHub Desktop.
Save carmolim/a92eaecf0e698890713038704c912ebd to your computer and use it in GitHub Desktop.
011
let startX;
let startY;
let possibleDirections = [];
const mod = 20;
let points = [];
function setup() {
createCanvas(600, 600);
background(240);
// calculates de starting point
startX = mod * floor(random(1, width / mod));
startY = mod * floor(random(1, height / mod));
points.push(new spoint(startX, startY));
frameRate(12);
background(240);
// draw grid points
grid();
}
function draw() {
background(240);
grid();
for (let i = 0; i < points.length; i++) {
stroke(0);
noFill();
ellipse(points[i].x, points[i].y,10);
}
noFill();
beginShape();
for (let i = 0; i < points.length; i++) {
vertex(points[i].x, points[i].y);
}
endShape();
nextPoint();
}
function spoint(x, y) {
this.x = x;
this.y = y;
}
function grid() {
strokeWeight(1);
stroke(100);
for (let x = mod; x < width; x += mod) {
for (let y = mod; y < height; y += mod) {
point(x, y);
}
}
}
function keyTyped() {
if (key === 's') {
saveCanvas("mssm_01_", 'png');
}
}
function nextPoint() {
checkPossibleDirections();
if (possibleDirections.length > 0) {
// random choose a direction
let direction = possibleDirections[floor(random(0, possibleDirections.length))];
let newX = 0;
let newY = 0;
// based on the direction choosed set the dest vector
switch (direction) {
// N
case 0:
newX = points[points.length - 1].x;
newY = points[points.length - 1].y - mod;
break;
// NE
case 1:
newX = points[points.length - 1].x + mod;
newY = points[points.length - 1].y - mod;
break;
// E
case 2:
newX = points[points.length - 1].x + mod;
newY = points[points.length - 1].y;
break;
// SE
case 3:
newX = points[points.length - 1].x + mod;
newY = points[points.length - 1].y + mod;
break;
// S
case 4:
newX = points[points.length - 1].x;
newY = points[points.length - 1].y + mod;
break;
// SW
case 5:
newX = points[points.length - 1].x - mod;
newY = points[points.length - 1].y + mod;
break;
// W
case 6:
newX = points[points.length - 1].x - mod;
newY = points[points.length - 1].y;
break;
// NW
case 7:
newX = points[points.length - 1].x - mod;
newY = points[points.length - 1].y - mod;
break;
}
points.push(new spoint(newX, newY));
possibleDirections = [];
checkPossibleDirections();
} else {
console.log('ended');
points = [];
startX = mod * floor(random(1, width / mod));
startY = mod * floor(random(1, height / mod));
points.push(new spoint(startX, startY));
}
}
function checkPossibleDirections() {
possibleDirections = [];
// N
if (points[points.length - 1].y - mod >= mod && checkExistingPoint(points[points.length - 1].x, points[points.length - 1].y - mod)) {
possibleDirections.push(0);
}
// NE
if (points[points.length - 1].x + mod <= width - mod && points[points.length - 1].y - mod >= mod && checkExistingPoint(points[points.length - 1].x + mod, points[points.length - 1].y - mod)) {
possibleDirections.push(1);
}
// E
if (points[points.length - 1].x + mod <= width - mod && checkExistingPoint(points[points.length - 1].x + mod, points[points.length - 1].y)) {
possibleDirections.push(2);
}
// SE
if (points[points.length - 1].x + mod <= width - mod && points[points.length - 1].y + mod <= height - mod && checkExistingPoint(points[points.length - 1].x + mod, points[points.length - 1].y + mod)) {
possibleDirections.push(3);
}
// S
if (points[points.length - 1].y + mod <= height - mod && checkExistingPoint(points[points.length - 1].x, points[points.length - 1].y + mod)) {
possibleDirections.push(4);
}
// SW
if (points[points.length - 1].x - mod >= mod && points[points.length - 1].y + mod <= height - mod && checkExistingPoint(points[points.length - 1].x - mod, points[points.length - 1].y + mod)) {
possibleDirections.push(5);
}
// W
if (points[points.length - 1].x - mod >= mod && checkExistingPoint(points[points.length - 1].x - mod, points[points.length - 1].y)) {
possibleDirections.push(6);
}
// NW
if (points[points.length - 1].x - mod >= mod && points[points.length - 1].y - mod >= mod && checkExistingPoint(points[points.length - 1].x - mod, points[points.length - 1].y - mod)) {
possibleDirections.push(7);
}
}
function checkExistingPoint(x, y) {
let pointToCheck = createVector(x, y);
for (let i = 0; i < points.length; i++) {
let createdPoint = createVector(points[i].x, points[i].y);
if (pointToCheck.equals(createdPoint)) {
return false;
}
}
return true;
}
function gradientCircle(x, y, r, c, a){
noStroke();
c.setAlpha(a);
fill(c);
for(let i = 0; i < r; i++){
ellipse(x, y, i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment