473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with arrays

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.

I need to fire rays from a a rectangular plane. The rays are parallel
to each other so their direction is same but they differ in their
origin points or source location. I tried to determine the source
location by creating a grid of the rectangular plane. The rays are
going to be spaced from each other at some distance lets say incr. The
rectangular plane is going to be divided into small squares each of
length incr.
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(r ay), numberofrays);

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].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++;
}

}

Unfortunately, with this approach I am getting a segfault error.

Also, when I entered the ray number as 20,000 actually there were
20164 rays getting created which means I had crossed the array bounds.
I could store these rays in a link list or a file but I think it is
not a good idea at all. Also, what I could do is ask the user to enter
incr first and based on that apply the above loop to calculate the
number of rays and then using that number i allocate an array of rays
and then add data sequentially. This approach is seeming too naive to
me and also does not allow user to enter number of rays.

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;

}

IF a ray does not intersect the object then it spawns no rays and
child is set to NULL
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.
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 ?
Jun 27 '08 #1
6 1550
sorry l and b are the length and breadth of the rectangular plane.
Jun 27 '08 #2
pereges <Br*****@gmail. comwrites:
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(r ay), 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.
Jun 27 '08 #3
On Apr 29, 9:50 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
<snip>
Better use malloc. There is no advantage in zeroing (arithmetically )
the pointers.
How is malloc any different ? Both will achieve same purpose ?

<snip>
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!

I have changed my approach a little bit for generating points on the
grid.

/*************** *************** *************** *************** *****/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
long int numpoints;
printf("Enter num of points\n");
scanf("%d", &numpoints);
long int numpointsx = sqrt((double)nu mpoints); //num of points
along x
long int numpointsy = sqrt((double)nu mpoints); // num of
points along y
numpoints = numpointsx * numpointsy; // total no of points for
which i will allocate memory
int i = 0;
typedef struct point //temproray ds for point (just experimenting)
{
float x, y;

} point;
point *pointinarray;

pointinarray = calloc(sizeof(p oint), numpoints); //new array with
numpoints
float xlength, ylength; //length and breadth
xlength = ylength = 20;
float xmin, ymin;
xmin = ymin = -10;
float xmax, ymax;
xmax = ymax = 10;
float xsize = xlength / numpointsx; //calculating increments
along x and y
float ysize = ylength / numpointsy;

for (int yi = 0; yi < numpointsy; ++yi)
{
for (int xi = 0; xi < numpointsx; ++xi)
{
pointinarray[i].x = xmin + xi * xsize;
pointinarray[i].y = ymin + yi * ysize;
++i;
}
}

printf("i: %d xmax: %f ymax: %f\n",i, pointinarray[i-1].x,
pointinarray[i-1].y);
return 0;

}

/
*************** *************** *************** *************** *************** **************/
With this approach there seems to be no problem except 1.

I checked out the last entry of pointsinarray[] which should
correspond to xmax and ymax (10 and 10 respectively) and the values i
get are:

9.83509
9.83509

You can verify by running the above program yourself and checking o/p.

Also, I noticed that when I entered 14000 as number of rays, The total
number of rays actually taken into consideration for calculation of
grid were only 13924. This problem arises when number of rays is not a
perfect square.
i believe problems are occuring because numpointsx and numpointsy are
getting truncated to smaller values. For eg. in c, the integer square
root of 5 is 2. I think if I can tweak it to some higher integer
value, problem will be solved. I believe there is a function in math.h
which does that.
Jun 27 '08 #4
Ok solved.

float xsize = xlength / (numpointsx - 1);
float ysize = ylength / (numpointsy - 1);

and use <= in both loops.

With this approach, now i have extra points:

For eg. numpoints = 14000 //entered by user

actual numpoints = 14161 //161 extra

xmax = 10.170940 ( actual xmax)
ymax = 10.170940 ( actual ymax)

but i don't mind really

what i will do is that i am doing actual calculations, i will simply
not consider points where:

x xmax || y ymax

x or y cannot be less than xmin, ymin anyway

Jun 27 '08 #5
pereges <Br*****@gmail. comwrites:
On Apr 29, 9:50 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
<snip>
>Better use malloc. There is no advantage in zeroing (arithmetically )
the pointers.

How is malloc any different ? Both will achieve same purpose ?
There was no point in zeroing the array, that is all. It is sometimes
useful and sometimes not.

