package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.Keyboard;
import flash.geom.*;
import flash.filters.*;
[SWF(width="500", height="500", frameRate="10", backgroundColor="#ffffff")]
public class SubdivisionCurves extends Sprite{
[Embed(source="subdivision.PNG")]
private var myImage:Class;
private var _balls:Array = [];
private var _vertices:Array = [];
public function SubdivisionCurves():void
{
var points:Array = [new Point(100, 200), new Point(20, 400)
, new Point(300, 400), new Point(400, 300) , new Point(300, 100)
];
for(var i:int = 0 ; i < points.length ; i++){
var b:Ball = new Ball(0xff0000, 10);
_balls.push(b);
addChild(b);
b.x = points[i].x;
b.y = points[i].y;
b.addEventListener( MouseEvent.MOUSE_DOWN, pickup );
b.addEventListener( MouseEvent.MOUSE_UP, place );
b.addEventListener( MouseEvent.MOUSE_MOVE, function(e:*):void{
// calcVolume();
});
}
initVertices();
var bt:SimpleButton = new SimpleButton();
bt.upState = new myImage();
bt.overState = bt.upState;
bt.hitTestState = bt.upState;
addChild(bt);
bt.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
subdivision();
});
}
private function initVertices():void{
_vertices = [];
for each (var b:Ball in _balls){
_vertices.push(new Point(b.x, b.y));
}
drawVertices();
}
private function pickup( event:MouseEvent ):void {
event.target.startDrag( );
event.target.filters = [ new DropShadowFilter( ) ];
setChildIndex( DisplayObject( event.target ), numChildren - 1 );
}
private function place( event:MouseEvent ):void {
event.target.stopDrag( );
event.target.filters = null;
initVertices();
}
private function subdivisionFromTo(from:Point, to:Point):Array{
var v1:Point = new Point(from.x + (to.x - from.x) * (1/4),
from.y + (to.y - from.y) * (1/4));
var v2:Point = new Point(from.x + (to.x - from.x) * (3/4),
from.y + (to.y - from.y) * (3/4));
return [v1, v2];
}
private function subdivision():void{
trace(_vertices.length);
if(_vertices.length > 100)
return;
var newVertices:Array = [];
var temp:Array;
for (var i:int = 0 ; i < _vertices.length - 1 ; i++){
temp = subdivisionFromTo(_vertices[i], _vertices[i+1]);
newVertices = newVertices.concat(temp);
}
temp = subdivisionFromTo(_vertices[_vertices.length - 1], _vertices[0]);
newVertices = newVertices.concat(temp);
_vertices = newVertices.concat();
drawVertices();
}
private function drawVertices():void{
graphics.clear();
graphics.lineStyle(4,0xff,0.5);
graphics.moveTo(this._vertices[0].x, this._vertices[0].y);
for (var i:int = 1 ; i < this._vertices.length ; i++){
graphics.lineTo(this._vertices[i].x, this._vertices[i].y);
}
graphics.lineTo(this._vertices[0].x, this._vertices[0].y);
}
}
}
import flash.display.*;
internal class Ball extends Sprite{
public function Ball(color:int, radius:int){
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}
Author:yamasv@gmail.com
コメント、トラックバック、リンクはお気軽に
-->
| 日 | 月 | 火 | 水 | 木 | 金 | 土 |
|---|---|---|---|---|---|---|
| - | - | 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | - | - | - |