public static bool LineRectIntersection(this Rect rect, Vector2 lineStart,
                                                Vector2 lineEnd, out Vector2 intersectionPoint)
        {
            Vector2 topLeft     = rect.TopLeftGUI();
            Vector2 topRight    = rect.TopRightGUI();
            Vector2 bottomLeft  = rect.BottomLeftGUI();
            Vector2 bottomRight = rect.BottomRightGUI();

            intersectionPoint = Vector2.zero;

            if (ZPMath.LineLineCollision(topLeft, topRight, lineStart, lineEnd, out intersectionPoint))
            {
                return(true);
            }

            if (ZPMath.LineLineCollision(topRight, bottomRight, lineStart, lineEnd, out intersectionPoint))
            {
                return(true);
            }

            if (ZPMath.LineLineCollision(bottomRight, bottomLeft, lineStart, lineEnd, out intersectionPoint))
            {
                return(true);
            }

            if (ZPMath.LineLineCollision(topLeft, bottomLeft, lineStart, lineEnd, out intersectionPoint))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper to divide a Vector3Int by another while ignoring 0's in the other vector
        /// (if the other vector has a 0 in an axis, that axis is ignored for division and the value
        /// of this vector is used instead. i.e. if other.x == 0, then result.x == thisVector.x)
        /// (Generally for getting averages of each axis)
        /// </summary>
        /// <param name="thisVector">This vector</param>
        /// <param name="other">The vector that represents the values to divide by</param>
        /// <param name="epsilon">Used to determine proximity to 0 to prevent divide by 0 errors</param>
        /// <returns>The results of the division. If any axis is 0, thisVectors value for that axis will be substituted </returns>
        public static Vector3Int SafeDivideComponents(this Vector3Int thisVector, Vector3Int other, float epsilon = 0.0001f)
        {
            for (int i = 0; i < 3; i++)
            {
                if (ZPMath.NearZero(other[i], epsilon))
                {
                    other[i] = 1;
                }
            }

            return(thisVector.DivideComponents(other));
        }