--
Ben.
Jun 27 '08 #6
On Tue, 29 Apr 2008 10:20:23 -0700 (PDT), pereges <Br*****@gmail. com>
wrote:

snip 30+ obsolete lines
>I have changed my approach a little bit for generating points on the
grid.
Then please don't quote irrelevant material.
>
/*************** *************** *************** *************** *****/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
long int numpoints;
printf("Enter num of points\n");
scanf("%d", &numpoints);
This invokes undefined behavior. The %d promises scanf that the
corresponding argument will be an int*. It is obviously a long int*.
The fact that it appears to work on your system is just bad luck.
long int numpointsx = sqrt((double)nu mpoints); //num of points
along x
The cast serves no purpose.

If you want people to test your code, then this is one reason why you
should not use // style comments.
long int numpointsy = sqrt((double)nu mpoints); // num of
points along y
numpoints = numpointsx * numpointsy; // total no of points for
which i will allocate memory
int i = 0;
Unless you have C99, declarations and definitions need to precede
executable statements.
> typedef struct point //temproray ds for point (just experimenting)
{
float x, y;

} point;
point *pointinarray;

pointinarray = calloc(sizeof(p oint), numpoints); //new array with
numpoints
float xlength, ylength; //length and breadth
xlength = ylength = 20;
float xmin, ymin;
xmin = ymin = -10;
float xmax, ymax;
xmax = ymax = 10;
float xsize = xlength / numpointsx; //calculating increments
along x and y
float ysize = ylength / numpointsy;

for (int yi = 0; yi < numpointsy; ++yi)
Defining a variable this way is an extension.
{
for (int xi = 0; xi < numpointsx; ++xi)
{
pointinarray[i].x = xmin + xi * xsize;
pointinarray[i].y = ymin + yi * ysize;
++i;
}
}

printf("i: %d xmax: %f ymax: %f\n",i, pointinarray[i-1].x,
pointinarray[i-1].y);
return 0;

}

/
************** *************** *************** *************** *************** ***************/
With this approach there seems to be no problem except 1.

I checked out the last entry of pointsinarray[] which should
correspond to xmax and ymax (10 and 10 respectively) and the values i
get are:

9.83509
What value did you enter for numpoints?
>9.83509

You can verify by running the above program yourself and checking o/p.

Also, I noticed that when I entered 14000 as number of rays, The total
number of rays actually taken into consideration for calculation of
grid were only 13924. This problem arises when number of rays is not a
perfect square.
i believe problems are occuring because numpointsx and numpointsy are
getting truncated to smaller values. For eg. in c, the integer square
root of 5 is 2. I think if I can tweak it to some higher integer
value, problem will be solved. I believe there is a function in math.h
which does that.
If you use an integer higher that the sqrt you will have the same
problem but in the opposite direction. WHY are you converting the
sqrt to integer? The only way to get a square grid with the number of
points you specify is to specify a perfect square.
Remove del for email
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6763
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
41
3979
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it the way i am trying to. Could some one please tell me what i am doing wrong, or another way of doing the same thing. i am trying to use the variable like this.
2
1637
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something about parameter passing. If someone give me comment, it will be thankful. thankyou very much
1
1481
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need to reset the array. The program would run faster if I don't need to reset. ------------------------------------------ int Array(void) .................
2
3363
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
8
1604
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of type strings (containing 20 elements). c) One of type char (containing 30 elements).
23
2556
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
1
3335
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells). Here is the exact problem specifications: Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
6
2669
by: junk | last post by:
Senerio: I have a custom user control which contains two control arrays. The user control has a group box in which the two control arrays are dynamically built. The two control arrays are RadioButtons and PictureBoxes. Task: What I need to accomplish with this custom user control is to rearrange the pictures in the PictureBox Control Array based on what RadioButton is selected. I do not want my RadioButton Control array to have a...
0
833
by: sumalats | last post by:
Hello, I need to use Visual basic to acquire some data through the serial port, store it etc. As I am just getting acquainted with VB6 I need some help. My Controller board (8 bit micro) sends out serial data 3 bytes at a time ( a packet). The data are typically binary bytes and can have values from 0 to 255. To signal the start of a set of bytes the CTS line is given a high. The data set is sent at intervals of 35 milliseconds. I need to...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.