首页 >

两直线交点

import java.util.*;

public class Main {
    static class Point {
        public double x, y;
        public Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
        public Point() {
            this(0.0, 0.0);
        }
    }

    static class Line {
        public Point point_A, point_B;
        public Line(Point a, Point b) {
            point_A = a;
            point_B = b;
        }
        public Line() {
            point_A = new Point();
            point_B = new Point();
        }
    }
    public static double[] getParam(Line line) {
        double[] nums = new double[3];
        double A1 = line.point_B.y - line.point_A.y;
        double B1 = line.point_A.x - line.point_B.x;
        double C1 = -B1 * line.point_A.y + line.point_A.x * (-A1);
        nums[0] = A1;
        nums[1] = B1;
        nums[2] = C1;
        return nums;
    }
    public static Point findMeetingPoint(Line lineA, Line lineB) {
        double[] lineab = getParam(lineA);
        double[] linecd = getParam(lineB);

        //判是否有重合或平行情况d = a1*b2-a2*b1 = 0
        if (lineab[0] * linecd[1] == linecd[0] * lineab[1]) {
            return new Point(-1.00,-1.00);
        }
        //x = (b1*c2-b2*c1)/(a1*b2-a2*b1)
        double x = (lineab[1] * linecd[2] - linecd[1] * lineab[2]) /
                   (lineab[0] * linecd[1] - linecd[0] * lineab[1]);
        //y = (a2*c1-a1*c2)/(a1*b2-a2*b1)
        double y = (linecd[0] * lineab[2] - lineab[0] * linecd[2]) /
                   (lineab[0] * linecd[1] - linecd[0] * lineab[1]);
        return new Point(x, y);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point A = new Point();
        A.x = scanner.nextDouble();
        A.y = scanner.nextDouble();
        Point B = new Point();
        B.x = scanner.nextDouble();
        B.y = scanner.nextDouble();
        Point C = new Point();
        C.x = scanner.nextDouble();
        C.y = scanner.nextDouble();
        Point D = new Point();
        D.x = scanner.nextDouble();
        D.y = scanner.nextDouble();
        Line AB = new Line(A, B);
        Line CD = new Line(C, D);
        Point ans = findMeetingPoint(AB, CD);
        System.out.println(ans.x + " " + ans.y);
        scanner.close();
    }
}

#include<bits/stdc++.h>
using namespace std;

struct point {
    double x, y;
    point(double A, double B) {
        x = A, y = B;
    }
    point() = default;
};

struct line {
    point point_A, point_B;
    line(point A, point B) {
        point_A = A, point_B = B;
    }
    line() = default;
};

