求两直线的交点

  1. /**
  2.     * 求直线AB, CD的交点
  3.     * 如果平行, 返回null.
  4.     * 
  5.     * @param a : Point 
  6.     * @param b : Point 
  7.     * @param c : Point 
  8.     * @param d : Point 
  9.     * @return Point - 交点
  10.     * @usage <code>
  11.     * import com.seyself.math.GeomMath;
  12.     * var a = { x:0 , y:0 };
  13.     * var b = { x:200 , y:200 };
  14.     * var c = { x:100 , y:0 };
  15.     * var d = { x:50 , y:200 };
  16.     * trace( GeomMath.intersection( a, b, c, d ) ); // 输出 : (x=80, y=80)
  17.     * </code>
  18.     */
  19.     public static function intersection( a:Object, b:Object, c:Object, d:Object ):Point
  20.     {
  21.         var pos1 = (b.y-a.y)/(b.x-a.x);
  22.         var pos2 = (d.y-c.y)/(d.x-c.x);
  23.         var pi = Number.POSITIVE_INFINITY;
  24.         var ni = Number.NEGATIVE_INFINITY;
  25.         
  26.         if(pos1==pos2){
  27.             return null;
  28.         }
  29.         if( pos1 == ni || pos1 == pi ){
  30.             pos1 = b.y-a.y;
  31.         }
  32.         if( pos2 == ni || pos2 == pi ){
  33.             pos2 = d.y-c.y;
  34.         }
  35.         
  36.         var nx = ( ( a.x*pos1 ) - a.y - ( c.x*pos2 ) + c.y )/( pos1-pos2 );
  37.         var ny = pos1*( nx-a.x ) + a.y;
  38.         
  39.         return new Point( nx, ny );
  40.     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值