日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#判斷點(diǎn)的位置(區(qū)域內(nèi)或外)

C#判斷點(diǎn)的位置方法一 

 
 
 
  1. public int isLeft(Point P0, Point P1,Point P2)  
  2.         {  
  3.             int abc= ((P1.X - P0.X) * (P2.Y - P0.Y) - (P2.X - P0.X) * (P1.Y - P0.Y));  
  4.             return abc;  
  5.                                                           
  6.         }  
  7.  
  8.         private bool PointInFences(Point pnt1, Point[] fencePnts)  
  9.         {  
  10.  
  11.             int wn = 0,j=0; //wn 計(jì)數(shù)器 j第二個(gè)點(diǎn)指針  
  12.             for (int i = 0; i <  fencePnts.Length; i++)  
  13.             {//開始循環(huán)  
  14.                      if (i == fencePnts.Length - 1)  
  15.                          j = 0;//如果 循環(huán)到最后一點(diǎn) 第二個(gè)指針指向第一點(diǎn)  
  16.                      else 
  17.                          j = j + 1; //如果不是 ,則找下一點(diǎn)  
  18.  
  19.  
  20.                 if (fencePnts[i].Y < = pnt1.Y) // 如果多邊形的點(diǎn) 小于等于 選定點(diǎn)的 Y 坐標(biāo)  
  21.                 {  
  22.                     if (fencePnts[j].Y > pnt1.Y) // 如果多邊形的下一點(diǎn) 大于于 選定點(diǎn)的 Y 坐標(biāo)  
  23.                     {  
  24.                         if (isLeft(fencePnts[i], fencePnts[j], pnt1) > 0)  
  25.                         {  
  26.                             wn++;  
  27.                         }  
  28.                     }  
  29.                 }  
  30.                 else 
  31.                 {  
  32.                     if (fencePnts[j].Y < = pnt1.Y)  
  33.                     {  
  34.                         if (isLeft(fencePnts[i], fencePnts[j], pnt1) <  0)  
  35.                         {  
  36.                             wn--;  
  37.                         }  
  38.                     }  
  39.                 }  
  40.             }  
  41.             if (wn == 0)  
  42.                 return false;  
  43.             else 
  44.                 return true;  
  45.        } 

C#判斷點(diǎn)的位置方法二——c#內(nèi)置函數(shù):

 
 
 
  1. GraphicsPath myGraphicsPath = new GraphicsPath();   
  2. Region myRegion=new Region();                
  3. myGraphicsPath.Reset();  
  4. Point inputponint = new Point(inputx, inputy);  
  5.  
  6. myGraphicsPath.AddPolygon(points);//points);  
  7.  
  8. myRegion.MakeEmpty();  
  9.  
  10. myRegion.Union(myGraphicsPath);     
  11. //返回判斷點(diǎn)是否在多邊形里  
  12. bool myPoint= myRegion.IsVisible(inputponint);  
  13. this.lblx.Text = myPoint.ToString(); 

圖形算法:

1,面積法。就是看所有邊和目標(biāo)點(diǎn)組成的三角形面積和是否等于總的多邊形面積,如果相等,則在內(nèi)部。反之在外部。這種方法計(jì)算量較大,用到的主要計(jì)算是查乘。

2,夾角和法。參見三樓,判斷所有邊和目標(biāo)點(diǎn)的夾角和是否為360度。計(jì)算量比上面這種方法稍微小點(diǎn),用到主要是點(diǎn)乘和求模計(jì)算。

3,引射線法。就是從該點(diǎn)出發(fā)引一條射線,看這條射線和所有邊的交點(diǎn)數(shù)目。如果有奇數(shù)個(gè)交點(diǎn),則說明在內(nèi)部,如果有偶數(shù)個(gè)交點(diǎn),則說明在外部。這是所有方法中計(jì)算量最小的方法,在光線追蹤算法中有大量的應(yīng)用。

在C#中的話,有一個(gè)Region類,可以直接調(diào)用IsVisible判斷是否在這個(gè)區(qū)域內(nèi)部,我估計(jì)內(nèi)部的實(shí)現(xiàn)應(yīng)該是上面說的第三種方法。主要看你的需求是哪種輸入了,如果在C#中,你完全可以用Region類來隱藏內(nèi)部實(shí)現(xiàn)。

C#判斷點(diǎn)的位置的另外一種解決方法:

1.已知點(diǎn)point(x,y)和多邊形Polygon(x1,y1;x2,y2;….xn,yn;);

2.以point為起點(diǎn),以無窮遠(yuǎn)為終點(diǎn)作平行于X軸的直線line(x,y; -∞,y);

3.循環(huán)取得(for(i=0;i< n;i++))多邊形的每一條邊side(xi,yi;xi+1,yi+1),且判斷是否平行于X軸,如果平行continue,否則,i++;

4. 同時(shí)判斷point(x,y)是否在side上,如果是,則返回1(點(diǎn)在多邊形

上),否則繼續(xù)下面的判斷;

5.判斷線side與line是否有交點(diǎn),如果有則count++,否則,i++。

