home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch5_5 / finite.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-04  |  798 b   |  27 lines

  1. #include <iostream.h>
  2. #include <math.h>
  3.  
  4. #include "global.h"
  5.  
  6. ostream& operator<<(ostream& o, sphere& s) {s.out(o); return o;}
  7.  
  8. list<intersect*> *sphere::shape(ray& r) {
  9.         list<intersect*> *l=new list<intersect*>;
  10.         double b=r.d%(r.o-this->c);
  11.         double C=(r.o-this->c)%(r.o-this->c)-this->r*this->r;
  12.         double d=b*b-C;
  13.         if(d<0.) return l;
  14.         d=sqrt(d); double t1=(-b-d); double t2=(-b+d);
  15.         if(t1>EPS) {
  16.                 intersect* i=new intersect; i->t=t1; i->p=r.o+r.d*t1;
  17.                 i->n=norm(i->p-this->c); i->o=this; 
  18.                 *l+=i;
  19.         }
  20.         if(t2>EPS) {
  21.                 intersect* i=new intersect; i->t=t2; i->p=r.o+r.d*t2;
  22.                 i->n=norm(i->p-this->c); i->o=this; 
  23.                 *l+=i;
  24.         }
  25.         return l;
  26. }
  27.