Mail Archives: djgpp/1998/07/11/12:56:00
Message-ID: | <001501bdacec$9b72e720$604e08c3@arthur>
|
From: | "Arthur" <arfa AT clara DOT net>
|
To: | "DJGPP Mailing List" <djgpp AT delorie DOT com>
|
Subject: | Re: About ray casting and look up tables
|
Date: | Sat, 11 Jul 1998 17:54:43 +0100
|
MIME-Version: | 1.0
|
>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 <math.h>
>float tan_table[320];
>float rad;
>for(ang=0;ang<320;ang++)
>{
> rad= ang *(pi/180);
> tan_table[ang]=tan(rad);
>}
>
>but i was cheking out my friend's code and found
>
>
>#include <math.h>
>#define ANG_360 1920
>#define ANG_0 0
>#define PI 3.1415927
>#define TWOPI ( 2 * PI)
>#define TWOPIDIV360 ( TWOPI / ANG_360)
>
>float *tan_table;
>
>tan_table = (float *) malloc( sizeof(float) * ANG_360);
>
>for(angulo=ANG_0;angulo<=ANG_360;angulo++)
>
>{
>
> ang_rad = ang * TWOPIDIV360 ;
>
> tan_table[ang] = (float)tan(ang_rad);
>
>}
>
>i have no problem with malloc and c stuff
>
>I don't understand why he uses this formula
>
> ang_rad = ang * TWOPIDIV360 ;
>insted of the one i'm using
OK, so you are using ang*(pi/180). This converts degrees to radians. It
follows that ang*(pi/180) is the same as ang*(2*pi/360) (basic mathematic
manipulation). So obviously, he has defined a macro which is equivalent to
this.
He's defined pi "manually" instead of the definition in math.h:
#define PI 3.1415927
And also 2*pi:
#define TWOPI ( 2 * PI)
And instead of 360 degrees, he's using a higher number of increments:
#define ANG_360 1920
(this is explained in other postings).
And also 2*pi/1920:
#define TWOPIDIV360 ( TWOPI / ANG_360)
so using TWOPIDIV360 is the same as using 2*pi/1920
Advantages of doing it this way? Well, the macroes are expanded in full in
the code so TWOPIDIV360 will be compiled as: 2*3.1415927/1920 which will
give marginally smaller and quicker code, but also he obviously thinks it's
easier to read this way. I disagree, but everyone has their own opinion :^)
HTH
James Arthur
jaa AT arfa DOT clara DOT net
- Raw text -