point findMeetingPoint(line line_A, line line_B) {
    point p;
    double xa = line_A.point_A.x, ya = line_A.point_A.y;
    double xb = line_A.point_B.x, yb = line_A.point_B.y;
    double xc = line_B.point_A.x, yc = line_B.point_A.y;
    double xd = line_B.point_B.x, yd = line_B.point_B.y;
    if (fabs((yc - yd) * (xa - xb) - (xc - xd) * (ya - yb)) < 1e-10) {
        // 检查是否重合
        // 如果AB上的点A到直线CD的距离为0,且CD上的点C到直线AB的距离为0,则重合
        double distAtoCD = fabs((xd - xc) * (ya - yc) - (xa - xc) * (yd - yc));
        double distCtoAB = fabs((xb - xa) * (yc - ya) - (xc - xa) * (yb - ya));

        if (distAtoCD < 1e-10 && distCtoAB < 1e-10) {
            // 两直线重合,交点不唯一
            return point(-1, -1);
        } else {
            // 两直线平行但不重合,无交点
            // 题目要求:若两直线交点不唯一,则返回点(-1, -1)
            // 平行且不重合时,交点不存在,但根据题目要求返回(-1, -1)
            return point(-1, -1);
        }
    }
    double x0 = ((xc - xd) * (xb * ya - xa * yb) - (xa - xb) *
                 (xd * yc - xc * yd)) / (-(yc - yd) * (xa - xb) + (xc - xd) * (ya - yb));
    double y0 = ((xa * yb - ya * xb) * (yc - yd) - (ya - yb) *
                 (xc * yd - yc * xd)) / ((yc - yd) * (xa - xb) - (xc - xd) * (ya - yb));

    p.x = x0, p.y = y0;
    return p;
}
int main() {
    point A, B, C, D;
    cin >> A.x >> A.y >> B.x >> B.y >> C.x >> C.y >> D.x >> D.y;
    line AB = line(A, B);
    line CD = line(C, D);
    point ans = findMeetingPoint(AB, CD);
    cout << fixed << setprecision(6);
    cout << ans.x << " " << ans.y;
    return 0;
}
发表于 2025-08-21 15:50:46 回复(2)
point findMeetingPoint(line line_A, line line_B) {
    // TODO: 在这里输入你的代码,求直线 line_A 与 line_B 的交点   
    double A_k,A_b,B_k,B_b;
    if (line_A.point_A.x-line_A.point_B.x==0 && line_B.point_A.x-line_B.point_B.x==0) {
        point P(-1,-1);
        return P;//两直线都垂直于x轴,没有斜率,会有无数个交点或者没有交点
    }else if (line_A.point_A.x-line_A.point_B.x==0 && line_B.point_A.x-line_B.point_B.x!=0) {
        B_k=(line_B.point_A.y-line_B.point_B.y)/(line_B.point_A.x-line_B.point_B.x);
        B_b=line_B.point_A.y-B_k*line_B.point_A.x;
        double x,y;
        x=line_A.point_A.x;
        y=B_k*x+B_b;
        point P(x,y);
        return P;//直线A垂直于x轴
    }else if (line_A.point_A.x-line_A.point_B.x!=0 && line_B.point_A.x-line_B.point_B.x==0) {
        A_k=(line_A.point_A.y-line_A.point_B.y)/(line_A.point_A.x-line_A.point_B.x);
        A_b=line_A.point_A.y-A_k*line_A.point_A.x;
        double x,y;
        x=line_B.point_A.x;
        y=A_k*x+A_b;
        point P(x,y);
        return P;//直线B垂直于x轴
    }else {
        A_k=(line_A.point_A.y-line_A.point_B.y)/(line_A.point_A.x-line_A.point_B.x);
        A_b=line_A.point_A.y-A_k*line_A.point_A.x;
        B_k=(line_B.point_A.y-line_B.point_B.y)/(line_B.point_A.x-line_B.point_B.x);
        B_b=line_B.point_A.y-B_k*line_B.point_A.x;
        if (A_k==B_k) {
            point P(-1,-1);
            return P;//两直线平行,会有无数个交点或者没有交点
        }else {
            double A1=-A_k,A2=-B_k,B1=1,B2=1,C1=-A_b,C2=-B_b;
            double x,y;
            x=(B1*C2-B2*C1)/(B2*A1-B1*A2);
            y=A_k*x+A_b;
            point P(x,y);
            return P;//除了以上四种特殊情况
        }
    }   
}  
方法有点笨
发表于 2026-03-26 17:52:11 回复(0)
情况考虑完整
public
static Point findMeetingPoint(Line lineA, Line lineB) {
        // TODO: 求直线 lineA 与 lineB 的交点
        double x1=lineA.point_A.x,y1=lineA.point_A.y;
        double x2=lineA.point_B.x,y2=lineA.point_B.y;
        double x3=lineB.point_A.x,y3=lineB.point_A.y;
        double x4=lineB.point_B.x,y4=lineB.point_B.y;
        //一、判断是否垂线
            //1.两条垂线
        if(x2==x1&&x3==x4) return new Point(-1.0,-1.0);
       
            //2.一条垂线
        if(x2==x1&&x3!=x4){
            double k34=(y4-y3)/(x4-x3);
            double b34=y3-k34*x3;
            double y_t1=k34*x1+b34;
            return new Point(x1,y_t1);
        }
        if(x2!=x1&&x3==x4){
            double k12=(y2-y1)/(x2-x1);
            double b12=y1-k12*x1;
            double y_t2=k12*x3+b12;
            return new Point(x3,y_t2);
        }
        //二、没有垂线
        //计算两条直线斜率和截距(初中公式)
        double k12=(y2-y1)/(x2-x1);
        double k34=(y4-y3)/(x4-x3);
        double b12=y1-k12*x1;
        double b34=y3-k34*x3;
        //1.考虑平行和重合
        if(k12==k34) return new Point(-1.0,-1.0);
       
        //2.没有特殊情况:求交点(初中公式:y=k1x+b和y=k2x+b2连立起来)
        double x_final,y_final;
        x_final=(b34-b12)/(k12-k34);
        y_final=k12*x_final+b12;
        return new Point(x_final,y_final);
    }
