forked from: 3D空間にあるオブジェクトの2D座標を得る
/**
* Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7o07
*/
// forked from Kay's 3D空間にあるオブジェクトの2D座標を得る
package {
import flash.display.*;
import flash.geom.*;
[SWF(width=465,height=465)]
public class ProjectTest2 extends Sprite {
private const SW:Number = stage.stageWidth;
private const SH:Number = stage.stageHeight;
public function ProjectTest2():void {
var proj:PerspectiveProjection = root.transform.perspectiveProjection;
proj.fieldOfView = 45;
var disPoint:Point = new Point(100, -50); // カンバス中央から
proj.projectionCenter = new Point(SW/2+disPoint.x,SH/2+disPoint.y);
var canvas:Canvas = new Canvas(SW,SH,disPoint);
canvas.x = SW/2;
canvas.y = SH/2;
addChild(canvas);
var numPoints:int = 6;
for (var i:int = 0; i < numPoints; i++) {
var dot:Dot = new Dot();
dot.x = Math.random()*SW-SW/2;
dot.y = Math.random()*SH-SH/2;
dot.z = Math.random()*200-50;
canvas.addChild(dot);
}
//---------------------------------------------------
// 3D/2D Vec
for (i = 0; i < numPoints; i++) {
var myPoint:Object = canvas.getChildAt(i);
var hint:Hint = new Hint(myPoint,15,0x0000ff);
canvas.addChild(hint);
// 2Dvec
var ratio:Number = proj.focalLength/(proj.focalLength+myPoint.z);
if (myPoint.z != 0) {
hint.x = myPoint.x*ratio + disPoint.x - disPoint.x*ratio;
hint.y = myPoint.y*ratio + disPoint.y - disPoint.y*ratio;
} else {
hint.x = myPoint.x*ratio;
hint.y = myPoint.y*ratio;
}
hint.z = 0;
hint.dispVector();
}
}
}
}
import flash.display.*;
import flash.geom.*;
import flash.text.*;
class Canvas extends Sprite
{
public function Canvas(w:Number, h:Number, p:Point):void
{
graphics.lineStyle(0,0x999999);
// x
graphics.moveTo(-w/2,0);
graphics.lineTo(w/2,0);
// y
graphics.moveTo(0,-h/2);
graphics.lineTo(0,h/2);
// z
graphics.moveTo(-p.x,-p.y);
graphics.lineTo(p.x,p.y);
}
}
class Dot extends Sprite {
public function Dot():void {
graphics.beginFill(0x0099ff);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
class Hint extends Sprite {
private var myObject:Object;
private var field:TextField = new TextField();
public function Hint(obj:Object, size:Number=0, nColor:int=0xff0000):void {
myObject = obj;
graphics.lineStyle(0);
graphics.moveTo(0,0);
graphics.lineTo(-size,-size);
field.autoSize = TextFieldAutoSize.LEFT;
field.selectable = false;
field.text = "temp\ntemp";
field.x = -field.height-size;
field.y = -field.height-size;
addChild(field);
}
public function dispVector():void {
field.text = '3D(x:'+myObject.x+', y:'+myObject.y+', z:'+myObject.z+")\n2D(x:"+x+', y:'+y+')';
}
}