点线计算

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct Point{
    double x,y;
    Point(){};
    Point(double a,double b):x(a),y(b){}
    friend Point operator + (Point a,Point b){
        return Point(a.x+b.x,a.y+b.y);
    }
    friend Point operator - (Point a,Point b){
        return Point(a.x-b.x,a.y-b.y);
    }
    friend Point operator * (Point a,double b){
        return Point(a.x*b,a.y*b);
    }
    friend Point operator / (Point a,double b){
        return Point(a.x/b,a.y/b);
    }
    void input(){
        scanf("%lf%lf",&x,&y);
    }
};
struct line{
    Point a,b;
    line(Point x,Point y):a(x),b(y){}
};
// 点积
double dot(Point a,Point b){
    return a.x*b.x+a.y*b.y;
}
// 叉积
double det(Point a,Point b){
    return a.x*b.y-a.y*b.x;
}
// 判断符号
int aps(double x){
    double eps = 1e-8;
    if (fabs(x)<=eps) return 0;
    if (x>0) return 1;
    return -1;
}
// 有交点
bool isCom(line a,line b){
    return (bool)aps(det(a.a-a.b,b.a-b.b));
}
// 同线
bool isOne(line a,line b){
    if (!aps(det(a.b-a.a,b.a-a.a)) && !(aps(det(a.b-a.a,b.b-a.a)))) return 1;
    return 0;
}
// 求交点
Point crossPoint(line a,line b){
    double s1=det(a.a-b.a,b.b-b.a);
    double s2=det(a.b-b.a,b.b-b.a);
    return (a.a*s2-a.b*s1)/(s2-s1);
}

调用

int main(){
    int n;
    Point c[8];
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    while (n--){
        for (int i(1);i<=4;i++){
            c[i].input();
            //printf(" %.2lf %.2lf\n",c[i].x,c[i].y);
        }
        line a = line(c[1],c[2]);
        line b = line(c[3],c[4]);
        if (isOne(a,b)) printf("LINE\n");
            else if (!isCom(a,b)) printf("NONE\n");
                else {
                    Point t=crossPoint(a,b);
                    printf("POINT %.2f %.2f\n",t.x,t.y);    // PS:在G++里,double以%lf读入以%f输出
                }
    }
    printf("END OF OUTPUT\n");
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值