Exemple #1
0
        public void Test_Contains()
        {
            // 包含的
            Assert.True(one.Contains(Vector2.zero));
            Assert.True(one.Contains(Vector2.one));
            bool temp = true;

            for (int i = 0; i < 10; i++)
            {
                temp &= one.Contains(new Vector2(Random.value, Random.value));
            }
            Assert.True(temp);
            // 不包含的
            Assert.True(!one.Contains(new Vector2(1.01f, 1.01f)));
            Assert.True(!one.Contains(new Vector2(-1.01f, -1.01f)));
        }
Exemple #2
0
        /// <summary>
        /// Get the distance between a point and a quad
        /// </summary>
        /// <param name="point">The point from which the distance is calculated`</param>
        /// <param name="a">One of the quad's point. Is connected to b and d.</param>
        /// <param name="b">One of the quad's point. Is connected to a and c.</param>
        /// <param name="c">One of the quad's point. Is connected to b and d.</param>
        /// <param name="d">One of the quad's point. Is connected to c and a.</param>
        /// <returns></returns>
        public static float DistanceToQuad(Vector2 point, Vector2 a, Vector2 b, Vector2 c, Vector2 d)
        {
            s_QuadBuffer[0] = a;
            s_QuadBuffer[1] = b;
            s_QuadBuffer[2] = c;
            s_QuadBuffer[3] = d;

            var bounds = new Bounds2D(s_QuadBuffer);

            //Check if the point is inside the quad
            if (bounds.Contains(point))
            {
                var firstEdgeDir = bounds.center - (Vector2)GetEdgeCenter(a, b);
                var rayStart     = bounds.center + firstEdgeDir * (bounds.size.y + bounds.size.x + 2f);
                var collisions   = 0;

                for (int i = 0, count = s_QuadBuffer.Length - 1; i < count; ++i)
                {
                    var p1 = s_QuadBuffer[i];
                    var p2 = s_QuadBuffer[i + 1];
                    if (AreSegmentIntersecting(rayStart, point, p1, p2))
                    {
                        ++collisions;
                    }
                }

                if (collisions % 2 != 0)
                {
                    return(0f);
                }
            }

            s_QuadDistanceBuffer[0] = DistanceToLine(point, a, b);
            s_QuadDistanceBuffer[1] = DistanceToLine(point, b, c);
            s_QuadDistanceBuffer[2] = DistanceToLine(point, c, d);
            s_QuadDistanceBuffer[3] = DistanceToLine(point, d, a);

            return(Mathf.Min(s_QuadDistanceBuffer));
        }