发表于 2025-11-13 17:54:25 回复(0)
from decimal import Decimal, getcontext

getcontext().prec = 50


class Point:
    def __init__(self, x=0.000000, y=0.000000):
        self.x = Decimal(str(x))
        self.y = Decimal(str(y))


class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()


def find_meeting_point(line_A, line_B):
    # TODO: 求直线 line_A 与 line_B 的交点
    # 以下是4个点的坐标:A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4)
    x1, y1, x2, y2, x3, y3, x4, y4 = (
        line_A.point_A.x,
        line_A.point_A.y,
        line_A.point_B.x,
        line_A.point_B.y,
        line_B.point_A.x,
        line_B.point_A.y,
        line_B.point_B.x,
        line_B.point_B.y,
    )
    d = Decimal(str((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)))
    if d == 0:  # 下面else中的那个交点公式的分母不能为0
        return Point(-1, -1)
    else:
        # 这个是交点公式,用百度或AI就可以查到
        x0, y0 = (
            (x1 * y2 - y1 * y2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)
        ) / d, ((x1 * y2 - y1 * y2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d
        return Point(x0, y0)  # 返回交点结果


def main():
    data = list(map(float, input().split()))
    data.extend(list(map(float, input().split())))
    A = Point(data[0], data[1])
    B = Point(data[2], data[3])
    C = Point(data[4], data[5])
    D = Point(data[6], data[7])
    AB = Line(A, B)
    CD = Line(C, D)
    ans = find_meeting_point(AB, CD)
    print(f"{ans.x:.6f} {ans.y:.6f}")  # 这里要改动,结果保留小数点后6位,不然自测输入那里通过不了


if __name__ == "__main__":
    main()

发表于 2025-11-11 10:46:59 回复(0)
from decimal import Decimal, getcontext

getcontext().prec = 50

class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = Decimal(str(x))
        self.y = Decimal(str(y))

class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()

def find_meeting_point(line_A, line_B):
    x1, y1 = line_A.point_A.x, line_A.point_A.y
    x2, y2 = line_A.point_B.x, line_A.point_B.y
    x3, y3 = line_B.point_A.x, line_B.point_A.y
    x4, y4 = line_B.point_B.x, line_B.point_B.y

    d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)

    if d == 0:
        return Point(-1, -1)

    t1 = x1 * y2 - y1 * x2
    t2 = x3 * y4 - y3 * x4

    x0 = (t1 * (x3 - x4) - (x1 - x2) * t2) / d
    y0 = (t1 * (y3 - y4) - (y1 - y2) * t2) / d

    return Point(x0, y0)

def main():
    data = list(map(float, input().split()))
    data.extend(list(map(float, input().split())))

    A = Point(data[0], data[1])
    B = Point(data[2], data[3])
    C = Point(data[4], data[5])
    D = Point(data[6], data[7])

    AB = Line(A, B)
    CD = Line(C, D)

    ans = find_meeting_point(AB, CD)
    print(f"{ans.x:.6f} {ans.y:.6f}")

if __name__ == "__main__":
    main()
发表于 2026-03-20 17:10:36 回复(0)
#include <bits/stdc++.h>
using namespace std;

struct point {
    double x, y;
    point(double A, double B) {
        x = A, y = B;
    }
    point() = default;
};

struct line {
    point point_A, point_B;
    line(point A, point B) {
        point_A = A, point_B = B;
    }
    line() = default;
};

point findMeetingPoint(line line_A, line line_B) {
    double x1 = line_A.point_A.x, y1 = line_A.point_A.y;
    double x2 = line_A.point_B.x, y2 = line_A.point_B.y;
    double x3 = line_B.point_A.x, y3 = line_B.point_A.y;
    double x4 = line_B.point_B.x, y4 = line_B.point_B.y;

    // 计算两条直线的斜率参数
    double denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

    // 处理平行或重合的情况
    if (fabs(denominator) < 1e-12) {
        // 两条直线平行或重合,返回一个特殊点(这里返回原点)
        return point(-1, -1);
    }

    // 使用行列式公式计算交点坐标
    double numerator_x = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) *
                         (x3 * y4 - y3 * x4);
    double numerator_y = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) *
                         (x3 * y4 - y3 * x4);

    double x = numerator_x / denominator;
    double y = numerator_y / denominator;

    return point(x, y);
}


