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? 1 2331
Jacob wrote: 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++){
Why "- 1"?
px1 = x1 + (x2 - x1) * pointlist->value[n]; py1 = y1 + (y2 - y1) * pointlist->value[n]; px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
When n == pointlist->xsize - 2, i.e. at the last iteration, you're adding
a whole 'pointlist->xsize' to it. What element does it represent? Does
your "matrix" have the element [pointlist->xsize*2 - 2]?
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);
What does 'gfx_line' do, anything relevant?
} }
}
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.
With all due respect, seeing "what the code looked like" is pointless
unless we can understand what it _did_, and it's impossible without the
definition of your MAT_matrix template.
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".
Here is more reason to see what your MAT_matrix looks like. Do you manage
the dynamic memory correctly? Passing by value uses the copy-constructor,
did you define it properly? Do you know of the "Rule of Three"? Did you
follow it?
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?
Bad memory management for one.
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Glenn Venzke |
last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an
enum. Is it possible to pass the item name without the enum name in your
calling statement?
EXAMPLE:
public enum...
|
by: ckerns |
last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0).
If value is null or non-numeric I want to assign the value of "0".
rowString = "L411" //conrol name
if (isNaN(eval...
|
by: Pieter Linden |
last post by:
Hi,
Hopefully it's just me missing something obvious (but at 2AM, not much
is obvious, I guess...) I have a table of courses that people have
taken (StudentID,CourseID) and the user selects a...
|
by: Javier Campos |
last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-)
I can reformat a non-HTML post if you want me to (and...
|
by: Ashish |
last post by:
Hi All,
I have a server runnable textbox control. I also have a server side button
object but I hook up javascript to it to show a modal dialog. Upon return it
places the value in the textbox...
| |
by: summer00 |
last post by:
Hi everyone,
I found that the value of a variable(string type for example) is lost
after the aspx page postback.
E.G:
private void Page_Load(object sender, System.EventArgs e)
{
|
by: catweezle2010 |
last post by:
Hello NG,
I have three files (default.aspx, search.aspx and work.aspx). The way
is: login on default (if session is newsession). The loginname I write
into as sessionvariable (username). So I...
|
by: alanwo |
last post by:
Hi Experts,
Interesting finding, when comparing two dictionary of byte(),
KeyNotFoundException throwed but, that byte() key is present in another
dictionary. Is that the limitation of comparison...
|
by: vikas251074 |
last post by:
I am using Oracle 9i and ASP
I have empno, empname, designation, category, dob.
Category have two option 'S' or 'O'
When I enter empno, empname, designation, category and dob
and when I...
|
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,...
|
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,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |