unity 計算點是否在平面範圍內

這段是網上的傳入三維座標和三維座標集合就可以

 ///<summary>
        /// 計算一個點是否在一個多邊形範圍內
        /// 如果過該點的線段與多邊形的交點不爲零且距該點左右方向交點數量都爲奇數時  該點再多邊形範圍內
        /// <summary>
        /// <param name="point">測試點</param>
        /// <param name="vertexs">多邊形的頂點集合</param>
        /// <returns><returns>
        public static bool PolygonIsContainPoint(Vector3 point, List<Vector3> vertexs)
        {
            //for (int i = 0; i < vertexs.Count; i++)
            //{
            //    if (point.x == vertexs[i].x && point.z == vertexs[i].z)
            //    {
            //        return true;
            //    }
            //}

            //判斷測試點和橫座標方向與多邊形的邊的交叉點

            int leftNum = 0;  //左方向上的交叉點數
            int rightNum = 0;  //右方向上的交叉點數
            int index = 1;
            for (int i = 0; i < vertexs.Count; i++)
            {
                if (i == vertexs.Count - 1) { index = -i; }
                //找到相交的線段 
                if (point.z >= vertexs[i].z && point.z < vertexs[i + index].z || point.z < vertexs[i].z && point.z >= vertexs[i + index].z)
                {
                    Vector3 vecNor = (vertexs[i + index] - vertexs[i]);


                    //處理直線方程爲常數的情況
                    if (vecNor.x == 0.0f)
                    {

                        if (vertexs[i].x < point.x)
                        {
                            leftNum++;
                        }
                        else if (vertexs[i].x == point.x)
                        { }
                        else
                        {

                            rightNum++;
                        }

                    }
                    else
                    {
                        vecNor = vecNor.normalized;
                        float k = vecNor.z / vecNor.x;
                        //print(vertexs[i + index]);
                        float b = vertexs[i].z - k * vertexs[i].x;

                        if ((point.z - b) / k < point.x)
                        {
                            leftNum++;

                        }
                        else if ((point.z - b) / k == point.x)
                        {

                        }
                        else
                        {
                            rightNum++;

                        }

                    }

                }

            }

            if (leftNum % 2 != 0 || rightNum % 2 != 0)
            {
                return true;
            }
            return false;
        }

計算邊緣兩邊的點

/// <summary>
        /// 邊緣兩邊的點
        /// </summary>
        /// <param name="pointA"></param>
        /// <param name="pointB"></param>
        /// <param name="lineWidth"></param>
        /// <param name="isOn"></param>
        /// <returns></returns>
        private List<Vector3> SidePoint(Vector3 pointA, Vector3 pointB, float lineWidth)
        {
            List<Vector3> points = new List<Vector3>();

            //Debug.Log(pointA + ":" + pointB);
            float HorDisABx = pointB.x - pointA.x;
            float HorDisABz = pointB.z - pointA.z;
            float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx, 2) + Mathf.Pow(HorDisABz, 2));
            float offsetX = HorDisABz * lineWidth / HorDisAB;
            float offsetZ = HorDisABx * lineWidth / HorDisAB;

            Vector3 Point1 = new Vector3(pointA.x - offsetX, pointA.y, pointA.z + offsetZ);
            Vector3 Point2 = new Vector3(pointA.x + offsetX, pointA.y, pointA.z - offsetZ);
            Vector3 Point3 = new Vector3(pointB.x + offsetX, pointB.y, pointB.z - offsetZ);
            Vector3 Point4 = new Vector3(pointB.x - offsetX, pointB.y, pointB.z + offsetZ);

            //Vector3 centL = (Point1 + Point4) / 2;
            //Vector3 centR = (Point2 + Point3) / 2;
            //points.Add(centL);
            //points.Add(centR);

            points.Add(Point1);
            points.Add(Point2);
            points.Add(Point3);
            points.Add(Point4);

            //  Debug.Log("Point0" + points[0]+ "Point1" + points[1]+"Point2" + points[2]+ "Point3" + points[3]);
            return points;
        }

傳入二維座標點計算

 public static bool IsPointInPolygon(Vector2 p, List<Vector2> vertexs)
        {
            int crossNum = 0;
            int vertexCount = vertexs.Count;

            for (int i = 0; i < vertexCount; i++)
            {
                Vector2 v1 = vertexs[i];
                Vector2 v2 = vertexs[(i + 1) % vertexCount];

                if (((v1.y <= p.y) && (v2.y > p.y))
                    || ((v1.y > p.y) && (v2.y <= p.y)))
                {
                    if (p.x < v1.x + (p.y - v1.y) / (v2.y - v1.y) * (v2.x - v1.x))
                    {
                        crossNum += 1;
                    }
                }
            }

            if (crossNum % 2 == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章