int main() {
    point A, B, C, D;
    cin >> A.x >> A.y >> B.x >> B.y >> C.x >> C.y >> D.x >> D.y;
    line AB = line(A, B);
    line CD = line(C, D);
    point ans = findMeetingPoint(AB, CD);
    cout << fixed << setprecision(12) << ans.x << " " << ans.y;
    return 0;
}

发表于 2026-02-12 19:37:27 回复(0)
import java.util.*;

public class Main {
    static class Point {
        public double x, y;
        public Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
        public Point() {
            this(0.0, 0.0);
        }
    }

    static class Line {
        public Point point_A, point_B;
        public Line(Point a, Point b) {
            point_A = a;
            point_B = b;
        }
        public Line() {
            point_A = new Point();
            point_B = new Point();
        }
    }
    public static double[] getParam(Line line) {
        double[] nums = new double[3];
        double A1 = line.point_B.y - line.point_A.y;
        double B1 = line.point_A.x - line.point_B.x;
        double C1 = -B1 * line.point_A.y + line.point_A.x * (-A1);
        nums[0] = A1;
        nums[1] = B1;
        nums[2] = C1;
        return nums;
    }
    public static Point findMeetingPoint(Line lineA, Line lineB) {
        double[] lineab = getParam(lineA);
        double[] linecd = getParam(lineB);

        //判是否有重合或平行情况d = a1*b2-a2*b1 = 0
        if (lineab[0] * linecd[1] == linecd[0] * lineab[1]) {
            return new Point(-1.00,-1.00);
        }
        //x = (b1*c2-b2*c1)/(a1*b2-a2*b1)
        double x = (lineab[1] * linecd[2] - linecd[1] * lineab[2]) /
                   (lineab[0] * linecd[1] - linecd[0] * lineab[1]);
        //y = (a2*c1-a1*c2)/(a1*b2-a2*b1)
        double y = (linecd[0] * lineab[2] - lineab[0] * linecd[2]) /
                   (lineab[0] * linecd[1] - linecd[0] * lineab[1]);
        return new Point(x, y);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point A = new Point();
        A.x = scanner.nextDouble();
        A.y = scanner.nextDouble();
        Point B = new Point();
        B.x = scanner.nextDouble();
        B.y = scanner.nextDouble();
        Point C = new Point();
        C.x = scanner.nextDouble();
        C.y = scanner.nextDouble();
        Point D = new Point();
        D.x = scanner.nextDouble();
        D.y = scanner.nextDouble();
        Line AB = new Line(A, B);
        Line CD = new Line(C, D);
        Point ans = findMeetingPoint(AB, CD);
        System.out.println(ans.x + " " + ans.y);
        scanner.close();
    }
}

