Inital Code Referenced from http://www.nicoptere.net/AS3/homographie/blog/Homography.as
to project to perspective plane defined by 4 points.
Used http://www.mathpapa.com/algebra-calculator.html to reverse the invert() method
to get the point on projection.
//Inital Code Referenced from http://www.nicoptere.net/AS3/homographie/blog/Homography.as
//to project to perspective plane defined by 4 points.
//Used http://www.mathpapa.com/algebra-calculator.html to reverse the invert() method
//to get the point on projection.
package {
import flash.display.*;
import flash.utils.*;
import flash.geom.*;
import flash.events.*;
public class DrawStar extends Sprite {
private var _pointList:Array = [];
private var dotty:Sprite;
private var dotty2:Sprite;
private var s:Array;
public function DrawStar() {
super();
stage.addEventListener(Event.ENTER_FRAME, onLoop);
_pointList = [new Point (250,50), new Point (450,50), new Point (440,280), new Point (160,300)];
var sp:Sprite= addChild( new Sprite() ) as Sprite;
//sp.x = 20;
var drawer:Drawer = new Rect(2);
drawer.draw( sp.graphics, _pointList);
s = getSystem(_pointList);
var a:Number = s[0];
var b:Number = s[1];
var c:Number = s[2];
var d:Number = s[3];
var i:Number = s[4];
var f:Number = s[5];
var g:Number = s[6];
var h:Number = s[7];
var x:Number = 160;
var y:Number = 140;
var v:Number = (0-c*g*y+f*g*x-a*f+a*y+c*d-d*x) / (0-a*h*y+b*g*y+d*h*x-g*i*x+a*i-b*d) ;
var u:Number = (c*h*y-f*h*x+b*f-b*y-c*i+i*x) / (0-a*h*y+b*g*y+d*h*x-g*i*x+a*i-b*d);
dotty= dot(x, y, 0x00FF00);
dotty2 = dot(x+50, y+50);
}
private function onLoop(e:Event):void {
dotty2.x = stage.mouseX;
dotty2.y = stage.mouseY;
var pos:Array = getTranslated(dotty2.x, dotty2.y);
dotty.x = pos[0] * stage.stageWidth;
dotty.y = pos[1] * stage.stageHeight;
}
private function getTranslated (x:Number, y:Number):Array {
s = getSystem(_pointList);
var a:Number = s[0];
var b:Number = s[1];
var c:Number = s[2];
var d:Number = s[3];
var i:Number = s[4];
var f:Number = s[5];
var g:Number = s[6];
var h:Number = s[7];
var v:Number = (0-c*g*y+f*g*x-a*f+a*y+c*d-d*x) / (0-a*h*y+b*g*y+d*h*x-g*i*x+a*i-b*d);
var u:Number = (c*h*y-f*h*x+b*f-b*y-c*i+i*x) / (0-a*h*y+b*g*y+d*h*x-g*i*x+a*i-b*d);
return [u,v];
}
private function dot(xp:Number, yp:Number, col:uint = 0xFF0000, rad:Number=4):Sprite {
var s:Sprite = Sprite(addChild(new Sprite));
s.x = xp;
s.y = yp;
with(s.graphics) beginFill(col), drawCircle(0,0,rad);
return s;
}
static private function getSystem( P:Array ):Array
{
var system:Array = new Array( 8 );
var sx:Number = (P[0].x-P[1].x)+(P[2].x-P[3].x);
var sy:Number = (P[0].y-P[1].y)+(P[2].y-P[3].y);
var dx1:Number = P[1].x-P[2].x;
var dx2:Number = P[3].x-P[2].x;
var dy1:Number = P[1].y-P[2].y;
var dy2:Number = P[3].y-P[2].y;
var z:Number = (dx1*dy2)-(dy1*dx2);
var g:Number = ((sx*dy2)-(sy*dx2))/z;
var h:Number = ((sy*dx1)-(sx*dy1))/z;
system[0]=P[1].x-P[0].x+g*P[1].x;
system[1]=P[3].x-P[0].x+h*P[3].x;
system[2]=P[0].x;
system[3]=P[1].y-P[0].y+g*P[1].y;
system[4]=P[3].y-P[0].y+h*P[3].y;
system[5]=P[0].y;
system[6]=g;
system[7]=h;
return system;
}
}
}
import flash.display.*;
import flash.utils.*;
import flash.geom.*;
class Rect implements Drawer {
private var _lineWeight:uint = 0;
private var _lineColor:uint = 0;
private var _pointList:Array = [];
public function Rect(lineWeight:uint=2, lineColor:uint = 0xFF0000 ) {
_lineWeight = lineWeight;
_lineColor = lineColor;
_pointList = [new Point (150,50), new Point (350,60), new Point (440,290), new Point (160,300)];
}
public function drawRect (canvas:Graphics, points:Array):void {
for( var i:int=0; i<points.length; ++i ) {
var p:Point = points[i];
canvas.lineTo( p.x, p.y);
}
}
public function draw( canvas:Graphics, _tpointList:Array ):void {
_pointList = _tpointList;
var fp:Point= _pointList[3] as Point;
canvas.lineStyle( _lineWeight, _lineColor);
canvas.moveTo( fp.x, fp.y);
drawRect (canvas, _pointList);
}
}
interface Drawer {
function draw( canvas:Graphics, _tpointList:Array ):void;
}