The judgment point is in a straight line and within a triangle.

Source: Internet
Author: User

When determining a point in a straight line, two conditions need to be met, such as determining whether the qpoint is in the online segment P1P2.

1 (Q-P1) x (P2-P1) = 0; // The cross multiplication is 0.

2: Q is in the rectangle with the diagonal vertex P1 and P2. // ensure that the vertex Q is not in the extension line or reverse extension line of the P1P2 line segment.

Points in the triangle:

If point P is in the triangle, spab + SPAC + spbc = SABC

The triangle area formula is given by the cross product S = 1/2 × | crossproduct (a, B, c) |;

This method has a floating point error.

The other is to judge whether point P is on the right side of each side along the clockwise direction of the side of the triangle. If yes, it is in the triangle.

This method has no floating point error.

Only floating point errors are given below.

#include <stdio.h>#include <math.h>#include <iostream>#include <algorithm>#define eps 1e-8using namespace std;typedef struct node{    double x,y;}point;typedef struct triangle{    point A;    point B;    point C;};double crossProduct(point p1,point p2,point p0)//(p1-p0)X(p2-p0){    double x1,x2,y1,y2;    x1=p1.x-p0.x;    y1=p1.y-p0.y;    x2=p2.x-p0.x;    y2=p2.y-p0.y;    return x1*y2-x2*y1;}bool inTriangle(triangle t,point P){    point A,B,C;    A=t.A;    B=t.B;    C=t.C;    double Sabc=fabs(crossProduct(A,B,C));printf("abc:%lf ",Sabc);    double Spab=fabs(crossProduct(P,A,B));printf("pab:%lf ",Spab);    double Spac=fabs(crossProduct(P,A,C));printf("pac:%lf ",Spac);    double Spbc=fabs(crossProduct(P,B,C));printf("pbc:%lf ",Spbc);    if(fabs(Sabc-(Spab+Spac+Spbc))<eps)        return true;    else        return false;}bool onSegment(point Pi,point Pj,point Q){    if((Q.x-Pi.x)*(Pj.y-Pi.y)==(Pj.x-Pi.x)*(Q.y-Pi.y)&&//x1*y2=x2*y1    min(Pi.x,Pj.x)<=Q.x&&Q.x<=max(Pi.x,Pj.x)&&    min(Pi.y,Pj.y)<=Q.y&&Q.y<=max(Pi.y,Pj.y))        return true;    else        return false;}int main(){    point p,q,r,s;    scanf("%lf%lf%lf%lf",&p.x,&p.y,&q.x,&q.y);//segment p--q    scanf("%lf%lf",&r.x,&r.y);//point r    scanf("%lf%lf",&s.x,&s.y);//point s    if(onSegment(p,q,r))    {        printf("YES\n");    }    else    {        printf("NO\n");    }    triangle t;    t.A=p;    t.B=q;    t.C=r;    if(inTriangle(t,s))    {        printf("YES\n");    }    else    {        printf("NO\n");    }    return 0;}
View code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.