各位老大~!就是因为API精度不够,我要自己写算法的呀~!谢谢…!如果知道请告知详细算法~!

解决方案 »

  1.   

    我这里有一些算法用来解决点是否在三角形里的,作为参考。-----------------
    方法1:过该点作一条射线,然后判断该射线与三条边的相交情况;
    方法2:分别连接P点和三个顶点A,B,C,然后判断矢量PA,PB,PC所夹角之和是否为360度,如果是360度则P在三角形内1. 
    #include <stdio.h>
    #include <math.h>struct TPoint
    {
        float x;
        float y;
    };float area(struct TPoint p1,struct TPoint p2,struct TPoint p3){
        return fabs((p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y));
    }float mul(struct TPoint p1,struct TPoint p2,struct TPoint p0){
        return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
    }int inside(struct TPoint tr[],struct TPoint p){
        int i;
        for (i=0;i<3;i++)
            if (mul(p,tr[i],tr[(i+1)%3])*mul(p,tr[(i+2)%3],tr[(i+1)%3])>0)
                return 0;
        return 1;
    }int inside2(struct TPoint tr[],struct TPoint p){
        if (fabs(area(tr[0],tr[1],tr[2])-
                 area(p,tr[1],tr[2])-
                 area(tr[0],p,tr[2])-
                 area(tr[0],tr[1],p))<1.0e-20)
            return 1;
        else
            return 0;
    }main(){
        struct TPoint tr[3]={{-1,1},{1,0},{3,0}},p={1,2};
        printf("algorithm 1:");
        if (inside(tr,p))
            printf("In\n");
        else
            printf("Out\n");
        printf("algorithm 2:");
        if (inside2(tr,p))
            printf("In\n");
        else
            printf("Out\n");
    }2
    就是判断点到三个顶点的夹角之和为2*PI
    // 返回1 表示在那,0表示在外
    int inside3(const struct TPoint tr[], struct TPoint p)
    {
        TPoint p0,p1,p2;
        p0.x = tr[0].x - p.x ;  p0.y = tr[0].y - p.y;
        p1.x = tr[1].x - p.x ;  p1.y = tr[1].y - p.y;
        p2.x = tr[2].x - p.x ;  p2.y = tr[2].y - p.y;    double arg1 = acos((p0.x*p1.x + p0.y*p1.y)/sqrt(p0.x*p0.x + p0.y*p0.y)/sqrt(p1.x*p1.x+p1.y*p1.y));
        double arg2 = acos((p0.x*p2.x + p0.y*p2.y)/sqrt(p0.x*p0.x + p0.y*p0.y)/sqrt(p2.x*p2.x+p2.y*p2.y));
        double arg3 = acos((p2.x*p1.x + p2.y*p1.y)/sqrt(p2.x*p2.x + p2.y*p2.y)/sqrt(p1.x*p1.x+p1.y*p1.y));    if( fabs(2*3.1415926-arg1-arg2-arg3)<0.0001 ) return 1;
        return 0;
    }