- /**
- * 求直线AB, CD的交点
- * 如果平行, 返回null.
- *
- * @param a : Point
- * @param b : Point
- * @param c : Point
- * @param d : Point
- * @return Point - 交点
- * @usage <code>
- * import com.seyself.math.GeomMath;
- * var a = { x:0 , y:0 };
- * var b = { x:200 , y:200 };
- * var c = { x:100 , y:0 };
- * var d = { x:50 , y:200 };
- * trace( GeomMath.intersection( a, b, c, d ) ); // 输出 : (x=80, y=80)
- * </code>
- */
- public static function intersection( a:Object, b:Object, c:Object, d:Object ):Point
- {
- var pos1 = (b.y-a.y)/(b.x-a.x);
- var pos2 = (d.y-c.y)/(d.x-c.x);
- var pi = Number.POSITIVE_INFINITY;
- var ni = Number.NEGATIVE_INFINITY;
- if(pos1==pos2){
- return null;
- }
- if( pos1 == ni || pos1 == pi ){
- pos1 = b.y-a.y;
- }
- if( pos2 == ni || pos2 == pi ){
- pos2 = d.y-c.y;
- }
- var nx = ( ( a.x*pos1 ) - a.y - ( c.x*pos2 ) + c.y )/( pos1-pos2 );
- var ny = pos1*( nx-a.x ) + a.y;
- return new Point( nx, ny );
- }
求两直线的交点
最新推荐文章于 2023-08-12 17:15:57 发布