0

I'm trying to convert this processing code into p5 so I can adapt it for my first website.

I've looked around and found that

  • all variable type declarations are replaced with "var"
  • "void"s are replaced with "function"s
  • size() turns into createCanvas()
  • List item

Here's my processing code. (Sorry for length)

//drawing hexagons

Grid grid;
int numColumns;
int numRows;
float radius;
int diameter;

color[] pallet = {#2e2e2e, #52658a, #212c41, #8ba3d4, #a7c0f1};

void setup(){
  size(1440, 900);
  smooth();

  radius = 15;
  diameter = int(radius) * 2;

  //setting length and width for 2-d array of hexagons
  numColumns = int(width / (sqrt(3) * radius)) + 1;
  numRows = int(height / (.75 * diameter)) + 1;

  //create grid of hexagons object
  grid = new Grid(numColumns, numRows, radius);

  //randomly set initial color of hexagons
  for(int i = 0; i < numColumns; i++){
    for(int j = 0; j < numRows; j++){
      Hexagon selected = grid.getHexagon(i, j);
      selected.setFillColor(pallet[int(random(5))]);
    }
  }

  grid.draw();

  frameRate(60);
}

void draw(){
  grid.draw();


  //randomly pick 15 hexagons from grid and randomly assign them a new color
  for(int i = 0; i < 15; i++){
    Hexagon selected = grid.getHexagon(int(random(numColumns)), int(random(numRows)));
    selected.setFillColor(pallet[int(random(5))]);
  }
}

class Hexagon{
  color c;

  float r;
  float x;
  float y;

  float angle = TWO_PI / 6;
  float startAngle = PI / 6;

  Hexagon(float[] center, float radius){
    r = radius;
    x = center[0];
    y = center[1];
  }

  void draw(){
    //noStroke();
    fill(c);

    //arrays of coordinates for beginShape()
    float[] vertY = new float[6];
    float[] vertX = new float[6];

    //spinning the circle and calculating x and y position for vertices
    //removing start angle makes the hexagons flat-topped but they won't be spaced on grid properly
    for(int i = 0; i < 6; i++){
      float angleRad = (angle * i) + startAngle;
      vertX[i] = x + (r * cos(angleRad));
      vertY[i] = y + (r * sin(angleRad));
    }

    beginShape();
    vertex(vertX[0], vertY[0]);
    vertex(vertX[1], vertY[1]);
    vertex(vertX[2], vertY[2]);
    vertex(vertX[3], vertY[3]);
    vertex(vertX[4], vertY[4]);
    vertex(vertX[5], vertY[5]);
    endShape(CLOSE);
  }

  void setFillColor(color setColor){
    c = setColor;
  }
}

class Grid{

  int columns;
  int rows;
  float r;
  Hexagon[][] hexagons;

  Grid(int numColumns, int numRows, float radius){

    columns = numColumns;
    rows = numRows;
    r = radius;

    hexagons = new Hexagon[columns][rows];

    //starts the first hexagon at the corner of the window
    float[] center = {0, 0};

    for(int i = 0; i < rows; i++){
      for(int j = 0; j < columns; j++){
        //create a new hexagon with center center
        hexagons[j][i] = new Hexagon(center, r);
        //add the width of the hexagon to the x-coordinate of center
        center[0] += (r * sqrt(3));
      }
      //add the height spacing to the y-coordinate of center
      center[1] += (r * 3) / 2;

      //if the row number is divisible by two, bump the first center in the row
      if(i % 2 == 0){
        center[0] = (sqrt(3) / 2) * r;
      }else{
        center[0] = 0;
      }
    }
  }

  void draw(){
    for(int i = 0; i < rows; i++){
      for(int j = 0; j < columns; j++){
        hexagons[j][i].draw();
      }
    }
  }

  //for setting the color and possibly other things
  Hexagon getHexagon(int column, int row){
    return hexagons[column][row];
  }
}

And here is what I've got so far for the JS conversion:

//drawing hexagons

var grid;
var numColumns;
var numRows;
var radius;
var diameter;

var pallet = {123, 244, 321, 342, 783};

function setup(){
  var createCanvas(1440, 900);

  canvas.parent("sketch-holder");

  canvas.id("background-canvas");

  smooth();

  radius = 15;
  diameter = radius * 2;

  numColumns = width / (sqrt(3) * radius) + 1;
  numRows = height / (.75 * diameter) + 1;

  grid = new Grid(numColumns, numRows, radius);

  for(var i = 0; i < numColumns; i++){
    for(var j = 0; j < numRows; j++){
      var/*Hexagon*/ selected = grid.getHexagon(i, j);
      selected.setFillColor(pallet[random(5)]);
    }
  }

  grid.draw();

  frameRate(60);
}

function draw(){
  grid.draw();

  for(var i = 0; i < 15; i++){
    var/*Hexagon*/ selected = grid.getHexagon(random(numColumns), random(numRows));
    selected.setFillColor(pallet[random(5)]);
  }
}

class Hexagon{
  var c;

  var r;
  var x;
  var y;

  var angle = TWO_PI / 6;
  var startAngle = PI / 6;

  constructor(center, radius){
    this.r = radius;
    this.x = center[0];
    this.y = center[1];
  }

  function draw(){
    //noStroke();
    fill(c);

    vertY = new Array(6);
    vertX = new Array(6);

    for(var i = 0; i < 6; i++){
      angleRad = (angle * i) + startAngle;
      vertX[i] = x + (r * cos(angleRad));
      vertY[i] = y + (r * sin(angleRad));
    }
    beginShape();
    vertex(vertX[0], vertY[0]);
    vertex(vertX[1], vertY[1]);
    vertex(vertX[2], vertY[2]);
    vertex(vertX[3], vertY[3]);
    vertex(vertX[4], vertY[4]);
    vertex(vertX[5], vertY[5]);
    endShape(CLOSE);
  }

  function setFillColor(color setColor){
  c = setColor;
  }
}

class Grid{

  var columns;
  var rows;
  var r;
  var hexagons = new Array[][];

  constructor(numColumns, numRows, radius){

    this.columns = numColumns;
    this.rows = numRows;
    this.r = radius;

     var hexagons = new Array[columns][rows];

     var center = {0, 0};

    for(var i = 0; i < rows; i++){
      for(var j = 0; j < columns; j++){
        hexagons[j][i] = new Hexagon(center, r);
        center[0] += (r * sqrt(3));
      }
      center[1] += (r * 3) / 2;
      if(i % 2 == 0){
        center[0] = (sqrt(3) / 2) * r;
      }else{
        center[0] = 0;
      }
    }
  }

  function draw(){
    for(var i = 0; i < rows; i++){
      for(var j = 0; j < columns; j++){
        hexagons[j][i].draw();
      }
    }
  }

  function getHexagon(column, row){
    return hexagons[column][row];
  }
}

Any help would be appreciated.

3
  • Welcome to SO. Please visit the help center to see what and how to ask. SO is alas not a translation service. Show effort in a minimal reproducible example - use the <> snippet editor and ask specific questions to not have an off topic question - alternatively view the help at codereview and ask an on topic question there Commented Mar 19, 2017 at 7:01
  • O.k. then. I think the problem might be in my arrays. Can you do new Array[][]? Otherwise, what is the best way to create a 2-D array in JS without iterating through the first array and populating it with other arrays? Commented Mar 19, 2017 at 7:08
  • Map or filter comes to mind. No you cannot do new Array[][] as the console will tell you. The syntax for new Array is using () and not [], but the shorter method is simply var arr = []; so var arr = [[]] does work. Commented Mar 19, 2017 at 7:12

2 Answers 2

1

I managed to convert the Processing into Javascript. The biggest problem was converting the classes, classes are totally different in Javascript.

Also, the constants TWO_PI and PI don't exist.

//drawing hexagons in Javascript

var grid;
var numColumns;
var numRows;
var radius;
var diameter;

var canvasHeight;

//var pallet = ['#452123', '#567244', '#321321', '#987342', '#232783'];

function setup(){
    canvasHeight = document.body.clientHeight - height;

    var canvas = createCanvas(window.innerWidth, canvasHeight); //size(1440, 900);

    canvas.parent("sketch-holder");

    canvas.id("background-canvas");

    smooth();

    radius = 15;
    diameter = radius * 2;

    //setting length and width of 2-d array of hexagons
    numColumns = int(width / (sqrt(3) * radius)) + 1;
    numRows = int(height / (.75 * diameter)) + 1;

    //create grid of hexagons object
    grid = new Grid(numColumns, numRows, radius);

    /*
    //randomly set initial color of the hexagons
    for(var i = 0; i < numColumns; i++){
        for(var j = 0; j < numRows; j++){
            var selected = grid.getHexagon(i, j);
            selected.setFillColor(pallet[random(5)]);
        }
    }
    */

    grid.display();

    frameRate(1);
}

function draw(){
    grid.display();

    //randomly pick 15 hexagons from the grid and randomly assign them a new color
    /*
    for(var i = 0; i < 15; i++){
        var selected = grid.getHexagon(random(numColumns), random(numRows));
        selected.setFillColor(pallet[random(5)]);
    }
    */
}

function Hexagon(center, radius){
    //this.c;

    this.r = radius;
    this.x = center[0];
    this.y = center[1];

    this.angle = 6.28 / 6;
    this.startAngle = 3.14 / 6;

    console.log(this.angle);

    /*
    constructor(center, radius){
        this.r = radius;
        this.x = center[0];
        this.y = center[1];
    }
    */

    this.display = function(){
        //noStroke();
        //fill(this.c);

        //arrays of coordinates for beginShape()
        this.vertY = new Array(6); //float[] vertY = new float[6];
        this.vertX = new Array(6); //float[] vertX = new float[6];

        //spinning the circle and calculating the x and y coordinates for the vertices
        for(var i = 0; i < 6; i++){
            angleRad = (this.angle * i) + this.startAngle;
            this.vertX[i] = this.x + (this.r * cos(angleRad));
            this.vertY[i] = this.y + (this.r * sin(angleRad));
        }
        beginShape();
        console.log("asperw.me");
        vertex(this.vertX[0], this.vertY[0]);
        vertex(this.vertX[1], this.vertY[1]);
        vertex(this.vertX[2], this.vertY[2]);
        vertex(this.vertX[3], this.vertY[3]);
        vertex(this.vertX[4], this.vertY[4]);
        vertex(this.vertX[5], this.vertY[5]);
        endShape(CLOSE);
    }

    /*
    this.setFillColor = function(setColor){
    c = setColor;
    }
    */
}

function Grid(numColumns, numRows, radius){

    this.columns = numColumns;
    this.rows = numRows;
    this.r = radius;
    this.hexagons = []; //Hexagon[][] hexagons;

    for(var i = 0; i < numColumns; i++){
        this.hexagons[i] = new Array(numRows);
    }

    /*
    function Grid(numColumns, numRows, radius){

        this.columns = numColumns;
        this.rows = numRows;
        this.r = radius;
        console.log(numColumns);
    }
    */

    //starts the first hexagon at the corner of the canvas
    this.center = [0, 0];

    for(var i = 0; i < this.rows; i++){
        for(var j = 0; j < this.columns; j++){
            //create a new hexagon with center center
            this.hexagons[j][i] = new Hexagon(this.center, this.r);
            //add the width of the hexagon to the x-coordinate of the center
            this.center[0] += (this.r * sqrt(3));
        }
        //add the height spacing to the y-coordinate of the center
        this.center[1] += (this.r * 3) / 2;

        //if the row number is divisible by two, bump the first center in the row
        if(i % 2 == 0){
            this.center[0] = (sqrt(3) / 2) * this.r;
        }else{
            this.center[0] = 0;
        }
    }

    this.display = function(){
        for(var i = 0; i < this.rows; i++){
            for(var j = 0; j < this.columns; j++){
                this.hexagons[j][i].display();
            }
        }
    }

    /*
    //for getting a hexagon from the grid
    function/*Hexagon getHexagon(/*intcolumn, /*introw){
        return (hexagons[column][row]);
    }
    */
}
Sign up to request clarification or add additional context in comments.

Comments

0

Honestly, I think you're approaching this from the wrong angle. You don't translate code from one language to another by simple doing a replace-all of certain keywords.

Instead, you have to first figure out exactly what the code is doing. Write it out, in English, as a list of steps that the computer is following. When you have that written out, that's an algorithm you can start thinking about implementing in your target language.

You also need to get into the habit of working in smaller pieces. Instead of posting your entire project, post a specific line of code that's not working how you expect it to, and we'll go from there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.