/* A surface filled with one hundred medium to small sized circles. Each circle has a different size and direction, but moves at the same slow rate. Display: >>> A. The instantaneous intersections of the circles B. The aggregate intersections of the circles Implemented by J. Tarbell <http://levitated.net> 8 April 2004 Flash MX Flash files must be attached to different parts of the Flash environment before they will compile. They are presented on this page outside their context. */ // Code in the first frame of the Timeline Object.env = this; numCircle = 100; circles = new Array(); for (var i = 0; i < numCircle; i++) { var nombre = "circle"+String(i); var init = {_x:Math.random()*Stage.width,_y:Stage.height*i/numCircle,r:10+Math.random()*50,sp:Math.random()-Math.random(),ysp:Math.random()-Math.random(),id:i}; var neo = this.attachMovie("circle",nombre,i,init); this.circles.push(neo); } this.onEnterFrame = function() { this.clear(); for (var i = 0; i < numCircle; i++) { circles[i].update(); } for (var i = 0; i < numCircle; i++) { circles[i].move(); } } stop(); // Code associated with the circle Movie Clip #initclip // constructor function Circle() { this.r2 = this.r*this.r; } // allow component to inherit MovieClip properties Circle.prototype = new MovieClip(); // instance methods Circle.prototype.update = function() { for (var i = this.id+1; i < Object.env.numCircle; i++) { this.intersect(Object.env.circles[i]); } }; Circle.prototype.makepoint = function() { // not required, point exists as graphic in movieclip }; Circle.prototype.move = function() { this._x += this.sp; this._y += this.ysp; if(this.sp > 0) { if(this._x > Stage.width+this.r) { this._x = -this.r; } } else { if(this._x < -this.r) { this._x = Stage.width+this.r; } } if(this.ysp > 0) { if(this._y > Stage.height+this.r) { this._y = -this.r; } } else { if(this._y < -this.r) { this._y = Stage.height+this.r; } } }; Circle.prototype.intersect = function( cB ) { var cA = this; var dx = cA._x - cB._x; var dy = cA._y - cB._y; var d2 = dx*dx + dy*dy; var d = Math.sqrt(d2); if ( d>cA.r+cB.r || d<Math.abs(cA.r-cB.r) ) { return; // no solution } var a = (cA.r2 - cB.r2 + d2) / (2*d); var h = Math.sqrt( cA.r2 - a*a ); var x2 = cA._x + a*(cB._x - cA._x)/d; var y2 = cA._y + a*(cB._y - cA._y)/d; var paX = x2 + h*(cB._y - cA._y)/d; var paY = y2 - h*(cB._x - cA._x)/d; var pbX = x2 - h*(cB._y - cA._y)/d; var pbY = y2 + h*(cB._x - cA._x)/d; var dist = Math.sqrt((paX-pbX)*(paX-pbX)+(paY-pbY)*(paY-pbY)); Object.env.lineStyle(0,0x000000,dist*1.587); Object.env.moveTo(paX,paY); Object.env.lineTo(pbX,pbY); }; // Connect the class with the linkage ID for this movie clip Object.registerClass("circle", Circle); #endinitclip