题解 | 两直线交点

两直线交点

https://www.nowcoder.com/practice/b14130882e704fd6aa57f8762ae17bac

#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) {
    // TODO: 在这里输入你的代码,求直线 line_A 与 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 x0,y0; 
    struct point pointacorss;
    if((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)!=0){
      x0=((x1*y2-x2*y1)*(x3-x4)-(x3*y4-x4*y3)*(x1-x2))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
      y0=((x1*y2-x2*y1)*(y3-y4)-(x3*y4-x4*y3)*(y1-y2))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
      pointacorss.x=x0;
      pointacorss.y=y0;
    }
    else{
        pointacorss.x=-1;
        pointacorss.y=-1;
    }
    return pointacorss;
} 

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;
}

全部评论

相关推荐

Edgestr:没项目地址就干脆把那一栏删了呗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务