/* Manipulations sur des spheres et des droites 3D */

public class IntersectionSphereDroiteEnConsole {

/* 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) {
    Console.setTitle("IntersectionSphereDroite");
    Sphere sp = new Sphere();
    Droite3D dr = new Droite3D();
    double dx;
    double dy;
    double dz;
    double a;
    double b;
    double c;
    double delta;
    boolean intersection;
    Console.afficherln("Sphere");
    Console.afficher("Rayon                  ? ");
    sp.rayon = Console.saisirDouble();
    Console.afficher("Coordonnee x du centre ? ");
    sp.centre.x = Console.saisirDouble();
    Console.afficher("Coordonnee y du centre ? ");
    sp.centre.y = Console.saisirDouble();
    Console.afficher("Coordonnee z du centre ? ");
    sp.centre.z = Console.saisirDouble();
    Console.afficherln("Deuxieme sphere");
    Console.afficherln("Droite");
    Console.afficher("Coefficient ax         ? ");
    dr.ax = Console.saisirDouble();
    Console.afficher("Coefficient bx         ? ");
    dr.bx = Console.saisirDouble();
    Console.afficher("Coefficient ay         ? ");
    dr.ay = Console.saisirDouble();
    Console.afficher("Coefficient by         ? ");
    dr.by = Console.saisirDouble();
    Console.afficher("Coefficient az         ? ");
    dr.az = Console.saisirDouble();
    Console.afficher("Coefficient bz         ? ");
    dr.bz = Console.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; }
    Console.afficherln("Intersection : ",intersection);
  }
}