//task05



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

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

// init a color to use
RC.setPenColor(0, 0, 0);

//draw background
var freq = 100; //background grid 
var step = new Array; //creat an array to store the points of the grid
for (iw=0; iw<=freq; iw++){	
	step [iw] = new Array;
	for (ih=0; ih<=freq; ih++){
		step [iw][ih] = new Array (iw*w/freq, ih*h/freq);
	}
}
//draw the background grid
for (iw=0; iw<freq; iw++){
	for (ih=0; ihfreq; ih++){
		RC.setPenColor((iw%10/40+8)*.105, (ih%10/40+8)*.105, (iw*ih%10/40+8)*.1);
		var tRegion = new Array(step[iw][ih],step[iw+1][ih],step[iw+1][ih+1],step[iw][ih+1]);
		RC.fillPoly(tRegion);
	}
}


RC.newArtLayer(LayerKind.NORMAL);
// Using polar coords
// drawing a spiral

// define limit x and y to help stay on canvas
var limX = w-1;
var limY = h-1;
// define centerpoint of the spiral
var centerX = limX / 2;
var centerY = limY / 2;
// define resolution of plot (angular distance between samples)
var plotRes = .2;
// spiral params
var revolutions = 7; // number of time to go round
var totalRadians = 2 * Math.PI * revolutions;
var growthInc = .03; // affects how rapidly spiral gets bigger

//create arrays to hold the points info on the spirals
var spiralA = new Array ();
var spiralB = new Array ();

//input values for the points on spiral A
for (var radianAngle=0, distance=0, i=0; radianAngle  totalRadians; radianAngle+=plotRes, growthInc+=.01, distance+=growthInc, i++) {
   var cartesianPoint = polarToCartesian(distance, radianAngle);  
   spiralA [i] = new Array (centerX+cartesianPoint[0],centerY+cartesianPoint[1]);
}

//input values for the points on spiral B
for (var radianAngle=Math.PI*.5, distance=0, growthInc=.03, i=0; radianAngle < totalRadians+Math.PI*.5; radianAngle+=plotRes, growthInc+=.01, distance+=growthInc, i++) {
   var cartesianPoint = polarToCartesian(distance, radianAngle);
   spiralB [i] = new Array (centerX+cartesianPoint[0],centerY+cartesianPoint[1]);
}
   
//draw one loop of triangles
for (i=30; i<totalRadians/plotRes-31; i++){
	RC.setPenColor(i/250+.05, i/250, i/250);
	var sRegion = new Array(spiralA[i],spiralA[i+1],spiralB[i-Math.round((Math.PI*.5)/plotRes)]);
	RC.fillPoly(sRegion);
}
//draw another loop of triangles
for (i=30; i<totalRadians/plotRes-31; i++){
	RC.setPenColor(i/200, i/200, i/200);
	var sRegion = new Array(spiralA[i],spiralA[i+1],spiralA[i+Math.round((Math.PI*2)/plotRes)]);
	RC.fillPoly(sRegion);
}  

// release object referencess
RC = null;





// imitating a bouncing ball


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

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

// set a color to use
RC.setPenColor(0, 0, 0);

//help function to draw a ball in the given position with the radius and scale
function drawBall(px,py,r,opac,scale){
	for (var radianAngle = 0; radianAngle < 2*Math.PI; radianAngle += .05) {
	   var cartesianPoint = polarToCartesian(r, radianAngle);
	   RC.setPixel(px+cartesianPoint[0]*scale, py+cartesianPoint[1]/scale, opac);
	}
}

//define arrays to hold the positions and scaling factor of the ball in different frames
positionX = new Array ();
positionY = new Array ();
scale = new Array ();

//howmany frame to calculate in the scene
var howmany = 17;

//input values into the arrays
//using absolute sin wave to fake it
//scale is a function of Y value
for (var i=0; i<howmany; i++) {
	positionX[i] = (w/(howmany+2))*(i+1.5);
	posY=Math.abs(Math.sin(i*2*Math.PI/(howmany-1)+Math.PI/2));
	positionY[i] = -posY*100+180;
	scale[i] = Math.sqrt(Math.sqrt(1.5-posY));
}

//loop through each frame to draw the ball, put them in different layers
for (i=0; i<howmany; i++){
	RC.newArtLayer(LayerKind.NORMAL);
	
	//loop 3 times to also draw the faded ball of the previous 2 frames
	for (var j=i; j>i-3; j--){		
		drawBall(positionX[j],positionY[j],15,1-(i-j)/2.3,scale[j]);
	}
}

// release object referencess
RC = null;