6.判斷交點(diǎn)的總數(shù),如果為奇數(shù)則返回0(點(diǎn)在多邊形內(nèi)),偶數(shù)則返回2(點(diǎn)在多邊形外)。

代碼:

 
 
 
  1. /* 射線法判斷點(diǎn)q與多邊形polygon的位置關(guān)系,要求polygon為簡(jiǎn)單多邊形,頂點(diǎn)逆時(shí)針排列  
  2.  
  3. 如果點(diǎn)在多邊形內(nèi): 返回0  
  4.  
  5. 如果點(diǎn)在多邊形邊上: 返回1  
  6.  
  7. 如果點(diǎn)在多邊形外: 返回2  
  8.  
  9. */ 
  10.  
  11. const double INFINITY = 1e10;  
  12.  
  13. const double ESP = 1e-5;  
  14.  
  15. const int MAX_N = 1000;  
  16.  
  17.  
  18. struct Point {  
  19.  
  20. double x, y;  
  21.  
  22. };  
  23.  
  24. struct LineSegment {  
  25.  
  26. Point pt1, pt2;  
  27.  
  28. };  
  29.  
  30. typedef vector< Point> Polygon;  
  31.  
  32.  
  33. // 計(jì)算叉乘 |P0P1| × |P0P2|  
  34.  
  35. double Multiply(Point p1, Point p2, Point p0)  
  36.  
  37. {  
  38.  
  39. return ( (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y) );  
  40.  
  41. }  
  42.  
  43. // 判斷線段是否包含點(diǎn)point  
  44.  
  45. bool IsOnline(Point point, LineSegment line)  
  46.  
  47. {  
  48.  
  49. return( ( fabs(Multiply(line.pt1, line.pt2, point)) <  ESP ) &&  
  50.  
  51. ( ( point.x - line.pt1.x ) * ( point.x - line.pt2.x ) < = 0 ) &&  
  52.  
  53. ( ( point.y - line.pt1.y ) * ( point.y - line.pt2.y ) < = 0 ) );  
  54.  
  55. }  
  56.  
  57. // 判斷線段相交  
  58.  
  59. bool Intersect(LineSegment L1, LineSegment L2)  
  60.  
  61. {  
  62.  
  63. return( (max(L1.pt1.x, L1.pt2.x) >= min(L2.pt1.x, L2.pt2.x)) &&  
  64.  
  65. (max(L2.pt1.x, L2.pt2.x) >= min(L1.pt1.x, L1.pt2.x)) &&  
  66.  
  67. (max(L1.pt1.y, L1.pt2.y) >= min(L2.pt1.y, L2.pt2.y)) &&  
  68.  
  69. (max(L2.pt1.y, L2.pt2.y) >= min(L1.pt1.y, L1.pt2.y)) &&  
  70.  
  71. (Multiply(L2.pt1, L1.pt2, L1.pt1) * Multiply(L1.pt2, L2.pt2, L1.pt1) >= 0) &&  
  72.  
  73. (Multiply(L1.pt1, L2.pt2, L2.pt1) * Multiply(L2.pt2, L1.pt2, L2.pt1) >= 0)  
  74.  
  75. );  
  76.  
  77. }  
  78.  
  79. // 判斷點(diǎn)在多邊形內(nèi)  
  80.  
  81. bool InPolygon(const Polygon& polygon, Point point)  
  82.  
  83. {  
  84.  
  85. int n = polygon.size();  
  86.  
  87. int count = 0;  
  88.  
  89. LineSegment line;  
  90.  
  91. line.pt1 = point;  
  92.  
  93. line.pt2.y = point.y;  
  94.  
  95. line.pt2.x = - INFINITY;  
  96.  
  97.  
  98. for( int i = 0; i <  n; i++ ) {  
  99.  
  100. // 得到多邊形的一條邊  
  101.  
  102. LineSegment side;  
  103.  
  104. side.pt1 = polygon[i];  
  105.  
  106. side.pt2 = polygon[(i + 1) % n];  
  107.  
  108.  
  109. if( IsOnline(point, side) ) {  
  110.  
  111. return1 ;  
  112.  
  113. }  
  114.  
  115.  
  116. // 如果side平行x軸則不作考慮  
  117.  
  118. if( fabs(side.pt1.y - side.pt2.y) <  ESP ) {  
  119.  
  120. continue;  
  121.  
  122. }  
  123.  
  124.  
  125. if( IsOnline(side.pt1, line) ) {  
  126.  
  127. if( side.pt1.y > side.pt2.y ) count++;  
  128.  
  129. } else if( IsOnline(side.pt2, line) ) {  
  130.  
  131. if( side.pt2.y > side.pt1.y ) count++;  
  132.  
  133. } else if( Intersect(line, side) ) {  
  134.  
  135. count++;  
  136.  
  137. }  
  138.  
  139. }  
  140.  
  141.  
  142. if ( count % 2 == 1 ) {return 0;}  
  143.  
  144. else { return 2;}  
  145.  
  146. }  
  147.  

文章題目:C#判斷點(diǎn)的位置(區(qū)域內(nèi)或外)
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/djdipds.html