/* Manipulations sur des spheres et des droites 3D */ public class IntersectionSphereDroite { /* Type agrege de stockage d'une position 3D */ static class Position3D { double x = 0.0; double y = 0.0; double z = 0.0; }; /* Type agrege de stockage d'une sphere 3D */ static class Sphere { double rayon = 1.0; Position3D centre = new Position3D(); }; /* Type agrege de stockage d'une droite 3D */ static class Droite3D { double ax = 1.0; double bx = 0.0; double ay = 0.0; double by = 0.0; double az = 0.0; double bz = 0.0; }; /* Programme principal */ public static void main(String [] args) { Sphere sp = new Sphere(); Droite3D dr = new Droite3D(); double dx; double dy; double dz; double a; double b; double c; double delta; boolean intersection; Ecran.afficherln("Sphere"); Ecran.afficher("Rayon ? "); sp.rayon = Clavier.saisirDouble(); Ecran.afficher("Coordonnee x du centre ? "); sp.centre.x = Clavier.saisirDouble(); Ecran.afficher("Coordonnee y du centre ? "); sp.centre.y = Clavier.saisirDouble(); Ecran.afficher("Coordonnee z du centre ? "); sp.centre.z = Clavier.saisirDouble(); Ecran.afficherln("Deuxieme sphere"); Ecran.afficherln("Droite"); Ecran.afficher("Coefficient ax ? "); dr.ax = Clavier.saisirDouble(); Ecran.afficher("Coefficient bx ? "); dr.bx = Clavier.saisirDouble(); Ecran.afficher("Coefficient ay ? "); dr.ay = Clavier.saisirDouble(); Ecran.afficher("Coefficient by ? "); dr.by = Clavier.saisirDouble(); Ecran.afficher("Coefficient az ? "); dr.az = Clavier.saisirDouble(); Ecran.afficher("Coefficient bz ? "); dr.bz = Clavier.saisirDouble(); dx = sp.centre.x-dr.bx; dy = sp.centre.y-dr.by; dz = sp.centre.z-dr.bz; a = dr.ax*dr.ax + dr.ay*dr.ay + dr.az*dr.az; b = 2.0*(dr.ax*dx + dr.ay*dy + dr.az*dz); c = dx*dx + dy*dy + dz*dz - sp.rayon*sp.rayon; delta = b*b - 4.0*a*c; if ( delta >= 0.0 ) { intersection = true; } else { intersection = false; } Ecran.afficherln("Intersection : ",intersection); } }