예제 #1
0
        public static void TestCrossProduct()
        {
            var right   = Vector3Int.right;
            var left    = Vector3Int.left;
            var down    = Vector3Int.down;
            var up      = Vector3Int.up;
            var back    = new Vector3Int(0, 0, -1);
            var forward = new Vector3Int(0, 0, 1);

            Assert.AreEqual(forward, VECTOR3INT.Cross(right, up));
        }
예제 #2
0
        /// <summary>
        /// Computes the intersection of (1) the line defined by <paramref name="p0"/> and <paramref name="d0"/> and (2) the line defined by <paramref name="p1"/> and <paramref name="d1"/>.
        /// </summary>
        /// <param name="p0">Any point on the first line.</param>
        /// <param name="d0">The direction of the first line.</param>
        /// <param name="p1">Any point on the second line.</param>
        /// <param name="d1">The direction of second line.</param>
        /// <param name="intersection">The point of intersection.</param>
        /// <returns>Is there a valid intersection point?</returns>
        public static bool LineLineIntersection(Vector3Int p0, Vector3Int d0, Vector3Int p1, Vector3Int d1, out Vector3Int intersection)
        {
            Vector3Int delta          = p1 - p0;
            Vector3Int crossVec0and1  = VECTOR3INT.Cross(d0, d1);
            Vector3Int crossDeltaAnd1 = VECTOR3INT.Cross(delta, d1);

            int planarFactor = VECTOR3INT.Dot(delta, crossVec0and1);
            var isCoplanar   = planarFactor == 0;
            var isParallel   = crossVec0and1.sqrMagnitude == 0;

            if (isCoplanar && !isParallel)
            {
                int s = VECTOR3INT.Dot(crossDeltaAnd1, crossVec0and1) / crossVec0and1.sqrMagnitude;
                intersection = p0 + s * d0;
                return(true);
            }
            else
            {
                intersection = Vector3Int.zero;
                return(false);
            }
        }