POV-Ray : Newsgroups : povray.general : Array of points for use with bicubic patch : Re: Array of points for use with bicubic patch Server Time
29 Jul 2024 20:21:46 EDT (-0400)
  Re: Array of points for use with bicubic patch  
From: Alain
Date: 5 May 2011 14:18:53
Message: <4dc2ea0d@news.povray.org>

> Warp<war### [at] tagpovrayorg>  wrote:
>> D103<nomail@nomail>  wrote:
>>> #declare X1=-1.5;  //in the scene file these are equal to the v_length of the
>>> appropriate values.
>>> #declare Z1=-1.5;
>>> #declare Cnt=0;
>>
>>> #while(X1<=1.5)
>>>    #while(Z1<=1.5)
>>>      #if(Cnt>=15)
>>>        #debug "Cnt = illegal value.\n"
>>>      #else
>>>        #debug "Cnt is within legal range.\n"
>>>      #end
>>>      //#declare Pts[Cnt]=<X1, 0, Z1>;
>>>      sphere { 0, 0.1 pigment { White } translate<X1, 0, Z1>  }
>>>      #declare Z1=Z1+1;
>>>      #declare Cnt=Cnt+1;
>>>    #end
>>>    #declare X1=X1+1;
>>>    #declare Z1=-1.5;
>>> #end
>>
>>    You seem to have very complicated ways of performing nested loops.
>> What's wrong with the traditional way of doing nested loops? In other
>> words:
>>
>> #declare Cnt = 0;
>> #declare X1 = -1.5;
>> #while(X1<= 1.5)
>>    #declare Z1 = -1.5;
>>    #while(Z1<= 1.5)
>>
>>      ...
>>
>>      #declare Z1 = Z1 + 1;
>>      #declare Cnt = Cnt + 1;
>>    #end
>>    #declare X1 = X1 + 1;
>> #end
>>
>> --
>>                                                            - Warp
>
> Sorry, afraid I'm in the dark here. Perhaps you could explain in a bit more
> detail? I've read the help files on nested loops, maybe there's something I
> missed or misunderstood.
>
> Thank you very much.
>
> D103
>
>

When making nested loops, it's normaly beter to initiate any inner loop 
counter just before it's #while statement.

You initiate your counter first outside the outer loop. Then, you 
reinitiate that counter for the next iteration after the #end of the 
loop and after you increment the outer loop's counter.

The usual way is:
Initiate the outer loop's counter.
Start the outer loop.
possibly do some stuff.

Initiate the iner loop's counter.
Start the inner loop.
Do the iner loop's stuf and counter incrementation.
Close the iner loop.

possibly do some other outer loop stuff.
Increment the outer loop's counter.
Close the outer loop.

This is much cleaner as the iner loop is initiated just before the place 
where it's used.
You only need to initialise the iner loop in one place.
You can use a #local variable for the iner loop instead of one that is 
global. It reduces the risks of name collision and undesirable side effects.



Alain


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.