Chaos game hexagon

by Jonathan Lam

HTML

<canvas width="1920" height="1920" id="canvas"></canvas>

JavaScript

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;

var iterations = 1e6;

var pointer = {
	x: width/2,
  y: height/2
};
var corners = [
	{x: 375/500*width, y: 33/500*height},
  {x: 125/500*width, y: 33/500*height},
  {x: 0, y: height/2},
  {x: 125/500*width, y: 467/500*height},
  {x: 375/500*width, y: 467/500*height},
  {x: width, y: height/2}
];
function mod(n, m) {
  return ((n % m) + m) % m;
}
var restriction = function() {
	//console.log(cornerId, lastCornerId, cornerId %2, lastCornerId %2, cornerId%2 !== lastCornerId%2)
	return lastCornerId === null || 
  //true;
  //cornerId !== lastCornerId;
  //mod(cornerId+1, 6) !== lastCornerId;
  //(mod(lastCornerId+2, 6) !== cornerId);
  //(mod(lastCornerId+3, 3) !== cornerId);
  /*(secondLastCornerId === null || 
  	(
    	((lastCornerId)%6 !== cornerId) &&
      ((secondLastCornerId)%6 !== cornerId)
    )
  );*/
  (secondLastCornerId === null || thirdLastCornerId === null || 
  	(
    	((lastCornerId+3)%6 !== cornerId) &&
      ((secondLastCornerId+2)%6 !== cornerId) &&
      ((thirdLastCornerId+2)%6 !== cornerId)
    )
  );
  //cornerId % 2 === lastCornerId % 2;
};
var cornerId = null;
var lastCornerId = null;
var secondLastCornerId = null;
var thirdLastCornerId = null;
for(var i = 0; i < iterations; i++) {
  cornerId = Math.floor(Math.random()*corners.length);
  if(restriction()) {
  	var corner = corners[cornerId];
    pointer.x = (pointer.x + corner.x)/2;
    pointer.y = (pointer.y + corner.y)/2;
    ctx.fillRect(pointer.x, pointer.y, 1, 1);
    thirdLastCornerId = secondLastCornerId;
    secondLastCornerId = lastCornerId;
    lastCornerId = cornerId;
  } else {
  	i--;
  }
}