|
-
Jun 8th, 2009, 05:44 PM
#1
Thread Starter
Addicted Member
Help needed writing recursive function
Hi ,
I need help writing a recursive function from an algorithm called "The Diamond-Square Algorithm" @
[http://gameprogrammer.com/fractal.html#diamond]
So far I have this:
Code:
Function DiamondSquare(ByVal PointsColl As Point3DCollection) As Point3DCollection
'Variables
Dim SquareSize As Integer = Sqrt(PointsColl.Count)
Dim RandomRange As Integer = 5
Dim Rand As New Random(1234)
'Diamond Step
Dim SqrCorner1 As Integer = 1
Dim SqrCorner2 As Integer = SquareSize
Dim SqrCorner3 As Integer = (SquareSize - 1) * SquareSize + 1
Dim SqrCorner4 As Integer = SquareSize * SquareSize
Dim SqrCentre As Integer = (SquareSize / 2) ^ 2
'Assign Diamond Centre value
Dim SqrCentreValue As Double = PointsColl(SqrCorner1).Y + PointsColl(SqrCorner2).Y + PointsColl(SqrCorner3).Y + PointsColl(SqrCorner4).Y + Rand.Next(0, RandomRange)
PointsColl(SqrCentre) = New Point3D(PointsColl(SqrCentre).X, SqrCentreValue, PointsColl(SqrCentre).Z)
'Square Step
Dim DiamCorner1 As Integer = SquareSize / 2
Dim DiamCorner2 As Integer = DiamCorner1 + SquareSize
Dim DiamCorner3 As Integer = DiamCorner2 + SquareSize
Dim DiamCorner4 As Integer = DiamCorner3 + SquareSize
'Assign Diamond Centre Values to Diamond Corners
PointsColl(DiamCorner1) = New Point3D(PointsColl(DiamCorner1).X, SqrCentreValue, PointsColl(DiamCorner1).Z)
PointsColl(DiamCorner2) = New Point3D(PointsColl(DiamCorner2).X, SqrCentreValue, PointsColl(DiamCorner2).Z)
PointsColl(DiamCorner3) = New Point3D(PointsColl(DiamCorner3).X, SqrCentreValue, PointsColl(DiamCorner3).Z)
PointsColl(DiamCorner4) = New Point3D(PointsColl(DiamCorner4).X, SqrCentreValue, PointsColl(DiamCorner4).Z)
'Split into Four Squares
Dim Square1 As Point3DCollection '((SquareSize/2)-1)
Dim Square2 As Point3DCollection '((SquareSize/2)-1)
Dim Square3 As Point3DCollection '((SquareSize/2)-1)
Dim Square4 As Point3DCollection '((SquareSize/2)-1)
'Populate Square 1
Dim Index As Integer = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
Square1(Index) = PointsColl((i * SquareSize) + j)
Index = Index + 1
Next
Next
'Populate Square 2
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
Square2(Index) = PointsColl((i * SquareSize) + j + (SquareSize / 2))
Index = Index + 1
Next
Next
'Populate Square 3
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
Square3(Index) = PointsColl(((i + SquareSize / 2) * SquareSize) + j)
Index = Index + 1
Next
Next
'Populate Square 4
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
Square4(Index) = PointsColl(((i + SquareSize / 2) * SquareSize) + j + (SquareSize / 2))
Index = Index + 1
Next
Next
'Call function for each Quarter Square
Square1 = DiamondSquare(Square1)
Square2 = DiamondSquare(Square2)
Square3 = DiamondSquare(Square3)
Square4 = DiamondSquare(Square4)
'Replace Square Values in PointsColl
'Retrieve Square1
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
PointsColl((i * SquareSize) + j) = Square1(Index)
Index = Index + 1
Next
Next
'Retrieve Square2
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
PointsColl((i * SquareSize) + j + (SquareSize / 2)) = Square1(Index)
Index = Index + 1
Next
Next
'Retrieve Square3
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
PointsColl(((i + SquareSize / 2) * SquareSize) + j) = Square1(Index)
Index = Index + 1
Next
Next
'Retrieve Square4
Index = 0
For i = 0 To (SquareSize / 2) - 1
For j = 0 To (SquareSize / 2) - 1
PointsColl(((i + SquareSize / 2) * SquareSize) + j + (SquareSize / 2)) = Square1(Index)
Index = Index + 1
Next
Next
End Function
-
Jun 8th, 2009, 05:52 PM
#2
Re: Help needed writing recursive function
What help do you need?
That link has C source code available, are you trying to convert that to VB?
-
Jun 8th, 2009, 05:57 PM
#3
Thread Starter
Addicted Member
Re: Help needed writing recursive function
Sorry. I didnt notice that. I cant seem to find a link to the source code.
-
Jun 8th, 2009, 06:00 PM
#4
Thread Starter
Addicted Member
Re: Help needed writing recursive function
Found the source - www.math.umd.edu/~asnowden/dmnd-sqr.html
I would like to implement a VB.NET version.
-
Jun 8th, 2009, 06:23 PM
#5
Re: Help needed writing recursive function
The source on that page is in a zip at http://gameprogrammer.com/fractal/fractal.zip but the link you show above is a better starting point.
I dont know of a converter so I'm afraid its a matter of either converting it line by line manually or looking at the algorithm and starting afresh in .NET. The algorithm seems to be quite well documented so I'd probably plump for coding it from scratch.
-
Jun 8th, 2009, 06:33 PM
#6
Thread Starter
Addicted Member
Re: Help needed writing recursive function
I have almost finished it in VB.NET - my attempt is posted above. Im just stuck on the ending.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|