SHARE
TWEET

Untitled

a guest Jul 17th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. namespace gist.yifeitao.com
  4. {
  5.     /// <summary>
  6.     /// 二维坐标
  7.     /// </summary>
  8.     public struct Ponit2D
  9.     {
  10.         public double X;
  11.         public double Y;
  12.  
  13.         public Ponit2D(double x, double y)
  14.         {
  15.             X = x;
  16.             Y = y;
  17.         }
  18.  
  19.         public static Ponit2D operator -(Ponit2D left, Ponit2D right)
  20.         {
  21.             return new Ponit2D(left.X - right.X, left.Y - right.Y);
  22.         }
  23.  
  24.         public override string ToString()
  25.         {
  26.             return X + "," + Y;
  27.         }
  28.         /// <summary>
  29.         /// 二维向量叉乘
  30.         /// </summary>
  31.         /// <param name="left">(x1,y1,0)</param>
  32.         /// <param name="right">(x2,y2,0)</param>
  33.         /// <returns>(0,0,z)</returns>
  34.         public static double Cross(Ponit2D left, Ponit2D right)
  35.         {
  36.             return left.X*right.Y - left.Y*right.X;
  37.         }
  38.         /// <summary>
  39.         /// 测试点S、T是否在线段AB的同侧
  40.         /// </summary>
  41.         /// <param name="pA"></param>
  42.         /// <param name="pB"></param>
  43.         /// <param name="pT"></param>
  44.         /// <param name="pS"></param>
  45.         /// <returns></returns>
  46.         public static bool SameSideTest(Ponit2D pA, Ponit2D pB, Ponit2D pT, Ponit2D pS)
  47.         {
  48.             // ReSharper disable once InconsistentNaming
  49.             Ponit2D vectorAB = pB - pA;
  50.             // ReSharper disable once InconsistentNaming
  51.             Ponit2D vectorAP = pT - pA;
  52.             // ReSharper disable once InconsistentNaming
  53.             Ponit2D vectorAQ = pS - pA;
  54.             //判断条件,两个叉乘的符号相同,此处判定较为严格,靠近线段时认定为方向相同
  55.             double r = Cross(vectorAB, vectorAP) * Cross(vectorAB, vectorAQ);
  56.             return r >= 0;
  57.         }
  58.         /// <summary>
  59.         /// 测试点T是否在三角形ABC内部
  60.         /// </summary>
  61.         /// <param name="pA"></param>
  62.         /// <param name="pB"></param>
  63.         /// <param name="pC"></param>
  64.         /// <param name="pT"></param>
  65.         /// <returns></returns>
  66.         public static bool TriangleTest(Ponit2D pA, Ponit2D pB, Ponit2D pC, Ponit2D pT)
  67.         {
  68.             bool r = SameSideTest(pA, pB, pC, pT)
  69.                 && SameSideTest(pB, pC, pA, pT)
  70.                 && SameSideTest(pC, pA, pB, pT);
  71.             return r;
  72.         }
  73.         /// <summary>
  74.         /// 测试点T是否在凸四边形ABCD内部
  75.         /// </summary>
  76.         /// <param name="pA"></param>
  77.         /// <param name="pB"></param>
  78.         /// <param name="pC"></param>
  79.         /// <param name="pD"></param>
  80.         /// <param name="pT"></param>
  81.         /// <returns></returns>
  82.         public static bool QuadrilateralTest(Ponit2D pA, Ponit2D pB, Ponit2D pC, Ponit2D pD, Ponit2D pT)
  83.         {
  84.             //求重心
  85.             Ponit2D pV = new Ponit2D((pA.X + pB.X + pC.X + pD.X) / 4, (pA.Y + pB.Y + pC.Y + pD.Y) / 4);
  86.             //分割为四个三角形测试
  87.             return TriangleTest(pA, pB, pV, pT)
  88.                    || TriangleTest(pB, pC, pV, pT)
  89.                    || TriangleTest(pC, pD, pV, pT)
  90.                    || TriangleTest(pD, pA, pV, pT);
  91.         }
  92.         /// <summary>
  93.         /// 测试四边形ABCD是否为凸
  94.         /// </summary>
  95.         /// <param name="pA"></param>
  96.         /// <param name="pB"></param>
  97.         /// <param name="pC"></param>
  98.         /// <param name="pD"></param>
  99.         /// <returns></returns>
  100.         public static bool BulgeTest(Ponit2D pA, Ponit2D pB, Ponit2D pC, Ponit2D pD)
  101.         {
  102.             return !TriangleTest(pA, pB, pC, pD)
  103.                    && !TriangleTest(pD, pB, pC, pA)
  104.                    && !TriangleTest(pA, pD, pC, pB)
  105.                    && !TriangleTest(pA, pB, pD, pC);
  106.         }
  107.     }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top