0
点赞
收藏
分享

微信扫一扫

15.5计算几何(点和线段的固定代码) ——【点和线段的位置关系】

一葉_code 2022-03-13 阅读 41

文章目录

题目描述

平面直角坐标系中有一个点C和一条线段AB,求点C和线段AB的位置关系

输入描述

输出描述

输入输出样例

输入:

2
0 1
1 0
1 1
0 0
2 2
1 1

输出:

No
Yes



最终代码c/c++

#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);  //高精度圆周率
const double eps = 1e-8;        //偏差值
int sgn(double x){  //判断x是否等于0
    if(fabs(x) < eps)  return 0;
    else return x<0?-1:1;
}
struct Point{
    double x, y;
    void input(){ scanf("%lf%lf", &x, &y); }
    Point(){}
    Point(double x,double y):x(x),y(y){}
    Point operator + (Point B){return Point(x+B.x,y+B.y);}
    Point operator - (Point B){return Point(x-B.x,y-B.y);}
};
typedef Point Vector;                    //定义向量
double Cross(Point A,Point B){return A.x*B.y - A.y*B.x;} //叉积
double Dot(Vector A,Vector B){return A.x*B.x + A.y*B.y;} //点积
struct Line{
    Point p1,p2;       //线上的两个点
    Line(){}
    Line(Point p1,Point p2):p1(p1),p2(p2){}
};
bool Point_on_seg(Point p, Line v){ //点和线段:0 点不在线段v上;1 点在线段v上
    return sgn(Cross(p-v.p1, v.p2-v.p1)) == 0 && sgn(Dot(p - v.p1, p- v.p2)) <= 0;
}

int main(){
    int t;     cin >> t;
    while(t--){
        Point a, b, c;
        a.input(); b.input(); c.input();
        Line v;
        v=Line(a,b);
        int pos=Point_on_seg(c, v);
        if(pos==0) cout<<"No"<<endl;
        if(pos==1)cout<<"Yes"<<endl;
    }
    return 0;
}



过程理解

举报

相关推荐

0 条评论