求线段的交点

var p1: Point = new Point(0, 0);
var p2: Point = new Point(50, 50);
var p3: Point = new Point(0, 50);
var p4: Point = new Point(50, 0);

var p: Point = new Point;
if (p1.x == p2.x) {
    if (p3.x == p4.x) {
        //平行线
        p = null;
    } else {
        p.x = p1.x;
        p.y = p3.y + (p1.x - p3.x) / (p4.x - p3.x) * (p4.y - p3.y);
    }
} else if (p3.x == p4.x) {
    p.x = p3.x;
    p.y = p1.y + (p3.x - p1.x) / (p2.x - p1.x) * (p2.y - p1.y);
} else {
    var K1: Number = (p1.y - p2.y) / (p1.x - p2.x);
    var K2: Number = (p3.y - p4.y) / (p3.x - p4.x);
    if (K1 == K2) {
        //平行线
        p = null;
    } else {
        var B1: Number = (p1.x * p2.y - p1.y * p2.x) / (p1.x - p2.x);
        var B2: Number = (p3.x * p4.y - p3.y * p4.x) / (p3.x - p4.x);
        p.x = (B2 - B1) / (K1 - K2);
        p.y = K1 * p.x + B1;
    }
}
trace(p.x, p.y); //output: 25,25

  

posted on 2021-09-11 23:10  疯子_wu  阅读(47)  评论(0编辑  收藏  举报