Help With Pseudo Coded Algorithm for The Diamond-Square Algorithm

  • Thread starter quddusaliquddus
  • Start date
  • Tags
    Algorithm
In summary, the conversation was about a pseudocoded recursive algorithm for creating fractals, with a specific focus on the Diamond-Square algorithm. The output included a summary of the algorithm with suggestions for improvements, such as using a more efficient method for populating the squares and adding a base case to prevent infinite looping.
  • #1
quddusaliquddus
354
2

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.


[From link above]

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
 
Last edited by a moderator:
Physics news on Phys.org
  • #2


To improve this algorithm, I would suggest the following changes:

1. Use a more efficient way to populate the four squares. Instead of using nested for loops, you could use a single for loop and calculate the indices using simple arithmetic operations. This will save you some time and also make the code more readable.

2. Instead of creating a new Point3DCollection for each quarter square, you could use the original PointsColl and just update the values for the four corners. This will save memory and improve the performance of the algorithm.

3. Add a base case condition to the recursive function. Currently, the function will keep calling itself until the square size is equal to 1. This can lead to an infinite loop if the initial square size is not divisible by 2. Adding a base case will ensure that the function stops recursing when the square size is small enough.

4. Consider using a different random number generator. The current algorithm uses the same seed value (1234) for each recursion, which can lead to similar patterns in the resulting fractal. Using a different seed value for each recursion will result in a more diverse and interesting fractal.

5. Add error handling to the function. The current algorithm assumes that the input PointsColl is a valid collection with a length that is a power of 2. However, this may not always be the case. Adding error handling will make the function more robust and prevent unexpected errors.

Overall, your algorithm looks good and is on the right track. With these improvements, it will be more efficient, readable, and robust.
 
  • #3



Your algorithm looks like a good start. It seems like you have a clear understanding of the steps involved in the diamond-square algorithm. However, there are a few things that could be improved or clarified.

First, it might be helpful to include comments throughout your code to explain what each section is doing. This will make it easier for others (and yourself) to understand and debug the algorithm.

Second, in the "Do diamond step" and "Do square step" sections, it might be helpful to explicitly state what you are doing in each step. For example, in the diamond step, you are finding the center value of the diamond and assigning it to the center point. In the square step, you are finding the corners of the diamond and assigning the center value to them. This will make it easier to follow your logic.

Third, in the "Reduce random number range" section, it's not clear what you are trying to achieve. It might be helpful to explain why you are reducing the random number range and how it affects the algorithm.

Finally, in the "Call myself four times" section, it's not clear why you are calling the function four times. It might be helpful to explain that you are dividing the square into four smaller squares and calling the function recursively on each one.

Overall, your algorithm looks like a good start. I would suggest adding some comments and clarifications to make it easier to follow and debug. Keep up the good work!
 

1. What is the Diamond-Square algorithm?

The Diamond-Square algorithm is a method for creating a random terrain or heightmap using a set of data points. It is commonly used in computer graphics and game development to generate realistic landscapes.

2. How does the Diamond-Square algorithm work?

The Diamond-Square algorithm works by dividing a square into four smaller squares and calculating the average of the four corner points to determine the height of the center point. This process is repeated for each new square created, resulting in a more detailed and randomized terrain.

3. What is pseudo code?

Pseudo code is a simplified version of a programming language that is used to outline the steps or logic of an algorithm without using specific syntax or code. It is often used as a planning tool before writing actual code.

4. What are the benefits of using the Diamond-Square algorithm?

The Diamond-Square algorithm allows for the creation of realistic and detailed terrain without the need for manual input or pre-existing data. It also has a relatively simple implementation compared to other terrain generation methods.

5. Are there any limitations to the Diamond-Square algorithm?

While the Diamond-Square algorithm can generate diverse and complex terrain, it is not suitable for creating highly specific or controlled landscapes. It also requires a significant amount of computational power and may not be suitable for real-time applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
Replies
1
Views
1K
Replies
1
Views
1K
Back
Top