如果点在三角形内(当点位于三角形的边界时有帮助)

时间:2011-05-31 12:42:58

标签: c geometry

以下代码测试一个点是否在一个三角形内部,它可以正确地完成所有操作,但每当我在三角形的边界上指定一个点时,它就会说它在外面(我希望它在里面)。任何人都可以弄清楚什么是错的? (我没有写下面的代码,所以请理解风格是可怕的忽略它...相信我在清理之前更糟糕了。)

如果我输入带顶点A(0,0)B(10,0)C(0,10)和点(5,0)的三角形,它仍将显示为三角形外部!

#include <stdio.h>

int test2( double px, double py, double m, double b ) {    
    if (py < m * px + b ) {
        return -1; // point is under line
    }else if ( py == m * px + b ){
        return 0; // point is on line
    } else {
        return 1; // point is over line
    }
}

int test1(double px, double py, double m,double b, double lx,double ly) {     
   return (test2(px,py, m,b) == test2(lx,ly,m,b));    
}

int tritest (double x0,double y0,double x1,double y1,double x2,double y2,double px, double py) {

   int line1, line2, line3;    
   double m01 = (y1-y0)/(x1-x0);    
   double b01 = m01 * -x1 + y1;    
   double m02, m12, b02, b12;    
   m02 = (y2-y0)/(x2-x0);    
   m12 = (y2-y1)/(x2-x1);    
   b02 = m02 * -x2 + y2;    
   b12 = m12 * -x2 + y2;

   // vertical line checks

   if( x1 == x0 ) {    
      line1 = ( (px <= x0) == (x2 <= x0) );    
   } else {    
      line1 = test1( px, py, m01, b01,x2,y2);    
   }

   if( x1 == x2 ) {    
      line2 = ( (px <= x2) == (x0 <= x2) );    
   } else {    
      line2 = test1(px,py, m12, b12,x0,y0);    
   }

   if( x2 == x0 ) {    
      line3 = ( (px <= x0 ) == (x1 <= x0) );} else {    
      line3 = test1(px, py, m02,b02,x1,y1);    
   }

   return line1 && line2 && line3;
}

int main(int argc, char* argv[]) {    
   double x0,y0,x1,y1,x2,y2,px;    
   double py;    
   int scanfsReturnValueAggregatedOverAllScanfs = 0;

   // get input

   printf("Triangle Vertex A (enter x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x0,&y0);    
   printf("\nTriangle Vertex B (enter x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x1,&y1);    
   printf("\nTriangle Vertex C (enter x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &x2,&y2);    
   printf("\nTest Point (enter x,y): "); scanfsReturnValueAggregatedOverAllScanfs += scanf("%lf,%lf", &px,&py);
   // print error

   if( scanfsReturnValueAggregatedOverAllScanfs != 8 ) {    
      printf("You're stup** and didn't put in the right inputs!\n");    
      return 1;    
   }

   // print answer

   printf("\nThe point is ");

   if (tritest(x0,y0,x1,y1,x2,y2,px,py)) {    
      printf("INSIDE");    
   } else {    
      printf("OUTSIDE");    
   }

   printf(" the Triangle\n");

   // return 0

   return 0;    
}

2 个答案:

答案 0 :(得分:3)

我立刻想到的一件事是你使用==来比较双打。这种比较从来都不准确,可能会产生令人惊讶的结果。最好做一些像fabs(d1-d2) < 1e-3这样的事情来比较双打的相等性。

答案 1 :(得分:0)

int test2( double px, double py, double m, double b ) {

if (py < m * px + b ) {
    return -1; 
}else if ( py == m * px + b ){
    return 0;  //Should be return 1 as point is on line.Thus inside the triangle
} else {
    return 1; //should be return 0 as point is outside this line
}
}

我认为您在上述方法中返回的返回值存在问题。对于具有不同斜率的行,这些条件会有所不同。这些检查仅对m> 0​​且m <1有效。并且您的代码运行良好因为侥幸。测试你的代码用于不同斜率的线。