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();
}
} 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;//除了以上四种特殊情况
}
}
} 方法有点笨
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() #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;
} 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();
}
} 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);
} 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() 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)
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)