hilbert-curve/scripts/sketch.js
// Converts a linear coordinate into a 2-dimensional coordinate
Converts a linear coordinate into a 2-dimensional coordinate
Converts a linear coordinate into a 2 - dimensional coordinate
function hilbert(hindex, order) {
  const coordinates = [
    [0, 1],
    [0, 0],
    [1, 0],
    [1, 1],
  ];
  const posInOrderOne = coordinates[hindex & 3];
  let nextBits = hindex >>> 2;
  let x = posInOrderOne[0];
  let y = posInOrderOne[1];
  for (let o = 1; o < order; o++) {
    const length = Math.pow(2, o);
    const posInOrder = nextBits & 3;
    switch (posInOrder) {
      case 0: {
        const tmpX = length - 1 - x;
        x = length - 1 - y;
        y = tmpX + length;
        break;
      }
      case 1: 
        break;
      case 2: 
        x += length;
        break;
      case 3: {
        const tmpX = x;
        x = y + length;
        y = tmpX + length;
        break;
      }
    }
    nextBits = nextBits >>> 2;
  }
  const N = pow(2, order);
  const len = width / N;
  const v = createVector(x, y);
  v.mult(len);
  v.add(len / 2, len / 2);
  return v;
}
function hilbert ( hindex , order ) { const coordinates = [ [ 0 , 1 ] , [ 0 , 0 ] , [ 1 , 0 ] , [ 1 , 1 ] , ] ; const posInOrderOne = coordinates [ hindex & 3 ] ; let nextBits = hindex >>> 2 ; let x = posInOrderOne [ 0 ] ; let y = posInOrderOne [ 1 ] ; for ( let o = 1 ; o < order ; o ++ ) { const length = Math . pow ( 2 , o ) ; const posInOrder = nextBits & 3 ; switch ( posInOrder ) { case 0 : { const tmpX = length - 1 - x ; x = length - 1 - y ; y = tmpX + length ; break ; } case 1 : break ; case 2 : x += length ; break ; case 3 : { const tmpX = x ; x = y + length ; y = tmpX + length ; break ; } } nextBits = nextBits >>> 2 ; } const N = pow ( 2 , order ) ; const len = width / N ; const v = createVector ( x , y ) ; v . mult ( len ) ; v . add ( len / 2 , len / 2 ) ; return v ; }
Converts a linear coordinate into a 2-dimensional coordinate
Converts a linear coordinate into a 2 - dimensional coordinate
// Initially find where the node is in the first order curve // Shift the bits over to see where it's in the next order // Get the initial coordinates based on the position in the first order curve // Go through each higher order until they are all exhausted // The whole width of the particular order of the curve // The position in that order // TODO: change how the curves are rotated // Bottom left (shift vertically & rotate right) // Top left // Top right (translate horizontally) // Bottom right (rotate left, translate horizontally & vertically) // Look at the next position // Return the translated vertex