Brock J. Stearn
Task 05
Programming for Artists

Winter Pattern

Winter Patterns - bjs_task5.js
/*
Programming for Artists - Assignment #5
Brock J. Stearn
"Winter Patterns"
*/

// set dimensions for new document
var w = 540; // units
var h = 530;

// instantiate raster canvas for use
var RC = new RasterCanvas(w, h, Units.PIXELS)
RC.setBackgroundColor(.2, .2, .4);

// Make new Layer to make pattern
RC.newArtLayer(LayerKind.NORMAL);
// initialize variables
var opacity = 1;
// define number of tiles in x and y
var tilesX = 4;
var tilesY = 3;
var thresholdX = w / tilesX;
var thresholdY = h / tilesY;
// use mod to make a tiled version of our color plot
for (var x = 0; x < w; x+=1.75) {
   for (var y = 0; y < h; y+=1.75) {
      var tileR = 1 - (x % thresholdX / thresholdX);
      var tileB = y % tileR;
      RC.setPenColor(0, 0, tileB);
      RC.setPixel(x,y, opacity);
   }
}


/* Continuous No Pixel Selected Error ... too tired to keep trying
   Now Commenting this part out
RC.newArtLayer(LayerKind.NORMAL);

var opacity = .5;
var tilesX = 4;
var tilesY = 3;
var thresholdX = w / tilesX;
var thresholdY = h / tilesY;
for (var x = 0; x < w; x+=2.2) {
   for (var y = 0; y < h; y+=2.2) {
      var tileR = 1 - (x % thresholdX / thresholdX);
      var tileB = y % tileR;
      RC.setPenColor(.8, .8, .9);
      RC.setPixel(x,y, opacity);
   }
}
*/

/* Continuous No Pixel Selected Error ... too tired to keep trying
   Now Commenting this part out
RC.newArtLayer(LayerKind.NORMAL);

var opacity = .5;
var tilesX = 4;
var tilesY = 2;
var thresholdX = w / tilesX;
var thresholdY = h / tilesY;
for (var x = 0; x < w; x+=1.95) {
   for (var y = 0; y < h; y+=1.95) {
      var tileR = 1 - (x % thresholdX / thresholdX);
      var tileB = y % tileR;
      RC.setPenColor(0, 0, tileB);
      RC.setPixel(x,y, opacity);
   }
}
*/


// Make snowflakes by using the polar function
// Make new layer
RC.newArtLayer(LayerKind.NORMAL);
for (var x = 20; x < w; x+=10) {
   for (var y = 0; y < h; y+=10) {
      var opacity = .5;

   // initialize variables
   var opacity = 1;
   // define limit x and y to help stay on canvas
   var limX = w-1;
   var limY = h-1;
   // define centerpoint of polar
   // set to x and y so that it tiles
   var centerX = x;
   var centerY = y;
   // define radius of flower
   var radius = Math.min(w, h) / 9;
   // tinker with petals --1.5, 3, 4 are fun
   // 5 - 9 work well as snowflakes but since it is small 5 will do
   var petalVar = 5;
   // define resolution of plot (angular distance between samples)
   var plotRes = .02;
   // color - winter color
   RC.setPenColor(.7, .7, .9);
   // plot a petaled flower
   for (var radianAngle = 0; radianAngle < 2*Math.PI; radianAngle += plotRes) {
   var distance = Math.sin(petalVar * radianAngle) * radius;
   var cartesianPoint = polarToCartesian(distance, radianAngle);
   RC.setPixel(centerX+cartesianPoint[0], centerY+cartesianPoint[1], opacity);
  
   }
  }
}

// Make a pattern layer to go overtop of snowflakes
RC.newArtLayer(LayerKind.NORMAL);

var opacity = .5;
var tilesX = 5;
var tilesY = 3;
var thresholdX = w / tilesX;
var thresholdY = h / tilesY;
for (var x = 0; x < w; x+=1.55) {
   for (var y = 0; y < h; y+=1.55) {
      var tileR = 1 - (x % thresholdX / thresholdX);
      var tileB = y % tileR;
      RC.setPenColor(.5, .5, tileB);
      RC.setPixel(x,y, opacity);
   }
}



// release object referencess
RC = null;




// Pete's helper functions ---------------------------------

function polarToCartesian(distance, radianAngle) {
   var x = distance * Math.cos(radianAngle);
   var y = distance * Math.sin(radianAngle);
   return new Array(x, y);
}


function cartesianToPolar(x, y) {
   var distance = Math.sqrt(x*x + y*y);
   var radianAngle;
   if (x == 0) {
      if (y >= 0) { radianAngle = Math.PI * .5; }
      else        { radianAngle = 3*Math.PI * .5; }
   }
   else if (x > 0) { radianAngle = Math.atan(y / x); }
   else            { radianAngle = Math.PI - Math.atan(y / -x); }
   
   return new Array(distance, radianAngle);
}