comp.lang.c
[Top] [All Lists]

Re: need help with arrays

Subject: Re: need help with arrays
From: Ben Bacarisse
Date: Tue, 29 Apr 2008 17:50:54 +0100
Newsgroups: comp.lang.c

pereges <Broli00@xxxxxxxxx> writes:

> Ok, I have some problem with arrays which i want to use for storing
> rays in my ray tracing project. please have a little patience to read.
<snip>
> unsigned long int numberofrays;
>
> so
>
>  incr = sqrt(l * b / numberofrays)
>
>
> now lets say the plane is represented by four corners:
>
> xmin, ymin
> xmax, ymin
> xmin, ymax
> xmax, ymax
>
> all are doubles
>
> My ray data structure is :
>
> typedef struct ray_struct
> {
>
>     double ox, oy, oz;      // ray origin or source represented by the
> 3 x, y, z coordinates
>     double t;     //distance travelled
>     double dx, dy, dz;   // ray direction represented by 3 x, y, z
> coordinates
>
> }ray;
>
> Now, this is what I did to create the list of rays:
>
> ray *ray_list;
>
> ray_list = calloc(sizeof(ray), numberofrays);

Better use malloc.  There is no advantage in zeroing (arithmetically)
the pointers.

> Then,  I try to store the rays:
>
> double xcord, ycord;   // represents the origin of a random ray
> int i = 0;
>
> for(ycord = ymin; ycord <= ymax;  ycord  += incr)
> {
>      for(xcord = xmin; xcord<= xmax; xcord+= incr)
>      {

ray_list[i] does not point anywhere.  You need to either malloc a ray
structure for each one, or use a 2D array in the first place.

>            ray_list[i].ox = x;
>            ray_list[i].oy = y;
>            ray_list[i].oz = 1000;
>            ray_list[i].dx = 0; // all rays parallel to each other and
> travelling in +z direction
>            ray_list[i].dy = 0;
>            ray_list[i].dz  = 1;
>            ray_list[i].t = DBL_MAX;    // rays go till infinity
>            i++;
>      }
>
> }

Also, watch our for you <=.  Take a 3 x 4 rectangle with 48 rays and
incr = 0.5.  If you go <= you get both an extra row and ne more ray
per row:

  X--X--X--X--X--X--X--X--X
  |     |     |     |     |
  X  X  X  X  X  X  X  X  X
  |     |     |     |     |
  X--X--X--X--X--X--X--X--X
  |     |     |     |     |
  X  X  X  X  X  X  X  X  X
  |     |     |     |     |
  X--X--X--X--X--X--X--X--X
  |     |     |     |     |
  X  X  X  X  X  X  X  X  X
  |     |     |     |     |
  X--X--X--X--X--X--X--X--X

That is 63 rays, not 48!

<snip>
> One other problem I have is that in my project, it is possible that a
> ray hits an object and a reflected ray is generated and  this
> reflected ray hits the object again and spawns a new ray. I need it
> for some energy calculations and this allows me to traverse the entire
> optical path of  a ray. I was wondering if it is ok to have child
> pointer to solve this problem :
>
> typedef struct ray_struct
> {
>       ........
>      ........
>      struct ray_struct ray *child;

      struct ray_struct *child;

Yes, that is OK.  But remember to allocate space for it.

> }

> I also want to ask, how useful is it to have doubles in a numerical
> simulation program(high accuracy is desired) ? Or is it ok to use
> floats instead ? Using doubles has really slowed down my program.

There is no general rule.  If you replace all your doubles with a
typedef name you can test the difference when you are done with a
single change.

> Another thing I have noted is that its best to have variables with
> scopes as small as possible. I still wonder why many people prefer to
> use :
>
> extern const double PI  = 3.14
>
> over
>
> #define PI 3.14
>
> shouldn't the second option be more efficient ?

See recent discussion of const.

-- 
Ben.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy