Newsgroups: comp.os.msdos.djgpp From: tob AT world DOT std DOT com ("Tom Breton") Subject: Re: About ray casting and look up tables Message-ID: Sender: root AT pc Organization: The World Public Access UNIX, Brookline, MA References: <35A62FBB DOT AF821E92 AT cue DOT satnet DOT net> Date: Fri, 10 Jul 1998 22:21:30 GMT Lines: 70 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk "Ronald Patiņo G" writes: > > Hi > > I'm learning how to make a ray casting engine, i'm using > djgpp, and a friend of mine sent me some source code, but > i have a problem with look up tables. > > I usually calculate them in this way > > #include > float tan_table[320]; > float rad; > for(ang=0;ang<320;ang++) > { > rad= ang *(pi/180); That seems to come out uneven. IE, you haven't filled the whole 360 degrees, tho you've filled the whole table. > tan_table[ang]=tan(rad); > } > > but i was cheking out my friend's code and found Your friend writes nice code. > > #include > #define ANG_360 1920 You seem to be expecting the same number of elements in the table as degrees in an angle, but it hain't neccessarily so. ANG_360 is the *number of array elements* in a *360* degree circle, which is the whole array. > #define ANG_0 0 > #define PI 3.1415927 > #define TWOPI ( 2 * PI) > #define TWOPIDIV360 ( TWOPI / ANG_360) Here he defines his conversion ratio. Your friend seems to like to write his stuff out explicitly, a habit I recommend. > float *tan_table; > > tan_table = (float *) malloc( sizeof(float) * ANG_360); For stuff like this, I like to have an extra entry so that tan_table[ ANG_360 ] == tan_table[ 0 ], because that way there's always a rounded-up element and a rounded-down element, which saves some tests. > > for(angulo=ANG_0;angulo<=ANG_360;angulo++) > > { > > ang_rad = ang * TWOPIDIV360 ; > > tan_table[ang] = (float)tan(ang_rad); > > } > That's pretty much the same as you did. He's done the conversion separately, and (properly) cast the result of tan (). Tom