package { import flash.display.*; import flash.events.*; dynamic public class Base_Class extends MovieClip { public var shBack:Shape; public var curPhi:Number; public var sphere_shape:Shape; public var curTheta:Number; public var tilesColors:Array; public var nMesh:Number; public var spBoard:Sprite; public var prevY:Number; public var prevX:Number; public var firstRun:Boolean; public var doRotate:Boolean; public var fLen:Number; // focal length public var tilesArray:Array; public function Base_Class() // constructor { addFrameScript(0, frame1); return; } // -------------------------------------------------------- // function frame1() { fLen = 2000; spBoard = new Sprite(); this.addChild(spBoard); // spBoard.x = 200; // spBoard.y = 190; spBoard.x = 130; spBoard.y = 130; shBack = new Shape(); spBoard.addChild(shBack); drawBack(); sphere_shape = new Shape(); spBoard.addChild(sphere_shape); doRotate = false; curTheta = -10; curPhi = 60; // nMesh = 30; nMesh = 30; tilesArray = []; firstRun = true; tilesColors = []; setTilesArray(); renderView(curTheta, curPhi); spBoard.addEventListener(MouseEvent.ROLL_OUT, boardOut); spBoard.addEventListener(MouseEvent.MOUSE_MOVE, boardMove); spBoard.addEventListener(MouseEvent.MOUSE_DOWN, boardDown); spBoard.addEventListener(MouseEvent.MOUSE_UP, boardUp); return; }// end function: frame1() // -------------------------------------------------------------------------- // This function takes red, green and blue components of a color and // returns the combined RGB number corresponding to the color. // public function combineRGB(red:Number, green:Number, blue:Number) : Number { var rgb:Number; if (red > 255) {red = 255;} if (green > 255) {green = 255;} if (blue > 255) {blue = 255;} if (red < 0) {red = 0;} if (green < 0) {green = 0;} if (blue < 0) {blue = 0;} // bitwise shift rgb = red << 16 | green << 8 | blue; return rgb; }// bottom of function: "combineRGB()" // -------------------------------------------------- // public function boardUp(ee:MouseEvent) : void { doRotate = false; return; } // -------------------------------------------------- // public function drawBack() : void { shBack.graphics.lineStyle(1, 0); // shBack.graphics.beginFill(0); shBack.graphics.beginFill(0x000000, 0.0); // shBack.graphics.drawRect(-160, -160, 320, 320); shBack.graphics.drawRect(-200, -200, 350, 350); shBack.graphics.endFill(); return; }// end function // -------------------------------------------------- //param1: theta //param2: phi public function renderView(theta:Number, phi:Number) : void { var ii:int; var jj:int; var nn:int; var distance_array:*; var projection_array:*; var new_tiles_array:*; var mid_point:Array; var distance:Number; // distance to viewer var depth_length:Number; // depth length, length of array of vertices var color_factor:*; distance_array = []; projection_array = []; new_tiles_array = []; mid_point = []; color_factor = 1.2; // convert to radians theta = theta * Math.PI / 180; phi = phi * Math.PI / 180; // clear the previous view of the sphere sphere_shape.graphics.clear(); ii = 0; while (ii <= nMesh) { // new_tiles_array[ii] = []; jj = 0; while (jj <= nMesh) { // calculate new rotated points new_tiles_array[ii][jj] = pointNewView(tilesArray[ii][jj], theta, phi); jj++; } ii++; } ii = 0; while (ii < nMesh) { if (firstRun) { tilesColors[ii] = []; } jj = 0; while (jj < nMesh) { // mid_point[0] = (new_tiles_array[ii][jj][0] + new_tiles_array[ii + 1][jj][0] + new_tiles_array[ii][jj + 1][0] + new_tiles_array[ii + 1][jj + 1][0]) / 4; mid_point[1] = (new_tiles_array[ii][jj][1] + new_tiles_array[ii + 1][jj][1] + new_tiles_array[ii][jj + 1][1] + new_tiles_array[ii + 1][jj + 1][1]) / 4; mid_point[2] = (new_tiles_array[ii][jj][2] + new_tiles_array[ii + 1][jj][2] + new_tiles_array[ii][jj + 1][2] + new_tiles_array[ii + 1][jj + 1][2]) / 4; distance = Math.sqrt(Math.pow(fLen - mid_point[0], 2) + Math.pow(mid_point[1], 2) + Math.pow(mid_point[2], 2)); // distance array saves vertex indices with // thier cooresponding distance to viewer distance_array.push([distance, ii, jj]); // The coloring for each face is determined // based on the initial position of each face. if (firstRun) { tilesColors[ii][jj] = combineRGB((2 * color_factor * mid_point[2] + 100) * 0.8 + 70, (2 * color_factor * mid_point[1] + 100) * 0.8 + 70, (2 * color_factor * mid_point[0] + 100) * 0.8 + 70); } jj++; }// end while (jj < nMesh) ii++; }// end while (ii < nMesh) //distance_array.sort(byDist); ii = 0; while (ii <= nMesh) { // projection_array[ii] = []; jj = 0; while (jj <= nMesh) { // calculate projected points projection_array[ii][jj] = [fLen / (fLen - new_tiles_array[ii][jj][0]) * new_tiles_array[ii][jj][1], (-fLen) / (fLen - new_tiles_array[ii][jj][0]) * new_tiles_array[ii][jj][2]]; jj++; } ii++; } // sort array based on distance of points to viewer distance_array.sort(byDist); depth_length = distance_array.length; sphere_shape.graphics.lineStyle(1, 3355443); nn = 0; while (nn < depth_length) { ii = distance_array[nn][1]; jj = distance_array[nn][2]; // pixels are drawn using the projected points sphere_shape.graphics.moveTo(projection_array[ii][jj][0], projection_array[ii][jj][1]); sphere_shape.graphics.beginFill(tilesColors[ii][jj], 1); sphere_shape.graphics.lineTo(projection_array[ii][jj + 1][0], projection_array[ii][jj + 1][1]); sphere_shape.graphics.lineTo(projection_array[ii + 1][jj + 1][0], projection_array[ii + 1][jj + 1][1]); sphere_shape.graphics.lineTo(projection_array[ii + 1][jj][0], projection_array[ii + 1][jj][1]); sphere_shape.graphics.lineTo(projection_array[ii][jj][0], projection_array[ii][jj][1]); sphere_shape.graphics.endFill(); nn++; }// end while firstRun = false; return; }// end function: renderView() // -------------------------------------------------- // public function boardOut(ee:MouseEvent) : void { doRotate = false; return; } // -------------------------------------------------- // public function byDist(vv:Array, ww:Array) : Number { if (vv[0] > ww[0]) { return -1; } if (vv[0] < ww[0]) { return 1; } return 0; } // -------------------------------------------------- // public function boardDown(ee:MouseEvent) : void { prevX = spBoard.mouseX; prevY = spBoard.mouseY; doRotate = true; return; } // -------------------------------------------------- // public function pointNewView(vv:Array, theta:Number, phi:Number) : Array { var new_coords:Array; new_coords = []; new_coords[0] = vv[0] * Math.cos(theta) * Math.sin(phi) + vv[1] * Math.sin(theta) * Math.sin(phi) + vv[2] * Math.cos(phi); new_coords[1] = (-vv[0]) * Math.sin(theta) + vv[1] * Math.cos(theta); new_coords[2] = (-vv[0]) * Math.cos(theta) * Math.cos(phi) - vv[1] * Math.sin(theta) * Math.cos(phi) + vv[2] * Math.sin(phi); return new_coords; } // ----------------------------------------------------------------------- // // Here we define vertices that correspond to a parametrized // sphere with radius 90. ii and jj serve as sample values for the parameters. // ii changes between 0 and 7/4*pi to keep the sphere open, jj from 0 to pi. // (ii and jj correspond to the parametrization of the sphere // by spherical coordinates theta and phi.) // public function setTilesArray() : void { var ii:int; var jj:int; var i_step:Number; var j_step:Number; i_step = 7 / 4 * Math.PI / nMesh; j_step = Math.PI / nMesh; ii = 0; while (ii <= nMesh) { // tilesArray[ii] = []; jj = 0; while (jj <= nMesh) { // x, y, z, positions on sphere tilesArray[ii][jj] = [ 90 * Math.cos(i_step * ii) * Math.sin(j_step * jj), 90 * Math.sin(i_step * ii) * Math.sin(j_step * jj), 90 * Math.cos(j_step * jj) ]; jj++; } ii++; } return; }// end function: setTilesArray() // ---------------------------------------------------------------- // public function boardMove(ee:MouseEvent) : void { var old_X:Number; var old_Y:Number; var new_X:Number; var new_Y:Number; // save previous orientation old_X = prevX; old_Y = prevY; if (doRotate) { new_X = prevX = spBoard.mouseX; new_Y = prevY = spBoard.mouseY; curTheta = curTheta + (new_X - old_X); curPhi = curPhi + (new_Y - old_Y); // redraw the sphere renderView(curTheta, curPhi); ee.updateAfterEvent(); } return; } }// bottom of: dynamic public class Base_Class }// bottom of: package