Results 1 to 7 of 7

Thread: C conversion problem

  1. #1

    C conversion problem

    I've been trying to covert some C code to Pascal, but I'm not sure if I got it right, at least the results seem to be completely wrong.

    What can you make out of this?
    Code:
    /*
    * Heterogeneous procedural terrain function: stats by altitude method.
    * Evaluated at  point; returns value stored in  value.
    *
    * Parameters:
    *  H determines the fractal dimension of the roughest areas
    *  lacunarity is the gap between successive frequencies
    *  octaves is the number of frequencies in the fBm
    *  offset raises the terrain from  sea level
    */
    double
    Hetero_Terrain( Vector point,
    double H, double lacunarity, double octaves, double offset )
    {
    	double value, increment, frequency, remainder, Noise3();
    	int i;
    	static int first = TRUE;
    	static double *exponent_array;
    	/* precompute and store spectral weights, for efficiency */
    	if ( first ) {
    		/* seize required memory for exponent_array */
    		exponent_array = (double *)malloc( (octaves+1) * sizeof(double) );
    		frequency = 1.0;
    		for &#40;i=0; i<=octaves; i++&#41; &#123;
    			/* compute weight for each frequency */
    			exponent_array&#91;i&#93; = pow&#40; frequency, -H &#41;;
    			frequency *= lacunarity;
    		&#125;
    		first = FALSE;
    	&#125;
    	/* first unscaled octave of function; later octaves are scaled */
    	value = offset + Noise3&#40; point &#41;; point.x *= lacunarity;
    	point.y *= lacunarity; point.z *= lacunarity;
    	/* spectral construction inner loop, where the fractal is built */
    	for &#40;1=1; i<octaves; 1++&#41; &#123;
    		/* obtain displaced noise value */
    		increment = Noise3&#40; point &#41; + offset;
    		/* scale amplitude appropriately for this frequency */
    		increment *= exponent_array&#91;i&#93;;
    		/* scale increment by current  altitude of function */
    		increment *= value;
    		/* add increment to  value */
    		value += increment;
    		/* raise spatial frequency */
    		point.x *= lacunarity;
    		point.y *= lacunarity;
    		point.z *= lacunarity;
    	&#125; /* for */
    	/* take care of remainder in  octaves */
    	remainder = octaves - &#40;int&#41;octaves;
    	if &#40; remainder &#41; &#123;
    		/*  i and spatial freq. are preset in loop above */
    		/* note that the main loop code is made shorter here */
    		/* you may want to make that loop more like this */
    		increment = &#40;Noise3&#40; point &#41; + offset&#41; * exponent_array&#91;i&#93;;
    		value += remainder * increment * value;
    	&#125;
    	return&#40; value &#41;;
    &#125; /* Hetero_Terrain&#40;&#41; */
    I've converted the exponent_array table to a variable inside the class, so that's not a problem. The problem is with the variable i and that strange for (1=1; i<octaves; 1++) loop. For all that I know i is UNDEFINED when the function is executed, that should be the case with the C version too, except after that static initialization that is done when the function is called for the first time.

    I just don't understand the relation of that inner loop to variable i... So how do I convert this properly?
    If you develop an idiot proof system, the nature develops better idiots.

  2. #2

    C conversion problem

    Maybe it's silly what I'll say now, but it looks like it's an inifite loop. I'm not sure, just guessing.

  3. #3

    C conversion problem

    Yep, that's what it looks like to me too, but how it's exited? The variable i is not incremented inside the loop, so how can the i < octaves ever be true?

    EDIT: I converted that so that I set i to 0 and while i < octaves I increase it's value. I'm not sure if the function works as it should though...
    If you develop an idiot proof system, the nature develops better idiots.

  4. #4

    C conversion problem

    Here's your problem, first line:
    Code:
       for &#40;1=1; i<octaves; 1++&#41; &#123;
          /* obtain displaced noise value */
          increment = Noise3&#40; point &#41; + offset;
          /* scale amplitude appropriately for this frequency */
          increment *= exponent_array&#91;i&#93;;
          /* scale increment by current  altitude of function */
          increment *= value;
          /* add increment to  value */
          value += increment;
          /* raise spatial frequency */
          point.x *= lacunarity;
          point.y *= lacunarity;
          point.z *= lacunarity;
       &#125; /* for */
    Let me write that below this with bold so you can see what is wrong...
    for (1=1; i<octaves; 1++) {
    Those should be i, not 1.

  5. #5

    C conversion problem

    I think vgo is saying the original C source code was like that.

    I would agree though that the 1=1...1++ needs to be i.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  6. #6

    C conversion problem

    Quote Originally Posted by technomage
    I think vgo is saying the original C source code was like that.
    But it doesn't mean the code isn't flawed. No break, return, or even continue in there so that '1' mix up must be wrong.

  7. #7

    C conversion problem

    Yeah, it must be a typo. I was very confused indeed when I stumbled on that piece of code, it just didn't make any sense. Not that I already don't have fractals coming out of my ears!

    I need a break...
    If you develop an idiot proof system, the nature develops better idiots.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •