flash on 2011-3-23
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
private var origin:Point = new Point(Math.random()*550,Math.random()*400);
private var oIndex:uint = 0;
private var mouse:Point = new Point(0,0);
private var drawing:Boolean = false;
private var nodes:Array = new Array();
private var links:Array = new Array();
private const RAD:uint = 15*15
public function FlashTest() {
generate();
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
}
public function generate():void {
var o:Object;
for(var i:int=0;i<5;i++){
o = new Object();
do {
o.x = Math.random()*300;
o.y = Math.random()*300;
}while(check(o));
nodes.push(o);
}
}
public function check(o:Object):Boolean {
var n:Object;
var X:int,Y:int,D:int;
for(var i:int=0;i<nodes.length;i++){
n = nodes[i];
X = o.x-n.x;
Y = o.y-n.y;
D = dist(X,Y);
if(D < 30*30) return true;
}
return false;
}
public function main(e:Event):void {
if(drawing) {
mouse.x = mouseX;
mouse.y = mouseY;
}
render();
}
public function mouseDown(e:MouseEvent):void {
var n:Object;
var X:int,Y:int,D:uint;
for(var i:int=0;i<nodes.length;i++){
n = nodes[i];
X = mouseX-n.x;
Y = mouseY-n.y;
D = dist(X,Y);
if(D < RAD) {
origin.x = n.x;
origin.y = n.y;
drawing = true;
oIndex = i;
return;
}
}
}
public function mouseUp(e:MouseEvent):void {
drawing = false;
var n:Object;
var X:int,Y:int,D:int;
for(var i:int=0;i<nodes.length;i++) {
n = nodes[i];
X = n.x-mouseX;
Y = n.y-mouseY;
D = dist(X,Y);
if(D < RAD){
if(i == oIndex) return;
for(var j:int=0;j<links.length;j++) {
if(i == links[i].x && oIndex == links[i].y) return;
if(i == links[i].y && oIndex == links[i].x) return;
}
links.push(new Point(i,oIndex));
}
}
}
public function drawLine(pStart:Point,pEnd:Point):void {
graphics.lineStyle(4,0xffff00,1);
graphics.moveTo(pStart.x,pStart.y);
var X:int = pEnd.x-pStart.x;
var Y:int = pEnd.y-pStart.y;
var toPoint:Point = new Point();
var dist:uint = Math.sqrt(dist(X,Y));
var angle:Number = Math.atan2(Y,X);
var midPoints:uint = Math.floor(dist/20);
for(var i:int=1;i<midPoints;i++) {
toPoint.x = origin.x + X/midPoints*i;
toPoint.y = origin.y + Y/midPoints*i;
var variation:Number = Math.random()*12-6;
toPoint.x += Math.cos(angle+Math.PI/2)*variation;
toPoint.y += Math.sin(angle+Math.PI/2)*variation;
graphics.lineTo(toPoint.x,toPoint.y);
}
graphics.lineTo(pEnd.x,pEnd.y);
}
public function dist(X:int,Y:int):uint {
return X*X+Y*Y;
}
public function render():void
{
graphics.clear();
if(drawing) drawLine(origin,mouse);
var sPoint:Point = new Point(), ePoint:Point = new Point();
for(var i:int=0;i<links.length;i++) {
sPoint.x = nodes[links[i].x].x;
sPoint.y = nodes[links[i].x].y;
ePoint.x = nodes[links[i].y].x;
ePoint.y = nodes[links[i].y].y;
drawLine(ePoint,sPoint);
}
var n:Object;
for(i=0;i<nodes.length;i++) {
n = nodes[i];
graphics.lineStyle(2,0x6666cc,1);
graphics.beginFill(0x9999ff);
graphics.drawCircle(n.x,n.y,15);
graphics.endFill();
}
}
}
}