C# 计算游戏技能攻击区域

尊重原著成果: http://blog.csdn.net/u010019717/article/details/78313392

1、判断  一个点是否在  与坐标平行的矩形内




  1. public struct AAB2 {  
  2.     public Vector2 Min;  
  3.     public Vector2 Max;  
  4.   
  5.   
  6.     public static AAB2 CreateAAB2(Transform point0, Transform point1)  
  7.     {  
  8.         // Creates aab from two unsorted points, if you know min and max use constructor  
  9.         return CreateFromTwoPoints(point0.position, point1.position);  
  10.     }  
  11.   
  12.     public static AAB2 CreateFromTwoPoints(Vector2 point0, Vector2 point1)  
  13.     {  
  14.         AAB2 aab;  
  15.         if (point0.x < point1.x)  
  16.         {  
  17.             aab.Min.x = point0.x;  
  18.             aab.Max.x = point1.x;  
  19.         }  
  20.         else  
  21.         {  
  22.             aab.Min.x = point1.x;  
  23.             aab.Max.x = point0.x;  
  24.         }  
  25.         if (point0.y < point1.y)  
  26.         {  
  27.             aab.Min.y = point0.y;  
  28.             aab.Max.y = point1.y;  
  29.             return aab;  
  30.         }  
  31.         aab.Min.y = point1.y;  
  32.         aab.Max.y = point0.y;  
  33.         return aab;  
  34.     }  
  35.   
  36.     public bool Contains(ref Vector2 point)  
  37.     {  
  38.         if (point.x < this.Min.x)  
  39.         {  
  40.             return false;  
  41.         }  
  42.         if (point.x > this.Max.x)  
  43.         {  
  44.             return false;  
  45.         }  
  46.         if (point.y < this.Min.y)  
  47.         {  
  48.             return false;  
  49.         }  
  50.         if (point.y > this.Max.y)  
  51.         {  
  52.             return false;  
  53.         }  
  54.         return true;  
  55.     }  
  56.   
  57.     public void CalcVertices(out Vector2 vertex0, out Vector2 vertex1, out Vector2 vertex2, out Vector2 vertex3)  
  58.     {  
  59.         vertex0 = this.Min;  
  60.         vertex1 = new Vector2(this.Max.x, this.Min.y);  
  61.         vertex2 = this.Max;  
  62.         vertex3 = new Vector2(this.Min.x, this.Max.y);  
  63.     }  




  1. [ExecuteInEditMode]  
  2. public class Test_ContPoint2AAB2 : MonoBehaviour  
  3. {  
  4.     public Transform Point;  
  5.     public Transform Box_Point0, Box_Point1;  
  6.   
  7.     private const float _pointRadius = .11f;  
  8.   
  9.     private void OnDrawGizmos()  
  10.     {  
  11.         Vector2 point = Point.position;  
  12.         AAB2 box = AAB2.CreateAAB2(Box_Point0, Box_Point1);  
  13.   
  14.         bool cont = box.Contains(ref point);  
  15.   
  16.         FiguresColor();  
  17.         DrawAAB(ref box);  
  18.         if (cont) ResultsColor();  
  19.             DrawPoint(point);  
  20.   
  21.         LogInfo(cont);  
  22.     }  
  23.     protected void DrawPoint(Vector2 position)  
  24.     {  
  25.         Gizmos.DrawSphere(position, _pointRadius);  
  26.     }  
  27.     protected void ResultsColor()  
  28.     {  
  29.         Gizmos.color = Color.blue;  
  30.     }  
  31.   
  32.     protected void FiguresColor()  
  33.     {  
  34.         Gizmos.color = Color.gray;  
  35.     }  
  36.     protected void LogInfo(object value)  
  37.     {  
  38.         Debug.Log(value);  
  39.     }  
  40.   
  41.     protected void DrawAAB(ref AAB2 box)  
  42.     {  
  43.         Vector2 v0, v1, v2, v3;  
  44.         box.CalcVertices(out v0, out v1, out v2, out v3);  
  45.         Gizmos.DrawLine(v0, v1);  
  46.         Gizmos.DrawLine(v1, v2);  
  47.         Gizmos.DrawLine(v2, v3);  
  48.         Gizmos.DrawLine(v3, v0);  
  49.     }  
  50. }  



2 判断是否在矩形内




矩形就是 Box 对象决定, 自己是原点, 旋转决定了两个轴  Scale决定了 宽高!


  1. [ExecuteInEditMode]  
  2. public class Test_ContPoint2Box2 : MonoBehaviour  
  3. {  
  4.     public Transform Point;  
  5.     public Transform Box;  
  6.   
  7.     private const float _pointRadius = .11f;  
  8.     private void OnDrawGizmos()  
  9.     {  
  10.         Vector2 point = Point.position;  
  11.         Box2 box = Box2.CreateBox2(Box);  
  12.   
  13.         bool cont = box.Contains(ref point);  
  14.   
  15.         FiguresColor();  
  16.         DrawBox(ref box);  
  17.         if (cont) ResultsColor();  
  18.         DrawPoint(point);  
  19.   
  20.         LogInfo(cont);  
  21.     }  
  22.   
  23.     protected void DrawPoint(Vector2 position)  
  24.     {  
  25.         Gizmos.DrawSphere(position, _pointRadius);  
  26.     }  
  27.     protected void ResultsColor()  
  28.     {  
  29.         Gizmos.color = Color.blue;  
  30.     }  
  31.   
  32.     protected void FiguresColor()  
  33.     {  
  34.         Gizmos.color = Color.gray;  
  35.     }  
  36.     protected void LogInfo(object value)  
  37.     {  
  38.         Debug.Log(value);  
  39.     }  
  40.   
  41.     protected void DrawBox(ref Box2 box)  
  42.     {  
  43.         Vector2 v0, v1, v2, v3;  
  44.         box.CalcVertices(out v0, out v1, out v2, out v3);  
  45.         Gizmos.DrawLine(v0, v1);  
  46.         Gizmos.DrawLine(v1, v2);  
  47.         Gizmos.DrawLine(v2, v3);  
  48.         Gizmos.DrawLine(v3, v0);  
  49.     }  
  50. }  



  1. public struct Box2 {  
  2.     public Vector2 Center;  
  3.     public Vector2 Axis0;  
  4.     public Vector2 Axis1;  
  5.     public Vector2 Extents;  
  6.   
  7.     public Box2(Vector2 center, Vector2 axis0, Vector2 axis1, Vector2 extents)  
  8.     {  
  9.         this.Center = center;  
  10.         this.Axis0 = axis0;  
  11.         this.Axis1 = axis1;  
  12.         this.Extents = extents;  
  13.     }  
  14.   
  15.     public static Box2 CreateBox2(Transform box)  
  16.     {  
  17.         return new Box2(box.position, box.right, box.up, box.localScale);  
  18.     }  
  19.   
  20.     public bool Contains(ref Vector2 point)  
  21.     {  
  22.         Vector2 vector;  
  23.         vector.x = point.x - this.Center.x;  
  24.         vector.y = point.y - this.Center.y;  
  25.         float num = vector.Dot(ref this.Axis0);  
  26.         if (num < -this.Extents.x)  
  27.         {  
  28.             return false;  
  29.         }  
  30.         if (num > this.Extents.x)  
  31.         {  
  32.             return false;  
  33.         }  
  34.         num = vector.Dot(ref this.Axis1);  
  35.         if (num < -this.Extents.y)  
  36.         {  
  37.             return false;  
  38.         }  
  39.         if (num > this.Extents.y)  
  40.         {  
  41.             return false;  
  42.         }  
  43.         return true;  
  44.     }  
  45.   
  46.     public void CalcVertices(out Vector2 vertex0, out Vector2 vertex1, out Vector2 vertex2, out Vector2 vertex3)  
  47.     {  
  48.         Vector2 vector = (Vector2)(this.Axis0 * this.Extents.x);  
  49.         Vector2 vector2 = (Vector2)(this.Axis1 * this.Extents.y);  
  50.         vertex0 = (this.Center - vector) - vector2;  
  51.         vertex1 = (this.Center + vector) - vector2;  
  52.         vertex2 = (this.Center + vector) + vector2;  
  53.         vertex3 = (this.Center - vector) + vector2;  
  54.     }  
  55.   
  56. }  
  57.   
  58. public static class Vector2ex  
  59. {  
  60.     public static float Dot(this Vector2 vector, ref Vector2 value)  
  61.     {  
  62.         return ((vector.x * value.x) + (vector.y * value.y));  
  63.     }  
  64. }  


3  判断一个点是否在园内部




  1. [ExecuteInEditMode]  
  2. public class Test_ContPoint2Circle2 : MonoBehaviour  
  3. {  
  4.     public Transform Point;  
  5.     public Transform Circle;  
  6.   
  7.     private const float _pointRadius = .11f;  
  8.   
  9.     private void OnDrawGizmos()  
  10.     {  
  11.         Vector2 point = Point.position;  
  12.         Circle2 circle = Circle2.CreateCircle2(Circle);  
  13.   
  14.         bool cont = circle.Contains(ref point);  
  15.   
  16.         FiguresColor();  
  17.         DrawCircle(ref circle);  
  18.         if (cont) ResultsColor();  
  19.         DrawPoint(point);  
  20.   
  21.         LogInfo(cont);  
  22.     }  
  23.   
  24.     protected void DrawPoint(Vector2 position)  
  25.     {  
  26.         Gizmos.DrawSphere(position, _pointRadius);  
  27.     }  
  28.     protected void ResultsColor()  
  29.     {  
  30.         Gizmos.color = Color.blue;  
  31.     }  
  32.   
  33.     protected void FiguresColor()  
  34.     {  
  35.         Gizmos.color = Color.gray;  
  36.     }  
  37.     protected void LogInfo(object value)  
  38.     {  
  39.         Debug.Log(value);  
  40.     }  
  41.   
  42.     protected void DrawCircle(ref Circle2 circle)  
  43.     {  
  44.         int count = 40;  
  45.         float delta = 2f * Mathf.PI / count;  
  46.         Vector3 prev = circle.Eval(0);  
  47.         for (int i = 1; i <= count; ++i)  
  48.         {  
  49.             Vector3 curr = circle.Eval(i * delta);  
  50.             Gizmos.DrawLine(prev, curr);  
  51.             prev = curr;  
  52.         }  
  53.     }  
  54. }  


  1. public struct Circle2  
  2. {  
  3.     public Vector2 Center;  
  4.     public float Radius;  
  5.   
  6.     public Circle2(Vector2 center, float radius)  
  7.     {  
  8.         this.Center = center;  
  9.         this.Radius = radius;  
  10.     }  
  11.   
  12.     public bool Contains(ref Vector2 point)  
  13.     {  
  14.         Vector2 vector = point - this.Center;  
  15.         return (vector.SqrMagnitude() <= (this.Radius * this.Radius));  
  16.     }  
  17.   
  18.     public Vector2 Eval(float t)  
  19.     {  
  20.         return new Vector2(this.Center.x + (this.Radius * Mathf.Cos(t)), this.Center.y + (this.Radius * Mathf.Sin(t)));  
  21.     }  
  22.   
  23.     public static Circle2 CreateCircle2(Transform circle)  
  24.     {  
  25.         return new Circle2(circle.position, circle.localScale.x);  
  26.     }  
  27. }  

4  三角形内



  1. [ExecuteInEditMode]  
  2. public class Test_ContPoint2Triangle2 : MonoBehaviour  
  3. {  
  4.     public Transform Point;  
  5.     public Transform V0, V1, V2;  
  6.   
  7.     private const float _pointRadius = .11f;  
  8.   
  9.     private void OnDrawGizmos()  
  10.     {  
  11.         Vector2 point = Point.position;  
  12.         Triangle2 triangle = Triangle2.CreateTriangle2(V0, V1, V2);  
  13.   
  14.         Orientations orientation = triangle.CalcOrientation();  
  15.         if (orientation == Orientations.CCW)  
  16.         {  
  17.             bool cont = triangle.Contains(ref point);  
  18.             bool cont1 = triangle.ContainsCCW(ref point); // 如果你知道三角形方向(顶点顺序方向),就用这个  
  19.   
  20.             FiguresColor();  
  21.             DrawTriangle(ref triangle);  
  22.             if (cont) ResultsColor();  
  23.             DrawPoint(point);  
  24.   
  25.             LogInfo("Orientation: " + orientation + "    Contained: " + cont);  
  26.             if (cont != cont1)  
  27.                 Debug.LogError("cont != cont1");  
  28.         }  
  29.         else if (orientation == Orientations.CW)  
  30.         {  
  31.             bool cont = triangle.Contains(ref point);  
  32.             bool cont1 = triangle.ContainsCW(ref point); // 如果你知道三角形方向(顶点顺序方向),就用这个  
  33.   
  34.             FiguresColor();  
  35.             DrawTriangle(ref triangle);  
  36.             if (cont) ResultsColor();  
  37.             DrawPoint(point);  
  38.   
  39.             LogInfo("Orientation: " + orientation + "    Contained: " + cont);  
  40.             if (cont != cont1)  
  41.                 Debug.LogError("cont != cont1");  
  42.         }  
  43.         else // Degenerate  
  44.         {  
  45.             Debug.LogError("Triangle is degenerate");  
  46.         }  
  47.     }  
  48.   
  49.   
  50.     void DrawTriangle(ref Triangle2 triangle)  
  51.     {  
  52.         Gizmos.DrawLine(triangle.V0, triangle.V1);  
  53.         Gizmos.DrawLine(triangle.V1, triangle.V2);  
  54.         Gizmos.DrawLine(triangle.V2, triangle.V0);  
  55.     }  
  56.   
  57.     protected void DrawPoint(Vector2 position)  
  58.     {  
  59.         Gizmos.DrawSphere(position, _pointRadius);  
  60.     }  
  61.     protected void ResultsColor()  
  62.     {  
  63.         Gizmos.color = Color.blue;  
  64.     }  
  65.   
  66.     protected void FiguresColor()  
  67.     {  
  68.         Gizmos.color = Color.gray;  
  69.     }  
  70.     protected void LogInfo(object value)  
  71.     {  
  72.         Debug.Log(value);  
  73.     }  
  74. }  


  1. public enum Orientations  
  2. {  
  3.     CW,  
  4.     CCW,  
  5.     None  
  6. }  
  7.   
  8. public struct Triangle2 {  
  9.     public Vector2 V0;  
  10.     public Vector2 V1;  
  11.     public Vector2 V2;  
  12.   
  13.     public static Triangle2 CreateTriangle2(Transform v0, Transform v1, Transform v2)  
  14.     {  
  15.         return new Triangle2(v0.position, v1.position, v2.position);  
  16.     }  
  17.   
  18.     public Triangle2(Vector2 v0, Vector2 v1, Vector2 v2)  
  19.     {  
  20.         this.V0 = v0;  
  21.         this.V1 = v1;  
  22.         this.V2 = v2;  
  23.     }  
  24.   
  25.     public Orientations CalcOrientation(float threshold = 1E-05f)  
  26.     {  
  27.         float num = this.CalcDeterminant();  
  28.         if (num > threshold)  
  29.         {  
  30.             return Orientations.CCW;  
  31.         }  
  32.         if (num < -threshold)  
  33.         {  
  34.             return Orientations.CW;  
  35.         }  
  36.         return Orientations.None;  
  37.     }  
  38.   
  39.     public float CalcDeterminant()  
  40.     {  
  41.         return ((((((this.V1.x * this.V2.y) + (this.V0.x * this.V1.y)) + (this.V2.x * this.V0.y)) - (this.V1.x * this.V0.y)) - (this.V2.x * this.V1.y)) - (this.V0.x * this.V2.y));  
  42.     }  
  43.   
  44.     public bool Contains(ref Vector2 point)  
  45.     {  
  46.         bool flag = (((point.x - this.V1.x) * (this.V0.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V0.x - this.V1.x))) < 0f;  
  47.         bool flag2 = (((point.x - this.V2.x) * (this.V1.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V1.x - this.V2.x))) < 0f;  
  48.         if (flag != flag2)  
  49.         {  
  50.             return false;  
  51.         }  
  52.         bool flag3 = (((point.x - this.V0.x) * (this.V2.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V2.x - this.V0.x))) < 0f;  
  53.         return (flag2 == flag3);  
  54.     }  
  55.   
  56.     /// <summary>  
  57.     /// 当已经知道 三个顶点的顺序是逆时针方向的时候使用  
  58.     /// </summary>  
  59.     /// <param name="point"></param>  
  60.     /// <returns></returns>  
  61.     public bool ContainsCCW(ref Vector2 point)  
  62.     {  
  63.         if ((((point.x - this.V0.x) * (this.V1.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V1.x - this.V0.x))) > 0f)  
  64.         {  
  65.             return false;  
  66.         }  
  67.         if ((((point.x - this.V1.x) * (this.V2.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V2.x - this.V1.x))) > 0f)  
  68.         {  
  69.             return false;  
  70.         }  
  71.         if ((((point.x - this.V2.x) * (this.V0.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V0.x - this.V2.x))) > 0f)  
  72.         {  
  73.             return false;  
  74.         }  
  75.         return true;  
  76.     }  
  77.   
  78.     /// <summary>  
  79.     /// 当已经知道 三个顶点的顺序是顺时针方向的时候使用  
  80.     /// </summary>  
  81.     /// <param name="point"></param>  
  82.     /// <returns></returns>  
  83.     public bool ContainsCW(ref Vector2 point)  
  84.     {  
  85.         if ((((point.x - this.V0.x) * (this.V1.y - this.V0.y)) - ((point.y - this.V0.y) * (this.V1.x - this.V0.x))) < 0f)  
  86.         {  
  87.             return false;  
  88.         }  
  89.         if ((((point.x - this.V1.x) * (this.V2.y - this.V1.y)) - ((point.y - this.V1.y) * (this.V2.x - this.V1.x))) < 0f)  
  90.         {  
  91.             return false;  
  92.         }  
  93.         if ((((point.x - this.V2.x) * (this.V0.y - this.V2.y)) - ((point.y - this.V2.y) * (this.V0.x - this.V2.x))) < 0f)  
  94.         {  
  95.             return false;  
  96.         }  
  97.         return true;  
  98.     }  
  99. }  

5   凸多边形内



  1. public class Test_ContPoint2ConvexPolygon2 : MonoBehaviour {  
  2.     public Transform Point;  
  3.     public Transform[] ConvexPolygon;  
  4.     private const float _pointRadius = .11f;  
  5.   
  6.     private void OnDrawGizmos()  
  7.     {  
  8.         Vector2 point = Point.position;  
  9.         Polygon2 convexPolygon = Polygon2.CreatePolygon2(ConvexPolygon);  
  10.   
  11.         Orientations orientation;  
  12.         // 判断是凸状的不?  
  13.         bool convex = convexPolygon.IsConvex(out orientation);  
  14.         if (convex)  
  15.         {  
  16.             bool cont;  
  17.             if (orientation == Orientations.CCW)  
  18.             {  
  19.                 cont = convexPolygon.ContainsConvexCCW(ref point);  
  20.                 //cont = convexPolygon.ContainsConvexQuadCCW(point); // 如果你知道 方向(顶点顺序方向),就用这个  
  21.             }  
  22.             else // CW  
  23.             {  
  24.                 cont = convexPolygon.ContainsConvexCW(ref point);  
  25.                 //cont = convexPolygon.ContainsConvexQuadCW(point);  
  26.             }  
  27.   
  28.             FiguresColor();  
  29.             DrawPolygon(convexPolygon);  
  30.             if (cont) ResultsColor();  
  31.             DrawPoint(point);  
  32.   
  33.             LogInfo("Orientation: " + orientation + "     Contained: " + cont);  
  34.         }  
  35.         else  
  36.         {  
  37.             FiguresColor();  
  38.             DrawPolygon(convexPolygon);  
  39.             DrawPoint(point);  
  40.   
  41.             Debug.LogError("polygon is non-convex");  
  42.         }  
  43.     }  
  44.   
  45.     protected void DrawPolygon(Polygon2 polygon)  
  46.     {  
  47.         for (int i0 = 0, i1 = polygon.VertexCount - 1; i0 < polygon.VertexCount; i1 = i0, ++i0)  
  48.         {  
  49.             Gizmos.DrawLine(polygon[i0], polygon[i1]);  
  50.         }  
  51.     }  
  52.   
  53.     protected void DrawPoint(Vector2 position)  
  54.     {  
  55.         Gizmos.DrawSphere(position, _pointRadius);  
  56.     }  
  57.     protected void ResultsColor()  
  58.     {  
  59.         Gizmos.color = Color.blue;  
  60.     }  
  61.   
  62.     protected void FiguresColor()  
  63.     {  
  64.         Gizmos.color = Color.gray;  
  65.     }  
  66.     protected void LogInfo(object value)  
  67.     {  
  68.         Debug.Log(value);  
  69.     }  
  70. }  


  1. public struct Edge2  
  2. {  
  3.     public Vector2 Point0;  
  4.     public Vector2 Point1;  
  5.     public Vector2 Direction;  
  6.     public Vector2 Normal;  
  7.     public float Length;  
  8. }  
  9.   
  10. public static class Vector3ex  
  11. {  
  12.     public static Vector2 ToVector2XY(this Vector3 vector)  
  13.     {  
  14.         return new Vector2(vector.x, vector.y);  
  15.     }  
  16. }  
  17.   
  18. public struct Polygon2 {  
  19.   
  20.     private Edge2[] _edges;  
  21.     private Vector2[] _vertices;  
  22.     public Polygon2(int vertexCount)  
  23.     {  
  24.         this._vertices = new Vector2[vertexCount];  
  25.         this._edges = new Edge2[vertexCount];  
  26.     }  
  27.   
  28.     public Vector2 this[int vertexIndex]  
  29.     {  
  30.         get  
  31.         {  
  32.             return this._vertices[vertexIndex];  
  33.         }  
  34.         set  
  35.         {  
  36.             this._vertices[vertexIndex] = value;  
  37.         }  
  38.     }  
  39.   
  40.     public int VertexCount  
  41.     {  
  42.         get  
  43.         {  
  44.             return this._vertices.Length;  
  45.         }  
  46.     }  
  47.   
  48.     public static Polygon2 CreatePolygon2(Transform[] polygon)  
  49.     {  
  50.         Polygon2 result = new Polygon2(polygon.Length);  
  51.         for (int i = 0; i < polygon.Length; ++i)  
  52.         {  
  53.             result[i] = polygon[i].position.ToVector2XY();  
  54.         }  
  55.         result.UpdateEdges();  
  56.         return result;  
  57.     }  
  58.   
  59.     public void UpdateEdges()  
  60.     {  
  61.         int length = this._vertices.Length;  
  62.         int index = length - 1;  
  63.         for (int i = 0; i < length; i++)  
  64.         {  
  65.             Vector2 vector = (this._edges[index].Point1 = this._vertices[i]) - (this._edges[index].Point0 = this._vertices[index]);  
  66.             this._edges[index].Length = Vector2ex.Normalize(ref vector, 1E-05f);  
  67.             this._edges[index].Direction = vector;  
  68.             this._edges[index].Normal = vector.Perp();  
  69.             index = i;  
  70.         }  
  71.     }  
  72.   
  73.     /// <summary>  
  74.     /// 判断当前是不是 凸 状的  
  75.     /// </summary>  
  76.     /// <param name="orientation"></param>  
  77.     /// <param name="threshold"></param>  
  78.     /// <returns></returns>  
  79.     public bool IsConvex(out Orientations orientation, float threshold = 1E-05f)  
  80.     {  
  81.         orientation = Orientations.None;  
  82.         int length = this._edges.Length;  
  83.         int num2 = 0;  
  84.         int index = length - 1;  
  85.         for (int i = 0; i < length; i++)  
  86.         {  
  87.             Vector2 vector = -this._edges[index].Direction;  
  88.             Vector2 direction = this._edges[i].Direction;  
  89.             float num5 = vector.DotPerp(ref direction);  
  90.             int num6 = ((num5 < -threshold) || (num5 > threshold)) ? ((num5 > 0f) ? 1 : -1) : 0;  
  91.             if (num6 != 0)  
  92.             {  
  93.                 if (num2 != 0)  
  94.                 {  
  95.                     if (((num2 > 0) && (num6 < 0)) || ((num2 < 0) && (num6 > 0)))  
  96.                     {  
  97.                         return false;  
  98.                     }  
  99.                 }  
  100.                 else  
  101.                 {  
  102.                     num2 += num6;  
  103.                 }  
  104.             }  
  105.             index = i;  
  106.         }  
  107.         orientation = (num2 == 0) ? Orientations.None : ((num2 > 0) ? Orientations.CW : Orientations.CCW);  
  108.         return (orientation != Orientations.None);  
  109.     }  
  110.   
  111.     public bool ContainsConvexCCW(ref Vector2 point)  
  112.     {  
  113.         return this.SubContainsPointCCW(ref point, 0, 0);  
  114.     }  
  115.   
  116.     public bool ContainsConvexCW(ref Vector2 point)  
  117.     {  
  118.         return this.SubContainsPointCW(ref point, 0, 0);  
  119.     }  
  120.   
  121.     private bool SubContainsPointCCW(ref Vector2 p, int i0, int i1)  
  122.     {  
  123.         float num2;  
  124.         float num3;  
  125.         float num4;  
  126.         float num5;  
  127.         int num7;  
  128.         int length = this._vertices.Length;  
  129.         int num6 = i1 - i0;  
  130.         if ((num6 == 1) || ((num6 < 0) && ((num6 + length) == 1)))  
  131.         {  
  132.             num2 = this._vertices[i1].y - this._vertices[i0].y;  
  133.             num3 = this._vertices[i0].x - this._vertices[i1].x;  
  134.             num4 = p.x - this._vertices[i0].x;  
  135.             num5 = p.y - this._vertices[i0].y;  
  136.             return (((num2 * num4) + (num3 * num5)) <= 0f);  
  137.         }  
  138.         if (i0 < i1)  
  139.         {  
  140.             num7 = (i0 + i1) >> 1;  
  141.         }  
  142.         else  
  143.         {  
  144.             num7 = ((i0 + i1) + length) >> 1;  
  145.             if (num7 >= length)  
  146.             {  
  147.                 num7 -= length;  
  148.             }  
  149.         }  
  150.         num2 = this._vertices[num7].y - this._vertices[i0].y;  
  151.         num3 = this._vertices[i0].x - this._vertices[num7].x;  
  152.         num4 = p.x - this._vertices[i0].x;  
  153.         num5 = p.y - this._vertices[i0].y;  
  154.         if (((num2 * num4) + (num3 * num5)) > 0f)  
  155.         {  
  156.             return this.SubContainsPointCCW(ref p, i0, num7);  
  157.         }  
  158.         return this.SubContainsPointCCW(ref p, num7, i1);  
  159.     }  
  160.   
  161.     private bool SubContainsPointCW(ref Vector2 p, int i0, int i1)  
  162.     {  
  163.         float num2;  
  164.         float num3;  
  165.         float num4;  
  166.         float num5;  
  167.         int num7;  
  168.         int length = this._vertices.Length;  
  169.         int num6 = i1 - i0;  
  170.         if ((num6 == 1) || ((num6 < 0) && ((num6 + length) == 1)))  
  171.         {  
  172.             num2 = this._vertices[i1].y - this._vertices[i0].y;  
  173.             num3 = this._vertices[i0].x - this._vertices[i1].x;  
  174.             num4 = p.x - this._vertices[i0].x;  
  175.             num5 = p.y - this._vertices[i0].y;  
  176.             return (((num2 * num4) + (num3 * num5)) >= 0f);  
  177.         }  
  178.         if (i0 < i1)  
  179.         {  
  180.             num7 = (i0 + i1) >> 1;  
  181.         }  
  182.         else  
  183.         {  
  184.             num7 = ((i0 + i1) + length) >> 1;  
  185.             if (num7 >= length)  
  186.             {  
  187.                 num7 -= length;  
  188.             }  
  189.         }  
  190.         num2 = this._vertices[num7].y - this._vertices[i0].y;  
  191.         num3 = this._vertices[i0].x - this._vertices[num7].x;  
  192.         num4 = p.x - this._vertices[i0].x;  
  193.         num5 = p.y - this._vertices[i0].y;  
  194.         if (((num2 * num4) + (num3 * num5)) < 0f)  
  195.         {  
  196.             return this.SubContainsPointCW(ref p, i0, num7);  
  197.         }  
  198.         return this.SubContainsPointCW(ref p, num7, i1);  
  199.     }  
  200. }  


  1. public static class Vector2ex  
  2. {  
  3.     public static float Dot(this Vector2 vector, ref Vector2 value)  
  4.     {  
  5.         return ((vector.x * value.x) + (vector.y * value.y));  
  6.     }  
  7.   
  8.     public static float Normalize(ref Vector2 vector, float epsilon = 1E-05f)  
  9.     {  
  10.         float num = Mathf.Sqrt((vector.x * vector.x) + (vector.y * vector.y));  
  11.         if (num >= epsilon)  
  12.         {  
  13.             float num2 = 1f / num;  
  14.             vector.x *= num2;  
  15.             vector.y *= num2;  
  16.             return num;  
  17.         }  
  18.         vector.x = 0f;  
  19.         vector.y = 0f;  
  20.         return 0f;  
  21.     }  
  22.   
  23.     public static float DotPerp(this Vector2 vector, ref Vector2 value)  
  24.     {  
  25.         return ((vector.x * value.y) - (vector.y * value.x));  
  26.     }  
  27.   
  28.     public static Vector2 Perp(this Vector2 vector)  
  29.     {  
  30.         return new Vector2(vector.y, -vector.x);  
  31.     }  
  32. }  




6  直接判断是否在多边形内。   凹多边形内 (其实不管是凹还是凸了)






  1. public struct Edge2  
  2. {  
  3.     public Vector2 Point0;  
  4.     public Vector2 Point1;  
  5.     public Vector2 Direction;  
  6.     public Vector2 Normal;  
  7.     public float Length;  
  8. }  
  9.   
  10. public static class Vector3ex  
  11. {  
  12.     public static Vector2 ToVector2XY(this Vector3 vector)  
  13.     {  
  14.         return new Vector2(vector.x, vector.y);  
  15.     }  
  16. }  
  17.   
  18. public struct Polygon2 {  
  19.   
  20.     private Edge2[] _edges;  
  21.     private Vector2[] _vertices;  
  22.     public Polygon2(int vertexCount)  
  23.     {  
  24.         this._vertices = new Vector2[vertexCount];  
  25.         this._edges = new Edge2[vertexCount];  
  26.     }  
  27.   
  28.     public Vector2 this[int vertexIndex]  
  29.     {  
  30.         get  
  31.         {  
  32.             return this._vertices[vertexIndex];  
  33.         }  
  34.         set  
  35.         {  
  36.             this._vertices[vertexIndex] = value;  
  37.         }  
  38.     }  
  39.   
  40.     public int VertexCount  
  41.     {  
  42.         get  
  43.         {  
  44.             return this._vertices.Length;  
  45.         }  
  46.     }  
  47.   
  48.     public static Polygon2 CreatePolygon2(Transform[] polygon)  
  49.     {  
  50.         Polygon2 result = new Polygon2(polygon.Length);  
  51.         for (int i = 0; i < polygon.Length; ++i)  
  52.         {  
  53.             result[i] = polygon[i].position.ToVector2XY();  
  54.         }  
  55.         result.UpdateEdges();  
  56.         return result;  
  57.     }  
  58.   
  59.     public void UpdateEdges()  
  60.     {  
  61.         int length = this._vertices.Length;  
  62.         int index = length - 1;  
  63.         for (int i = 0; i < length; i++)  
  64.         {  
  65.             Vector2 vector = (this._edges[index].Point1 = this._vertices[i]) - (this._edges[index].Point0 = this._vertices[index]);  
  66.             this._edges[index].Length = Vector2ex.Normalize(ref vector, 1E-05f);  
  67.             this._edges[index].Direction = vector;  
  68.             this._edges[index].Normal = vector.Perp();  
  69.             index = i;  
  70.         }  
  71.     }  
  72.   
  73.     public bool ContainsSimple(ref Vector2 point)  
  74.     {  
  75.         bool flag = false;  
  76.         int length = this._vertices.Length;  
  77.         int index = 0;  
  78.         int num3 = length - 1;  
  79.         while (index < length)  
  80.         {  
  81.             float num4;  
  82.             float num5;  
  83.             Vector2 vector = this._vertices[index];  
  84.             Vector2 vector2 = this._vertices[num3];  
  85.             if (point.y < vector2.y)  
  86.             {  
  87.                 if (vector.y <= point.y)  
  88.                 {  
  89.                     num5 = (point.y - vector.y) * (vector2.x - vector.x);  
  90.                     num4 = (point.x - vector.x) * (vector2.y - vector.y);  
  91.                     if (num5 > num4)  
  92.                     {  
  93.                         flag = !flag;  
  94.                     }  
  95.                 }  
  96.             }  
  97.             else if (point.y < vector.y)  
  98.             {  
  99.                 num5 = (point.y - vector.y) * (vector2.x - vector.x);  
  100.                 num4 = (point.x - vector.x) * (vector2.y - vector.y);  
  101.                 if (num5 < num4)  
  102.                 {  
  103.                     flag = !flag;  
  104.                 }  
  105.             }  
  106.             num3 = index;  
  107.             index++;  
  108.         }  
  109.         return flag;  
  110.     }  
  111. }  


  1. [ExecuteInEditMode]  
  2. public class Test_ContPoint2Polygon2 : MonoBehaviour  
  3. {  
  4.     public Transform Point;  
  5.     public Transform[] Polygon;  
  6.   
  7.     private const float _pointRadius = .11f;  
  8.   
  9.   
  10.     private void OnDrawGizmos()  
  11.     {  
  12.         Vector2 point = Point.position;  
  13.         Polygon2 polygon = Polygon2.CreatePolygon2(Polygon);  
  14.   
  15.         bool cont = polygon.ContainsSimple(ref point);  
  16.   
  17.         FiguresColor();  
  18.         DrawPolygon(polygon);  
  19.         if (cont) ResultsColor();  
  20.         DrawPoint(point);  
  21.   
  22.         LogInfo(cont);  
  23.     }  
  24.   
  25.     protected void DrawPolygon(Polygon2 polygon)  
  26.     {  
  27.         for (int i0 = 0, i1 = polygon.VertexCount - 1; i0 < polygon.VertexCount; i1 = i0, ++i0)  
  28.         {  
  29.             Gizmos.DrawLine(polygon[i0], polygon[i1]);  
  30.         }  
  31.     }  
  32.   
  33.     protected void DrawPoint(Vector2 position)  
  34.     {  
  35.         Gizmos.DrawSphere(position, _pointRadius);  
  36.     }  
  37.     protected void ResultsColor()  
  38.     {  
  39.         Gizmos.color = Color.blue;  
  40.     }  
  41.   
  42.     protected void FiguresColor()  
  43.     {  
  44.         Gizmos.color = Color.gray;  
  45.     }  
  46.     protected void LogInfo(object value)  
  47.     {  
  48.         Debug.Log(value);  
  49.     }  
  50. }  


  1. public static class Vector2ex  
  2. {  
  3.     public static float Normalize(ref Vector2 vector, float epsilon = 1E-05f)  
  4.     {  
  5.         float num = Mathf.Sqrt((vector.x * vector.x) + (vector.y * vector.y));  
  6.         if (num >= epsilon)  
  7.         {  
  8.             float num2 = 1f / num;  
  9.             vector.x *= num2;  
  10.             vector.y *= num2;  
  11.             return num;  
  12.         }  
  13.         vector.x = 0f;  
  14.         vector.y = 0f;  
  15.         return 0f;  
  16.     }  
  17.     public static Vector2 Perp(this Vector2 vector)  
  18.     {  
  19.         return new Vector2(vector.y, -vector.x);  
  20.     }  
  21. }  



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值