forked from: Tiling Hexagon 六角模様
package
{
import flash.text.TextField;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.utils.getQualifiedClassName;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="0")]
public class HexaGraphics2 extends Sprite
{
private var output:TextField;
public function HexaGraphics2()
{
initOutput();
println('Construct ' + getQualifiedClassName(this));
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
public function init():void {
fillHexagon(2.5, 0.5, 96, 64);
}
private function fillHexagon(r:Number, space:Number, cx:int, cy:int):void {
var cos30:Number = Math.cos(Math.PI / 6);
var dist:Number = (2 * (r + space) * cos30);
var c1:Array = [0xff0000, 0x00ff00, 0x0000ff];
var c2:Array = [0x0000ff, 0xff0000, 0x00ff00];
var i:int;
for (var j:int = 0; j < cy; j++) {
for (i = 0; i < cx; i++) {
graphics.beginFill(c1[i%3]);
drawHexagon(r, i * dist, j * 2 * dist * cos30);
graphics.endFill();
}
for (i = 0; i < cx; i++) {
graphics.beginFill(c2[i%3]);
drawHexagon(r, (i + 0.5) * dist, (j * 2 + 1) * dist * cos30);
graphics.endFill();
}
}
}
private function drawHexagon(r:Number, x:Number = 0, y:Number = 0):void
{
var th:Number = Math.PI / 6;
var cos:Number = r * Math.cos(th);
var sin:Number = r * Math.sin(th);
graphics.moveTo(x + 0, y - r);
graphics.lineTo(x + cos, y -sin);
graphics.lineTo(x + cos, y + sin);
graphics.lineTo(x + 0, y + r);
graphics.lineTo(x - cos, y + sin);
graphics.lineTo(x - cos, y - sin);
graphics.lineTo(x, y - r);
}
private function addedToStage(e:Event):void
{
removeEventListener(e.type, arguments.callee);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
init();
}
////////////////
// 出力
///////////////
private function initOutput():void {
var margin:Number = 10;
addChild(output = new TextField());
output.selectable = false;
output.multiline = true;
output.width = stage.stageWidth - margin * 2;
output.height = stage.stageHeight - margin * 2;
}
public function print(... args):void {
output.appendText(args.join(", "));
}
public function println(... args):void {
output.appendText(args.join(", "));
output.appendText("\n");
}
}
}