Main Page | Class Hierarchy | Class List | File List | Class Members

random.h

00001 /* ************************************************************************* 00002 random.h - description 00003 ------------------- 00004 begin : Wed Oct 16 2002 00005 copyright : (C) 2002 by Micha Riser 00006 email : mriser@gmx.net 00007 00008 $Id: random_8h-source.html,v 1.1 2005/02/04 21:02:27 micha Exp $ 00009 00010 ************************************************************************* */ 00011 00012 /* ************************************************************************* 00013 * * 00014 * This program is free software; you can redistribute it and/or modify * 00015 * it under the terms of the GNU General Public License as published by * 00016 * the Free Software Foundation; either version 2 of the License, or * 00017 * (at your option) any later version. * 00018 * * 00019 ************************************************************************* */ 00020 00021 #ifndef RANDOM_H 00022 #define RANDOM_H 00023 00024 #include "types.h" 00025 #include "vector.h" 00026 00031 class Random { 00032 00033 public: 00034 Random(long s = 0) {randx = s;} 00035 void seed(long s) {randx = s;} 00036 00037 static Random global; 00038 00039 public: // Static methods 00040 00043 DBL getDouble() {return abs(draw())/max();} 00044 00047 DBL jitterFactor(DBL amouth) { 00048 return 1-amouth+2*amouth*getDouble(); 00049 } 00050 00051 00054 Vector3 pointInBox() { 00055 DBL d1 = getDouble(); 00056 DBL d2 = getDouble(); 00057 DBL d3 = getDouble(); 00058 return Vector3(2*d1-1,2*d2-1,2*d3-1); 00059 } 00060 00063 Vector3 pointInSphere() { 00064 Vector3 ret; 00065 do { 00066 ret = pointInBox(); 00067 } while (ret.lengthSq()>1); 00068 return ret; 00069 } 00070 00073 Vector3 direction() { 00074 Vector3 ret; 00075 do{ret = pointInSphere();} while(ret == Vector3(0)); 00076 ret.normalize(); 00077 return ret; 00078 } 00079 00080 private: 00081 unsigned long randx; 00082 static long abs(long x) {return x&0x7fffffff;} 00083 static double max() {return 2147483648.0;} 00084 long draw() {return randx = randx*1103515245 + 12345;} 00085 00086 }; 00087 00088 #endif

Generated on Thu Jan 27 12:16:06 2005 for raytracer.kdevelop by doxygen 1.3.8