# Help With Pseudo Coded Algorithm for The Diamond-Square Algorithm

## Homework Statement

I am trying to write a pseudocoded recursive algorithm.

Algorithm is located @: http://gameprogrammer.com/fractal.html#diamond"

I am getting the ending wrong.

## Homework Equations

Do diamond step.
Do square step.
Reduce random number range.
Call myself four times.

## The Attempt at a Solution

My attempt:

Function DiamondSquare(PointsColl as Points3DCollection) Points3DCollection

'Variables

Dim SquareSize as Integer = PointColl.Length

if SquareSize > 1 then

Dim RandomRange as Integer = 5

Dim Rand as 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

SqrCentreValue = PointsColl(SqrCorner1) + PointsColl(SqrCorner2) + PointsColl(SqrCorner3) + PointsColl(SqrCorner4) + 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(DiamondCorner1) = SqrCentreValue

PointsColl(DiamondCorner2) = SqrCentreValue

PointsColl(DiamondCorner3) = SqrCentreValue

PointsColl(DiamondCorner4) = SqrCentreValue

'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
Dim Index as integer = 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
Dim Index as integer = 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
Dim Index as integer = 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

Dim Square1 as Point3DCollection = DiamondSquare(Square1)

Dim Square2 as Point3DCollection = DiamondSquare(Square2)

Dim Square3 as Point3DCollection = DiamondSquare(Square3)

Dim Square4 as Point3DCollection = DiamondSquare(Square4)

Else

'Replace Square Values in PointsColl

'Retrieve Square1

Dim index as integer = 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

Dim index as integer = 0

for i = 0 to (SquareSize / 2) - 1

for j = 0 to (SquareSize / 2) - 1

PointsColl((i * SquareSize) + j + (SquareSize / 2)) = Square1(Index)

next

next

'Retrieve Square3

Dim index as integer = 0

for i = 0 to (SquareSize / 2) - 1

for j = 0 to (SquareSize / 2) - 1

PointsColl(((i + SquareSize / 2)* SquareSize) + j) = Square1(Index)

next

next

'Retrieve Square4

Dim index as integer = 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)

next

next

End Function

## The Attempt at a Solution

Last edited by a moderator: