Example #1
0
 public float Distance(Sphere other)
 => (Center.Distance(other.Center) - Radius - other.Radius).ClampLower(0);
Example #2
0
        public ContainmentType Contains(Sphere sphere)
        {
            if (sphere.Center.X - Min.X >= sphere.Radius &&
                sphere.Center.Y - Min.Y >= sphere.Radius &&
                sphere.Center.Z - Min.Z >= sphere.Radius &&
                Max.X - sphere.Center.X >= sphere.Radius &&
                Max.Y - sphere.Center.Y >= sphere.Radius &&
                Max.Z - sphere.Center.Z >= sphere.Radius)
            {
                return(ContainmentType.Contains);
            }

            double dmin = 0;

            double e = sphere.Center.X - Min.X;

            if (e < 0)
            {
                if (e < -sphere.Radius)
                {
                    return(ContainmentType.Disjoint);
                }
                dmin += e * e;
            }
            else
            {
                e = sphere.Center.X - Max.X;
                if (e > 0)
                {
                    if (e > sphere.Radius)
                    {
                        return(ContainmentType.Disjoint);
                    }
                    dmin += e * e;
                }
            }

            e = sphere.Center.Y - Min.Y;
            if (e < 0)
            {
                if (e < -sphere.Radius)
                {
                    return(ContainmentType.Disjoint);
                }
                dmin += e * e;
            }
            else
            {
                e = sphere.Center.Y - Max.Y;
                if (e > 0)
                {
                    if (e > sphere.Radius)
                    {
                        return(ContainmentType.Disjoint);
                    }
                    dmin += e * e;
                }
            }

            e = sphere.Center.Z - Min.Z;
            if (e < 0)
            {
                if (e < -sphere.Radius)
                {
                    return(ContainmentType.Disjoint);
                }
                dmin += e * e;
            }
            else
            {
                e = sphere.Center.Z - Max.Z;
                if (e > 0)
                {
                    if (e > sphere.Radius)
                    {
                        return(ContainmentType.Disjoint);
                    }
                    dmin += e * e;
                }
            }

            if (dmin <= sphere.Radius * sphere.Radius)
            {
                return(ContainmentType.Intersects);
            }

            return(ContainmentType.Disjoint);
        }
Example #3
0
        /// <summary>
        /// Gets whether or not the other <see cref="Sphere"/> intersects with this sphere.
        /// </summary>
        public bool Intersects(Sphere sphere)
        {
            var sqDistance = sphere.Center.DistanceSquared(Center);

            return(!(sqDistance > (sphere.Radius + Radius) * (sphere.Radius + Radius)));
        }
Example #4
0
 public static Box CreateFromSphere(Sphere sphere)
 => new Box(sphere.Center - new Vector3(sphere.Radius), sphere.Center + new Vector3(sphere.Radius));