/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2007 */ public class Sphere { public Position c; public float r; public Sphere() { c = new Position(); r = 1.0F; } public Sphere(Position c,float r) { this.c = new Position(c); this.r = r; } public Sphere(Sphere s) { c = new Position(s.c); r = s.r; } public boolean existeIntersection(RayonLumineux rl) { float sx = rl.o.x - c.x; float sy = rl.o.y - c.y; float sz = rl.o.z - c.z; float a = rl.d.dx*rl.d.dx + rl.d.dy*rl.d.dy + rl.d.dz*rl.d.dz; float b = 2.0F*(rl.d.dx*sx + rl.d.dy*sy + rl.d.dz*sz); float c = sx*sx + sy*sy +sz*sz - r*r; float delta = b*b-4*a*c; if ( delta < 0.0F ) return(false); if ( delta == 0 ) { float t = -b/2.0F/a; return( t >= 0.0F); } float t1 = (-b-((float) Math.sqrt(delta)))/2.0F/a; if ( t1 >= 0 ) return(true); float t2 = (-b+((float) Math.sqrt(delta)))/2.0F/a; return( t2 >= 0 ); } private Position positionSurRayon(RayonLumineux rl,float t) { return(new Position(rl.d.dx*t+rl.o.x, rl.d.dy*t+rl.o.y, rl.d.dz*t+rl.o.z)); } public Position premiereIntersection(RayonLumineux rl) { float sx = rl.o.x - c.x; float sy = rl.o.y - c.y; float sz = rl.o.z - c.z; float a = rl.d.dx*rl.d.dx + rl.d.dy*rl.d.dy + rl.d.dz*rl.d.dz; float b = 2.0F*(rl.d.dx*sx + rl.d.dy*sy + rl.d.dz*sz); float c = sx*sx + sy*sy +sz*sz - r*r; float delta = b*b-4*a*c; if ( delta < 0.0F ) return(null); if ( delta == 0 ) { float t = -b/2.0F/a; if ( t >= 0.0F) return(positionSurRayon(rl,t)); else return(null); } { float t = (-b-((float) Math.sqrt(delta)))/2.0F/a; if ( t >= 0 ) return(positionSurRayon(rl,t)); } { float t = (-b+((float) Math.sqrt(delta)))/2.0F/a; if ( t >= 0 ) return(positionSurRayon(rl,t)); } return(null); } public String toString() { return("["+c+","+r+"]"); } }