0
点赞
收藏
分享

微信扫一扫

15.4 计算几何(点和直线的固定代码) ——【点和直线的关系】

冶炼厂小练 2022-03-13 阅读 67

文章目录

题目描述

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

输入描述

输出描述

输入输出样例

输入:

3
0 1
1 0
1 1
0 0
1 1
2 2
0 0
0 1
1 0

输出:

L
IN
R

最终代码c/c++

#include<bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);  //高精度圆周率
const double eps = 1e-8;        //偏差值

//判断x是否等于0
int sgn(double x)
{  
    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);}
};

//叉积
double Cross(Point A,Point B){return A.x*B.y - A.y*B.x;}



struct Line
{
    Point p1,p2;       //线上的两个点
    Line(){}
    Line(Point p1,Point p2):p1(p1),p2(p2){}
};


int Point_line_relation(Point p, Line v)
{
            int c = sgn(Cross(p-v.p1,v.p2-v.p1));
            if(c < 0)return 1;       //1:p在v的左边
            if(c > 0)return 2;       //2:p在v的右边
            return 0;                  //0:p在v上
}
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_line_relation(c, v);
        if(pos==1) cout<<"L"<<endl;
         if(pos==2)cout<<"R"<<endl;
         if(pos==0)cout<<"IN"<<endl;
    }
    return 0;
}

过程理解

举报

相关推荐

0 条评论