发表于 2026-02-01 11:44:12 回复(0)
point findMeetingPoint(line line_A, line line_B) {
    // 直线参数方程: line_A = P + t*r, line_B = Q + u*s
    // P: line_A 起点, r: line_A 方向向量
    // Q: line_B 起点, s: line_B 方向向量

    // 计算方向向量
    double r_x = line_A.point_B.x - line_A.point_A.x;  // line_A 的 x 方向分量
    double r_y = line_A.point_B.y - line_A.point_A.y;  // line_A 的 y 方向分量
    double s_x = line_B.point_B.x - line_B.point_A.x;  // line_B 的 x 方向分量
    double s_y = line_B.point_B.y - line_B.point_A.y;  // line_B 的 y 方向分量

    // 计算分母 (两直线的叉积 r x s)
    // 如果分母为 0,说明两直线平行或重合,无唯一交点
    double denominator = r_x * s_y - r_y * s_x;

    if (denominator == 0) {
        return point(-1, -1); // 平行或重合,无交点
    }

    // 计算向量 Q - P (line_B 起点到 line_A 起点的向量)
    double diff_x = line_B.point_A.x - line_A.point_A.x;
    double diff_y = line_B.point_A.y - line_A.point_A.y;

    // 计算参数 t: t = ((Q - P) x s) / (r x s)
    // 交点 = P + t * r
    double t = (diff_x * s_y - diff_y * s_x) / denominator;

    return point(line_A.point_A.x + t * r_x,
                 line_A.point_A.y + t * r_y);
}
这个使用ai生成了一下,比较注意到就是平行是输出-1,-1
发表于 2026-01-28 15:20:07 回复(0)
class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()

def find_meeting_point(line_A, line_B):
    # TODO: 在这里输入你的代码,求直线 line_A 与 line_B 的交点
    #分类讨论,先求斜率,按斜率是否存在分类(看分母),然后求交点
    if line_A.point_A.x - line_A.point_B.x != 0 and  line_B.point_A.x - line_B.point_B.x != 0:
        k_1 = (line_A.point_A.y - line_A.point_B.y)/(line_A.point_A.x - line_A.point_B.x)
        k_2 = (line_B.point_A.y - line_B.point_B.y)/(line_B.point_A.x - line_B.point_B.x)
        b_1 = line_A.point_A.y - k_1*line_A.point_A.x
        b_2 = line_B.point_A.y - k_2*line_B.point_A.x

        if k_1 == k_2:
            return Point(-1, -1)
        else:
            x = (b_2-b_1)/(k_1-k_2)
            y = k_1*x + b_1
            return Point(x, y)
    elif line_A.point_A.x - line_A.point_B.x != 0 and  line_B.point_A.x - line_B.point_B.x == 0:
        k_1 = (line_A.point_A.y - line_A.point_B.y)/(line_A.point_A.x - line_A.point_B.x)
        b_1 = line_A.point_A.y - k_1*line_A.point_A.x
        return Point(line_B.point_A.x,k_1*line_B.point_A.x+b_1)

    elif line_A.point_A.x - line_A.point_B.x == 0 and  line_B.point_A.x - line_B.point_B.x != 0:
        k_2 = (line_B.point_A.y - line_B.point_B.y)/(line_B.point_A.x - line_B.point_B.x)
        b_2 = line_B.point_A.y - k_2*line_B.point_A.x
        return Point(line_A.point_A.x,k_2*line_A.point_A.x+b_2)
    else:
        return Point(-1, -1)


