POV-Ray : Newsgroups : povray.general : Array Problem Server Time
14 Nov 2024 21:14:35 EST (-0500)
  Array Problem (Message 1 to 4 of 4)  
From: The Ugly
Subject: Array Problem
Date: 4 Feb 2005 05:35:00
Message: <web.42034f671f35a84955f15cf90@news.povray.org>
Hi. Im trying to code a macro that creates a heightfield uning some sort of
recursive algorithm. I've searched the internet for a suitable algorithm
that I could convert to SDL (suitable=simple enough for me to understand).
A came across something named the Diamond-Square Algorithm (aka Midpoint
Displacement Algorithm). I got the general idea of the algorithm and
started to convert it to the SDL. I now have a file containing the code,
its still not finished, it is very inefficient, but I think the main things
are done.
The problem is...it doesnt work.

Here's the code:



//BEGIN CODE

#macro tilenum(num)

#while (num<0 & num>Size)
#if(num>Size)
(num-Size)
#end
#if(num<Size)
(num+Size)
#end
#end

#end


#macro Diamond(Sub_Size,Point_X,Point_Z)
#declare Array[Point_X][Point_Z]=(

(Array[tilenum(Point_X-(Sub_Size/2))][tilenum(Point_Z-(Sub_Size/2))])+

(Array[tilenum(Point_X+(Sub_Size/2))][tilenum(Point_Z-(Sub_Size/2))])+

(Array[tilenum(Point_X+(Sub_Size/2))][tilenum(Point_Z+(Sub_Size/2))])+

(Array[tilenum(Point_X-(Sub_Size/2))][tilenum(Point_Z+(Sub_Size/2))])

)

/4+

(rand(Rand)*(RandVal*2)-RandVal);

#end


#macro Square(Sub_Size,Point_X,Point_Z)
#declare Array[Point_X][Point_Z]=(

(Array[Point_X][tilenum(Point_Z-(Sub_Size/2))])+

(Array[tilenum(Point_X+(Sub_Size/2))][Point_Z])+

(Array[Point_X][tilenum(Point_Z-(Sub_Size/2))])+

(Array[tilenum(Point_X-(Sub_Size/2))][Point_Z])

)

/4+

(rand(Rand)*(RandVal*2)-RandVal);

#end


#macro MainLoop(Sub_Size)

#declare N1=0;
#declare N2=0;
#while (N2<Size)

#declare D_X=N1+(Sub_Size/2);
#declare D_Z=N2+(Sub_Size/2);

Diamond(Sub_Size,D_X,D_Z)

Square(Sub_Size,D_X,D_Z-(Sub_Size/2))
Square(Sub_Size,D_X+(Sub_Size/2),D_Z)
Square(Sub_Size,D_X,D_Z+(Sub_Size/2))
Square(Sub_Size,D_X-(Sub_Size/2),D_Z)


#declare N1=N1+Sub_Size;
#if (N1>=Size)
#declare N1=0;
#declare N2=N2+Sub_Size;
#end
#end


#declare RandVal=RandVal/2;
#declare Int_Val=Int_Val+1;
#if(Int_Val<=Int)
MainLoop(Sub_Size/2)
#end

#end


#macro FractalTerrain(Size,BaseHeight,Seed,Int,RandVal)

#declare Array=array[Size+1][Size+1]

#declare Array[0][0]=BaseHeight;
#declare Array[0][Size]=BaseHeight;
#declare Array[Size][0]=BaseHeight;
#declare Array[Size][Size]=BaseHeight;
#declare Rand=seed(Seed);
#declare Size=Size;
#declare RandVal=RandVal;

#declare Int=0;
MainLoop(Size)

#end

FractalTerrain(4,1,1,2,1)

//END CODE



I get the error "Parse Error: Expected 'numeric expression', ] found
instead"
Cant you use a macro when reffering to arrays?
Any comment on this would be appreciated.


Oh, I almost forgot to thank Thorsten, Shay and Mike for the help with the
"function macro" (long time ago).
It turned out quite good, thanks.


/The Ugly


Post a reply to this message

From: The Ugly
Subject: Re: Array Problem
Date: 4 Feb 2005 05:50:01
Message: <web.4203520f625187b655f15cf90@news.povray.org>
30 minutes after writing the message I saw how many faults there were in the
code..no wonder it didnt work as I expected...
My question still stands though. Is there something wrong with the Diamond
part of the code, because I cant find it..

thanks in advance


Post a reply to this message

From: Tim Nikias
Subject: Re: Array Problem
Date: 4 Feb 2005 05:55:44
Message: <420354b0@news.povray.org>
> #macro tilenum(num)
>
> #while (num<0 & num>Size)
> #if(num>Size)
> (num-Size)
> #end
> #if(num<Size)
> (num+Size)
> #end
> #end
>
> #end

I guess the problem is in the above quoted macro. I've often found that it
is better to do it like this:

#macro tilenum(num)
#local Temp=num;
#while (Temp<0 & Temp>Size)
#if (Temp>Size) #local Temp=Temp-Size; #end
#if (Temp<0) #local Temp=Temp+Size; #end
#end
Temp
#end

This way, a local value get's returned. I'm not sure if your code is
altogether correct, as your running a whilte-loop and evaluating it by hand
would come up with something like
"(num-Size)(num-Size)..." or "(num+Size)(num+Size)..." depending if num is
smaller than 0 or larger than Size.

I haven't tested it, but the idea is generally to use a local variable and
return that instead of returning a value inside any kind of loop or if.

Regards,
Tim

-- 
"Tim Nikias v2.0"
Homepage: <http://www.nolights.de>


Post a reply to this message

From: The Ugly
Subject: Re: Array Problem
Date: 7 Feb 2005 15:00:01
Message: <web.4207c749625187b655f15cf90@news.povray.org>
I think Im finished coding. The code can be found at:

http://news.povray.org/povray.binaries.scene-files/thread/<web.4207c6a44f337e1155f15cf90@news.povray.org>

/The Ugly


Post a reply to this message

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