flash on 2010-2-28
package {
import flash.display.*;
import flash.events.*;
public class Test extends Sprite {
public var dX:int = 10;
public var dY:int = 10;
public var areas:Vector.<Vector.<Vector.<Dot>>> = new Vector.<Vector.<Vector.<Dot>>>;
public var dots:int = 50;
public var lines:Sprite;
public function Test():void {
lines = Sprite(addChild(new Sprite()));
lines.graphics.lineStyle(1, 0xFF0000);
var i:int, j:int;
this.graphics.lineStyle(1, 0xBBBBBB);
for(i = 0; i < dY; i++) {
if(i > 0 && i < dY) {
this.graphics.moveTo(0, (stage.stageHeight / dY) * i);
this.graphics.lineTo(stage.stageWidth, (stage.stageHeight / dY) * i);
}
areas[i] = new Vector.<Vector.<Dot>>;
for(j = 0; j < dX; j++) {
if(j > 0 && j < dX) {
this.graphics.moveTo((stage.stageWidth / dX) * j, 0);
this.graphics.lineTo((stage.stageWidth / dX) * j, stage.stageHeight);
}
areas[i][j] = new Vector.<Dot>;
}
}
for(i = 0; i < dots; i++) {
this.addChild(new Dot(i, stage, areas, new Array(dX, dY)));
}
stage.addEventListener(Event.ENTER_FRAME, main);
}
public function main(e:Event):void {
lines.graphics.clear();
lines.graphics.lineStyle(1, 0xFF0000);
for(var i:int = 0; i < areas.length; i++) {
for(var j:int = 0; j < areas[i].length; j++) {
if(areas[i][j].length > 1) {
for(var k:int = 0; k < areas[i][j].length; k++) {
for(var l:int = 0; l < areas[i][j].length; l++) {
//if(k != l) {
var a:Dot = areas[i][j][k];
var b:Dot = areas[i][j][l];
lines.graphics.moveTo(a.x, a.y);
lines.graphics.lineTo(b.x, b.y);
//}
}
}
} else {
if(areas[i][j].length == 0) {
lines.graphics.beginFill(0xFF0000);
lines.graphics.drawRect(j * (stage.stageWidth / dX), i * (stage.stageHeight / dY), stage.stageWidth / dX, stage.stageHeight / dY);
lines.graphics.endFill();
}
}
}
}
}
}
}
import flash.display.*;
import flash.events.*;
class Dot extends Sprite {
public var s:Stage;
public var v:Vector.<Vector.<Vector.<Dot>>>;
public var d:Array;
public var xv:Number;
public var yv:Number;
public var cx:int;
public var cy:int;
public var id:int;
public function Dot(_id:int, _s:Stage, _v:Vector.<Vector.<Vector.<Dot>>>, _d:Array):void {
id = _id;
s = _s;
v = _v;
d = _d;
this.graphics.beginFill(0x222222);
this.graphics.drawCircle(0, 0, 2);
this.graphics.endFill();
x = Math.floor(Math.random() * s.stageWidth);
y = Math.floor(Math.random() * s.stageHeight);
var r:int = Math.floor(Math.random() * 360);
xv = 4 * Math.cos((r * Math.PI) / 180);
yv = 4 * Math.sin((r * Math.PI) / 180);
cx = calcX();
cy = calcY();
v[cy][cx].push(this);
s.addEventListener(Event.ENTER_FRAME, main);
}
public function calcX():int {
return Math.floor(d[0] / s.stageWidth * x) >= d[0] ? d[0] - 1 : Math.floor(d[0] / s.stageWidth * x);
}
public function calcY():int {
return Math.floor(d[1] / s.stageHeight * y) >= d[1] ? d[1] - 1 : Math.floor(d[1] / s.stageHeight * y);
}
public function main(e:Event):void {
x += xv;
y += yv;
if(x > s.stageWidth - (xv > 0 ? xv : 0)) {
xv = -Math.abs(xv);
}
if(x < 0 - (xv < 0 ? xv : 0)) {
xv = Math.abs(xv);
}
if(y > s.stageHeight - (yv > 0 ? yv : 0)) {
yv = -Math.abs(yv);
}
if(y < 0 - (yv < 0 ? yv : 0)) {
yv = Math.abs(yv);
}
var acx:int = calcX();
var acy:int = calcY();
if(cx != acx || cy != acy) {
v[cy][cx].splice(v[cy][cx].indexOf(this), 1);
cx = acx;
cy = acy;
v[cy][cx].push(this);
}
}
}