def main():
    data = list(map(float, input().split()))
    data.extend(list(map(float, input().split())))
    A = Point(data[0], data[1])
    B = Point(data[2], data[3])
    C = Point(data[4], data[5])
    D = Point(data[6], data[7])
    AB = Line(A, B)
    CD = Line(C, D)
    ans = find_meeting_point(AB, CD)
    print(f"{ans.x} {ans.y}")

if __name__ == "__main__":
    main()
发表于 2026-01-28 11:36:20 回复(0)
class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()

def find_meeting_point(line_A, line_B):
    x1, y1 = line_A.point_A.x, line_A.point_A.y
    x2, y2 = line_A.point_B.x, line_A.point_B.y
    x3, y3 = line_B.point_A.x, line_B.point_A.y
    x4, y4 = line_B.point_B.x, line_B.point_B.y

    # 计算分母d:(x1-x2)(y3-y4) - (y1-y2)(x3-x4)
    # d=0时,两直线平行/重合(交点不唯一)
    d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
    
    if abs(d) < 1e-9:
        return Point(-1.0, -1.0)
    
    # 计算两个直线的常数项:c1=x1y2-y1x2,c2=x3y4-y3x4
    c1 = x1 * y2 - y1 * x2
    c2 = x3 * y4 - y3 * x4
    
    # 计算交点横、纵坐标
    x0 = (c1 * (x3 - x4) - (x1 - x2) * c2) / d
    y0 = (c1 * (y3 - y4) - (y1 - y2) * c2) / d
    
    # 返回交点对象
    return Point(x0, y0)

def main():
    # 读取第一行输入:A(x1,y1)、B(x2,y2)
    data1 = list(map(float, input().split()))
    # 读取第二行输入:C(x3,y3)、D(x4,y4)
    data2 = list(map(float, input().split()))
    # 构造4个点对象
    A = Point(data1[0], data1[1])
    B = Point(data1[2], data1[3])
    C = Point(data2[0], data2[1])
    D = Point(data2[2], data2[3])
    # 构造两条直线对象
    AB = Line(A, B)
    CD = Line(C, D)
    # 求交点
    ans = find_meeting_point(AB, CD)
    # 格式化输出:保留6位小数,交点不唯一则输出-1 -1
    print("{0:.6f} {1:.6f}".format(ans.x, ans.y))

if __name__ == "__main__":
    main()

发表于 2026-01-27 23:38:48 回复(0)
class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()

def find_meeting_point(line_A, line_B):
    # TODO: 在这里输入你的代码,求直线 line_A 与 line_B 的交点
    x1,x2,x3,x4,y1,y2,y3,y4=line_A.point_A.x,line_A.point_B.x,line_B.point_A.x,line_B.point_B.x,line_A.point_A.y,line_A.point_B.y,line_B.point_A.y,line_B.point_B.y
    if x1==x2 and x3!=x4:
        k2=(y3-y4)/(x3-x4)
        b2=y3-k2*x3
        y0=k2*x1+b2
        return Point(x1,y0)
    elif x1!=x2 and x3==x4:
        k1=(y1-y2)/(x1-x2)
        b1=y2-k1*x2
        y0=k1*x3+b1
        return Point(x3,y0)
    elif x1==x2 and x3==x4:
        return Point(-1,-1)
    else:    
        k1=(y1-y2)/(x1-x2)
        b1=y1-k1*x1
        k2=(y3-y4)/(x3-x4)
        b2=y3-k2*x3
        if k1!=k2 :
            x0=(b2-b1)/(k1-k2)
            y0=k1*x0+b1
            return Point(x0,y0)
        else:
            return Point(-1, -1)

def main():
    data = list(map(float, input().split()))
    data.extend(list(map(float, input().split())))
    A = Point(data[0], data[1])
    B = Point(data[2], data[3])
    C = Point(data[4], data[5])
    D = Point(data[6], data[7])
    AB = Line(A, B)
    CD = Line(C, D)
    ans = find_meeting_point(AB, CD)
    print(f"{ans.x} {ans.y}")

if __name__ == "__main__":
    main()
