Polygons

Basil.js, form
Fabian Morón Zirfas

#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
// simple sketch showing the usage of some primitive forms

var w = 10; // we use this for easier calculation
var factor = 99;// the magic factor

var radius = 50;
function draw(){
  b.clear(b.doc()); // clear the current document
  b.units(b.MM); // we want to print. use MM intead of default pixels
  b.rectMode(b.CORNER); // draw rects from the center

  var doc = b.doc(); // a reference to the current document
  // set some preferneces of the document for better handling
  doc.documentPreferences.properties = {pageWidth:200,pageHeight:200};
  doc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
  b.rect(0,0,b.width,b.height);
  // now draw some shapes
  var xoff = 100; // use this to offset the polygons into the center of the page
  var yoff = 100; // use this to offset the polygons into the center of the page
  var step = 360/factor; // the steps on the circle
  var xrand1 = b.random(-20,20); // we want some randomness
  var yrand1 = b.random(-20,20); // we want some randomness
  var bufxrand = xrand1; // save the first random x value for the last polygon
  var bufyrand = yrand1; // save the first random y value for the last polygon
  // lets loop around a circle
  for(var angle = 0; angle < 360; angle+= step){
    var xrand2 = null; // this is the randomness for the second anchor
    var yrand2 = null; // this is the randomness for the second anchor
    // if we are on the last step we need to use the random value
    // from the first revolution or we wont have a closed form
    // se we create random values for all other occasions
    if(angle !== 360 - step){
      xrand2 = b.random(-20,20);
      yrand2 = b.random(-20,20);
    }else {
      // and use the buffered value here
      xrand2 = bufxrand;
      yrand2 = bufyrand;
    }
    // now we calculate the x and y values
    // by using a sinus/cosinus function
    var x1 = b.cos(b.radians(angle)) * radius + xoff;
    var y1 = b.sin(b.radians(angle)) * radius + yoff;
    // as you can see we offset one step for the second anchor
    var x2 = b.cos(b.radians(angle + step)) * radius + xoff;
    var y2 = b.sin(b.radians(angle + step)) * radius + yoff;
    // give it a random fill
    // b.fill(b.random(255),b.random(255),b.random(255)); colored
    b.fill(b.random(255)); // black to white
    // and draw it
    b.beginShape(b.CLOSE);
    b.vertex(x1 + xrand1 ,y1 + yrand1);
    b.vertex(x2 + xrand2 ,y2 + yrand2);
    b.vertex(xoff, yoff);
    b.endShape();
    // now overwrite the old random values for the next revolution
    xrand1 = xrand2;
    yrand1 = yrand2;
  }
 // --------
 // the next lines save the file and create an PNG
  var fname = File($.fileName).parent.fsName + '/' + ($.fileName.split('/')[$.fileName.split('/').length - 1]).split('.')[0] + '.indd';
  b.println(fname);
  doc.save(fname, false, 'basil', true);
  b.savePNG('out.png');
}

b.go();