forked from: flash on 2012-3-7
/**
* Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/16q1
*/
// forked from tepe's flash on 2012-3-7
package {
import flash.display.Sprite;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
public class FlashTest extends Sprite {
private var node:Array = new Array();
private var tf:TextField;
public function FlashTest() {
// write as3 code here..
init();
}
private function init():void{
var ar2:Array;
ar2 = setNode(10);
//shuffle(node);
var ar:Array;
ar = shuffle(ar2);
drawline(ar);
tf = new TextField();
addChild(tf);
tf.height = 200;
tf.width = 300;
tf.text = ar.toString();
for(var j:int=0;j<10;j++){
ar = shuffle(ar2);
tf.appendText("\n"+ar.toString());
tf.appendText(" "+String(func1(ar)));
}
addEventListener(KeyboardEvent.KEY_DOWN,onKey);
}
private var select:Array;
private function onKey(e:KeyboardEvent):void{
if(e.keyCode == 90){
var ar2:Array;
ar2 = setNode(10);
//shuffle(node);
var ar:Array;
ar = shuffle(ar2);
drawline(ar);
select = null;
select = new Array();
tf.text = ar.toString();
for(var j:int=0;j<10;j++){
//var ar3:Array = new Array();
//ar3 = shuffle(ar2)
select[j] = shuffle(ar2);
tf.appendText("\n"+select[j].toString());
tf.appendText(" "+String(func1(select[j])));
}
}
if(e.keyCode ==65){
graphics.clear();
for(var i:int=0;i<node.length;i++){
graphics.lineStyle(1,0x0);
graphics.beginFill(0x00ffff);
graphics.drawCircle(node[i].x,node[i].y,5);
graphics.endFill();
//var n:int = Math.floor(Math.random()*10);
//drawline(select[n]);
}
var n:int = Math.floor(Math.random()*10);
drawline(select[n]);
tf.text = select[n].toString();
for(i=0;i<select.length;i++){
tf.appendText("\n"+select[i].toString());
}
}
}
private function setNode(n:int = 10):Array{
var ar2:Array = new Array();
graphics.clear();
node = null;
node = new Array();
for(var i:int=0;i<n;i++){
var pos:Point = new Point(Math.random()*400,Math.random()*400);
node.push(pos)
graphics.lineStyle(1,0x0);
graphics.beginFill(0x00ffff);
graphics.drawCircle(pos.x,pos.y,5);
graphics.endFill();
ar2.push(i);
}
return ar2;
}
//遺伝子生成
private function shuffle(arr:Array):Array{
var num:int = arr.length; // 配列の数
for (var i:int = 0; i < num; i++)
{
var rundomNum:int = Math.floor(Math.random() * num);
var temp:int = arr[rundomNum];
arr[rundomNum] = arr[i];
arr[i] = temp;
}
return arr;
}
private function drawline(arr:Array):void{
//graphics.clear();
graphics.lineStyle(1,0xff0000);
graphics.moveTo(node[arr[arr.length-1]].x,node[arr[arr.length-1]].y);
for(var i:int=0;i<arr.length;i++){
graphics.lineTo(node[arr[i]].x,node[arr[i]].y);
}
}
//距離を測る
private function func1(arr:Array):Number{
var total:Number = 0;
total += Point.distance( node[arr[9]], node[arr[0]] );
for(var i:int=0;i<9;i++){
total += Point.distance( node[arr[i]], node[arr[i+1]] );
}
return total;
}
}
}