发表于 2025-11-23 20:24:53 回复(0)
def find_meeting_point(line_A, line_B):
    # TODO: 求直线 line_A 与 line_B 的交点
    """使用向量法求直线交点"""
    # 提取坐标
    x1, y1 = line_A.point_A.x, line_A.point_A.y
    x2, y2 = line_A.point_B.x, line_A.point_B.y
    x3, y3 = line_B.point_A.x, line_B.point_A.y
    x4, y4 = line_B.point_B.x, line_B.point_B.y
    
    # 计算分母
    denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
    
    # 检查是否平行
    if abs(denominator) < 1e-10:
        return Point(-1, -1)  # 平行或重合
    
    # 计算交点坐标
    numerator_x = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)
    numerator_y = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)
    
    x = numerator_x / denominator
    y = numerator_y / denominator
    
    return Point(x, y)

发表于 2025-10-29 19:46:45 回复(0)
class Point:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

class Line:
    def __init__(self, point_a=None, point_b=None):
        self.point_A = point_a if point_a is not None else Point()
        self.point_B = point_b if point_b is not None else Point()

def find_meeting_point(line_A, line_B):
    # 求delt
    x1 = float(line_A.point_A.x)
    y1 = float(line_A.point_A.y)
    x2 = float(line_A.point_B.x)
    y2 = float(line_A.point_B.y)
    x3 = float(line_B.point_A.x)
    y3 = float(line_B.point_A.y)
    x4 = float(line_B.point_B.x)
    y4 = float(line_B.point_B.y)
    delt = (x1 - x2) * (y3 -y4) - (y1 - y2) * (x3 - x4)
    if delt == 0:
        return Point(-1,-1)
    else:
        x0 = ((x1 * y2 - y1 * x2)*(x3 - x4) - (x1 - x2)*(x3 * y4 - y3 * x4)) / delt
        y0 = ((x1 * y2 - y1 * x2)*(y3 -y4) - (y1 - y2)*(x3 * y4 - y3 * x4)) / delt
        return Point(x0,y0)

发表于 2025-07-22 10:10:16 回复(0)
import java.util.*;

public class Main {
    static class Point {
        public double x, y;
        public Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
        public Point() {
            this(0.0, 0.0);
        }
    }

    static class Line {
        public Point point_A, point_B;
        public Line(Point a, Point b) {
            point_A = a;
            point_B = b;
        }
        public Line() {
            point_A = new Point();
            point_B = new Point();
        }
    }

    public static Point findMeetingPoint(Line lineA, Line lineB) {
        // TODO: 求直线 lineA 与 lineB 的交点
        Point A = lineA.point_A,B = lineA.point_B,C = lineB.point_A,D = lineB.point_B;
        double x = 0,y = 0;
        double A1 = B.y - A.y,B1 = A.x - B.x,C1 = B.x * A.y - A.x * B.y;
        double A2 = D.y - C.y,B2 = C.x - D.x,C2 = D.x * C.y - C.x * D.y;
        double den = A1 * B2 - A2 * B1;
        if(den == 0){
            return new Point(-1, -1);
        }
        x = (B1 * C2 - B2 * C1) / den;
        y = (A2 * C1 - A1 * C2) / den;
       
        return new Point(x, y);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point A = new Point();
        A.x = scanner.nextDouble();
        A.y = scanner.nextDouble();
        Point B = new Point();
        B.x = scanner.nextDouble();
        B.y = scanner.nextDouble();
        Point C = new Point();
        C.x = scanner.nextDouble();
        C.y = scanner.nextDouble();
        Point D = new Point();
        D.x = scanner.nextDouble();
        D.y = scanner.nextDouble();
        Line AB = new Line(A, B);
        Line CD = new Line(C, D);
        Point ans = findMeetingPoint(AB, CD);
        System.out.println(ans.x + " " + ans.y);
        scanner.close();
    }
}
发表于 2025-07-10 11:29:48 回复(0)