POV-Ray : Newsgroups : povray.general : trouble using rand inside of sine function : Re: trouble using rand inside of sine function Server Time
30 Jul 2024 14:18:56 EDT (-0400)
  Re: trouble using rand inside of sine function  
From: Tor Olav Kristensen
Date: 4 Dec 2008 19:07:12
Message: <493870b0$1@news.povray.org>
Tor Olav Kristensen wrote:
> Mike wrote:
>> Hi.
>> I am trying to create a random phase within a sine function, but keep 
>> getting an
>> error that a 'function identifier' is expected and I do not understand 
>> why.
>>
>> I am doing:
>> #declare R1=seed(1153);
>> #declare xspectrum =function{ sin  ( x+  rand(R1)   )    }
> ...
> rand() can not be used inside functions.
> See http://www.povray.org/documentation/view/3.6.1/231/
> 
> Do you really need it to be a function ?
> 
> 
> If so you may want to have a look at f_noise3d(x, y, z) instead.
...

Or you can do something like this:

#version 3.6;

#macro BuildSpectrumFunction(Amplitude, Harmonics, SS)

   function(x) {
     Amplitude*
     (
       0
       #local K = 1;
       #while (K <= Harmonics)
         #local Phase = rand(SS);
         + sin(2*pi*(K*x + Phase))
         #local K = K + 1;
       #end // while
     )
   }

#end // macro BuildSpectrumFunction


#local A = 0.025/2;
#local H = 10;

#declare X_SpectrumFn1 = BuildSpectrumFunction(A, H, seed(65189))
#declare X_SpectrumFn2 = BuildSpectrumFunction(A, H, seed(3520))

#declare SUM1 = X_SpectrumFn1(0.50000);
#debug "\n"
#debug str(SUM1, 0, -1)
#declare SUM1 = X_SpectrumFn1(0.50001);
#debug "\n"
#debug str(SUM1, 0, -1)
#debug "\n\n"

#declare SUM2 = X_SpectrumFn2(0.50000);
#debug "\n"
#debug str(SUM2, 0, -1)
#declare SUM2 = X_SpectrumFn2(0.50001);
#debug "\n"
#debug str(SUM2, 0, -1)
#debug "\n\n"

-- 
Tor Olav
http://subcube.com


Post a reply to this message

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