J
Jacob
I just wrote a function that draws a fractal by recursively calling
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:
void draw_series(SDL_Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<float> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];
px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];
if(depth){
draw_series(img, px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);
}
}
}
The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new". It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.
Does anyone know what would cause this?
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:
void draw_series(SDL_Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<float> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];
px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];
if(depth){
draw_series(img, px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);
}
}
}
The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new". It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.
Does anyone know what would cause this?