即根据3个点即width height 求 matrix 中的正反面,这个问题有两种解法,一个是观察法 还有一个是用向量的叉积来算。
/**
*点与点的关系有9种位置关系,两点相等返回0,其余的关系安 1-8,逆时针计算,比如第一象限为1
*p_target相对于p_orgin的关系
* -----------------------------------------------------------------------------
* ++ | -+ | -- | +- |(sX,sY)
* p1_0 p2_0 | p1_0 p2_0 | p1_0 p2_0 | p1_0 p2_0 |
* 1 7 | 1 3 | 1 7 | 1 3 |(pointPositionNo)
* 2 8 | 2 4 | 2 8 | 2 4 |
* 3 1 | 3 5 | 3 1 | 3 5 |
* 4 2 | 4 6 | 4 2 | 4 6 |
* 5 3 | 5 7 | 5 3 | 5 7 |
* 6 4 | 6 8 | 6 4 | 6 8 |
* 7 5 | 7 1 | 7 5 | 7 1 |
* 8 6 | 8 2 | 8 6 | 8 2 |
* | | | |
* 正 | 反 | 正 | 反 |(正反)
* | | | |
* r=q | r=PI-q | r=PI-q | r=q |p0_p1 于+X轴角度 q
* | | | |
* (1) | (2) | (3) | (4) |
* -----------------------------------------------------------------------------
*
* 相关尺寸的计算
* 运用到 DPI Matrix Point
* eg:
* 1.将物理尺寸转化为屏幕尺寸
* p0,p1,p2,p3(mmm),etc.根据 USER_SCREEN_DPI 和 MMM_PER_INCH 转化为 p0,p1,p2,p3(pix)
*
* 2.根据p0,p1,p2,p3,width(pix),height(pix)计算matrix
* a.p0 - p1 = (实际)width | p0 - p2 = (实际)height
* 计算|sX| |sY|
* b.根据p0,p1,p2,p3,countPositive2NegativeBy4Point计算图像的正反
* isPositive = countPositive2NegativeBy3Point(p0,p1,p2);
* c.p0 - p1 的角度
* radian = countRadianByP0AndP1(p0,p1);
* d.根据公式
* var a:Number = sX * Math.cos(radian);
* var b:Number = sX * Math.sin(radian);
* var c:Number = -isPositive*sY * Math.sin(radian);
* var d:Number = isPositive*sY * Math.cos(radian);
* var tx:Number = p0.x;
* var ty:Number = p0.y;
*
* 3.将屏幕尺寸转化为物理尺寸
*
*
*/
//返回p0和p1形成的角度(弧度制 -PI - PI)
public static function countRadianByP0AndP1(p0:Point,p1:Point):Number {
var dx:Number = p1.x - p0.x;
var dy:Number = p1.y - p0.y;
var radian:Number = Math.atan2(dy,dx);
return radian;
}
public static function cout3PointWHToMatrix(
p0:Point,
p1:Point,
p2:Point,
originPixWidth:Number,
originPixHeight:Number):Matrix {
var w:Number = Point.distance(p0,p1);
var h:Number = Point.distance(p0,p2);
var sX:Number = w/originPixWidth;
var sY:Number = h/originPixHeight;
var radian:Number = countRadianByP0AndP1(p0,p1);
var isPositive:int = countPositive2NegativeBy3Point(p0,p1,p2);
var a:Number = sX * Math.cos(radian);
var b:Number = sX * Math.sin(radian);
var c:Number = -isPositive*sY * Math.sin(radian);
var d:Number = isPositive*sY * Math.cos(radian);
var tx:Number = p0.x;
var ty:Number = p0.y;
return new Matrix(a,b,c,d,tx,ty);
}
//p0_p1 X p0_p3(叉积) > 0 则p1 在p0_p3向量的左侧,则图片为反的,同理 < 0, p1在 p0_p3 的右侧
//图片为正, =0 则p1在p0_p3上,图片为中间
public static function countPositive2NegativeBy3Point(p0:Point,p1:Point,p2:Point):int {
var p0_p1:Point = p1.subtract(p0);//计算向量p0_p1下同
var p0_p2:Point = p2.subtract(p0);
var p0_p3:Point = p0_p1.add(p0_p2);
var p0_p1Xp0_p3:Number = p0_p1.x*p0_p3.y - p0_p3.x*p0_p1.y;
return p0_p1Xp0_p3>0?1:(p0_p1Xp0_p3<0?-1:0);
}