create a shape with an array of vertices

hello, i am trying to create a shape with an array of pre-made vertices. here is the code:

function setup() {
  createCanvas(600, 600);
  background(150);    
}

function createVertices() {
  let vertices = [];
  let angle = TWO_PI / 6;
  for (let a = 0; a < TWO_PI; a += angle) {
    var sx = 50 + cos(a) * 20;
    var sy = 50 + sin(a) * 20;
    vertices.push(vertex(sx, sy));
  }
  return vertices;
}

function draw() {
  var verticesArray = createVertices();
  polygon(50, 50, 20, verticesArray);
}

function polygon(x, y, radius, ver) {
  beginShape();
  let vertices = [];
  for (var i = 0; i < ver.length - 1; i++) {
    vertices.push(vertex(ver[i], ver[i + 1]));
  }
  endShape(CLOSE);
}

function polygon() doesn't draw a hexagon in this way, but only if i copy the code in createVertices() function. what is the problem?

Answers

Sign In or Register to comment.