R3.Geometry.Sphere.Intersection C# (CSharp) Method

Intersection() public method

Finds the intersection (a circle) between us and another sphere. Returns null if sphere centers are coincident or no intersection exists. Does not currently work for planes.
public Intersection ( Sphere s ) : Circle3D
s Sphere
return Circle3D
        public Circle3D Intersection( Sphere s )
        {
            if( this.IsPlane || s.IsPlane )
                throw new System.NotImplementedException();

            double r = s.Radius;
            double R = this.Radius;

            Vector3D diff = this.Center - s.Center;
            double d = diff.Abs();
            if( Tolerance.Zero( d ) || d > r + R )
                return null;

            double x = ( d*d + r*r - R*R ) / ( 2*d );
            double y = Math.Sqrt( r*r - x*x );

            Circle3D result = new Circle3D();
            diff.Normalize();
            result.Normal = diff;
            result.Center = s.Center + diff * x;
            result.Radius = y;
            return result;
        }

Usage Example

Exemplo n.º 1
0
        public static void GenImage()
        {
            int size = 1000;
            Bitmap image = new Bitmap( size, size );
            ImageSpace i = new ImageSpace( size, size );
            double b = 1.1;
            i.XMin = -b; i.XMax = b;
            i.YMin = -b; i.YMax = b;

            Vector3D cen = HoneycombPaper.CellCenBall;
            cen = H3Models.BallToUHS( cen );
            Sphere[] simplex = Simplex( ref cen );

            Sphere inSphere = InSphere( simplex );

            using( Graphics g = Graphics.FromImage( image ) )
            foreach( int[] reflections in AllCells() )
            {
                Sphere clone = inSphere.Clone();
                foreach( int r in reflections )
                    clone.Reflect( simplex[r] );

                Sphere ball = new Sphere();
                Circle3D inSphereIdealBall = ball.Intersection( clone );
                Circle3D inSphereIdealUHS = H3Models.BallToUHS( inSphereIdealBall );
                Circle inSphereIdeal = new Circle { Center = inSphereIdealUHS.Center, Radius = inSphereIdealUHS.Radius };

                using( Brush brush = new SolidBrush( Color.Blue ) )
                    DrawUtils.DrawFilledCircle( inSphereIdeal, g, i, brush );
            }

            image.Save( "threefifty.png", ImageFormat.Png );
        }
All Usage Examples Of R3.Geometry.Sphere::Intersection