A master of mathematics-K. The relationship between point and triangle (cross product to find area)

Description Knowing the
coordinates of three points A, B, and C of a triangle in a plane coordinate system, judge whether another point D is in the triangle (a point on the side of the triangle is also considered to be in the triangle)

Input
has four lines with two numbers in each line. The first three lines represent the coordinates of A, B, and C, and the fourth line represents the coordinates of D.

Output
outputs a character string, "in" means point D is inside triangle ABC, and "out" means point D is outside triangle ABC.

Samples
Input 复制
1.0 2.0
7.0 1.0
7.0 5.0
5.0 3.0
Output
in

Idea:
Use the area relationship of triangles to judge the relationship between points and triangles.
Suppose the point is P, and the three vertices of the triangle are A, B, and C.
Calculate the areas of the triangles ABC, ABP, ACP, and BCP respectively:
if:
S(ABC)=S(ABP)+S(ACP)+S(BCP),
then the point is in the triangle or on the side of the triangle.
So the problem is transformed into finding the area of ​​the triangle by knowing the coordinates of the three points of the triangle.
There are several methods on Baidu on the Internet, all of which have accuracy errors:

double cul(node a,node b,node c){
    
    
    double x1=a.x,y1=a.y;
    double x2=b.x,y2=b.y;
    double x3=c.x,y3=c.y;
    return x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2;
}
double cul(node a,node b,node c)
{
    
    
    double x1=a.x,y1=a.y;
    double x2=b.x,y2=b.y;
    double x3=c.x,y3=c.y;
    double aa=sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
    double bb=sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) );
    double cc=sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) );
    double pp=(aa+bb+cc)/2;
    double S=sqrt( pp*(pp-aa)*(pp-bb)*(pp-cc));
    return S;
}

Then Zhang Ju said that if he could use the area of ​​the triangle, he would "kill me".
Zhang Ju's idea is to use the cross product to find the area of ​​a triangle.
According to high school knowledge, assuming that the two sides of the triangle are a, b, and the angle between the two sides is c, then the area of ​​the triangle is a ∗ b ∗ sinc ∗ 1 2 a*b*sinc*\frac{1}{2 }absinc21
The definition of cross product is the product of two vector modulus and then multiplied by the sine of the included angle, which can be converted into coordinate calculation.
Code:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    
    
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a,ll b,ll p)
{
    
    
    ll res=1;
    while(b)
    {
    
    
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
#define PI acos(-1)
const double eps=1e-10;
struct node
{
    
    
    double x,y;
    node operator - (node &s){
    
    
        return (node){
    
    x-s.x,y-s.y};
    }
};
double operator*(node a,node b){
    
    
    return a.x*b.y-b.x*a.y;
}
double cul(node a,node b,node c){
    
    
    return fabs((b-a)*(c-a)/2);
}
int main()
{
    
    

    node a,b,c,d;
    cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
    double abc=cul(a,b,c);
    double abd=cul(a,b,d);
    double acd=cul(a,c,d);
    double bcd=cul(b,c,d);
    if(abd+acd+bcd==abc) puts("in");
    else puts("out");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/114445589