Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2005, 02:46 PM   #1 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
Help with Fractal Terrain Generation Algorithm

Hi, I'm having problems again with another algorithm. This time it is the Diamond-Square algorithm(htpp://www.gameprogrammer.com/fractal.html if you are interested)
It is producing the desired effect, sort of. It is suppose to randomize all y values to non-zero values, however it only does about a quarter of them. Any help would be much appriciated.

Code:
// Brad Galloway
// Diamond-Square Implementation
// First compile: April 16th, 2005

#include<fstream>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cmath>

using namespace std;
const int ARRAY_SIZE = 33;

struct point{
       float x;
       float y;
       float z;
};

point array[ARRAY_SIZE][ARRAY_SIZE];

float random_number(){
        //Generates a random floating point number
        srand(time(NULL));
        int random = (rand() % 200) - 100;
        float return_value = (float)random / 100.0f;
        return return_value;
}

void create_array(){
     for(int i = 0; i < ARRAY_SIZE; i++){
        for(int j = 0; j < ARRAY_SIZE; j++){
           array[i][j].x = (float)j - (float)(ARRAY_SIZE/2);
           array[i][j].y = 0.0f;
           array[i][j].z = (float)i - (float)(ARRAY_SIZE/2);
        }
     }
}   
void diamond_step(int s, int n) //s = size of squares in this step, n = number of squares
{
     int count = 0; //keeps track of the column we are modifying.
     int xstart = 0;  //Keeps track of the starting location for this square x
     int zstart = 0;  //Keeps track of the starting location for this square z
     float average;     //Holds the average of the for corners
     for(int i = 0; i < n; i++){  //Loops equal to the number of squares to be done in this time
        // average = (corner1 + corner2 + corner3 + corner4)/4
        average = (array[xstart][zstart].y + array[xstart + s][zstart].y +
                  array[xstart][zstart + s].y + array[xstart + s][zstart + s].y)/4.0f;
        if(array[s/2 + 1 + xstart][s/2 + 1 + zstart].y == 0)
           array[s/2 + 1 + xstart][s/2 + 1 + zstart].y += average + random_number();    
        count++;     //Add one to count
        if(count^2 == n){  //If count squared equals n
           count = 0;      //then set count to 0
           xstart = 0;     //set xstart back to 0
           zstart += s;    //increment zstart
        }
        else{           
           xstart += s;    //if not, increment xstart and do the next square
        };
     }
}

void square_step(int half_size, int n){// Note xspot and zspot correspond to the points in the array that contain the data, not the data itself.
     int count_dia = 0;
     int test_number = (int)sqrt((float)n);
	 if(test_number == 0)
		 test_number = 1;
	 int xspot = (((ARRAY_SIZE/test_number)/2)+1);
     int zspot = (((ARRAY_SIZE/test_number)/2)+1);
     int xbegin = xspot;
     float average_dia_left;           //Keeps track of the average heights for the four diamonds
     float average_dia_top;            //around the axis.
     float average_dia_right;
     float average_dia_bottom;     
     for(int i = 0; i < n; i++){
        if(xspot >= (half_size * 2)){ //Left
          average_dia_left = array[xspot][zspot].y + array[xspot - half_size][zspot + half_size].y
           + array[xspot - (half_size * 2)][zspot].y + array[xspot - half_size][zspot - half_size].y;  
        }
        else{ //If the axis is on next to the outside of the array
          average_dia_left = array[xspot][zspot].y + array[xspot - half_size][zspot + half_size].y
           + array[xspot - half_size][zspot - half_size].y;  
        }   
        if(zspot >= (half_size * 2)){ //Top
          average_dia_top = array[xspot][zspot].y + array[xspot - half_size][zspot - half_size].y
           + array[xspot][zspot - (half_size * 2)].y + array[xspot + half_size][zspot + half_size].y;  
        }
        else{ //If the axis is on next to the outside of the array
          average_dia_top = array[xspot][zspot].y + array[xspot - half_size][zspot - half_size].y
           + array[xspot + half_size][zspot + half_size].y;  
        }   
        if(xspot + (half_size * 2) <= ARRAY_SIZE){ //Right
          average_dia_right = array[xspot][zspot].y + array[xspot + half_size][zspot - half_size].y
           + array[xspot + (half_size * 2)][zspot].y + array[xspot + half_size][zspot + half_size].y;  
        }
        else{ //If the axis is on next to the outside of the array
          average_dia_right = array[xspot][zspot].y + array[xspot + half_size][zspot - half_size].y
           + array[xspot + half_size][zspot + half_size].y;  
        }   
        if(zspot + (half_size * 2) <= ARRAY_SIZE){ //Bottom
          average_dia_bottom = array[xspot][zspot].y + array[xspot + half_size][zspot + half_size].y
           + array[xspot][zspot + (half_size * 2)].y + array[xspot - half_size][zspot + half_size].y;  
        }
        else{ //If the axis is on next to the outside of the array
          average_dia_bottom = array[xspot][zspot].y + array[xspot + half_size][zspot + half_size].y
           + array[xspot - half_size][zspot + half_size].y;  
        }   
                        
        if(array[xspot - half_size][zspot].y == 0){ //Left
           array[xspot - half_size][zspot].y += average_dia_left + random_number();     
        }
        if(array[xspot][zspot - half_size].y == 0){ //Top
           array[xspot][zspot - half_size].y += average_dia_top + random_number();     
        }
        if(array[xspot + half_size][zspot].y == 0){ //Right
           array[xspot + half_size][zspot].y += average_dia_right + random_number();     
        }
        if(array[xspot][zspot + half_size].y == 0){ //Bottom
           array[xspot][zspot + half_size].y += average_dia_bottom + random_number();     
        }   
        count_dia++;     //Add one to count
        if(count_dia^2 == n){  //If count squared equals n
           count_dia = 0;      //then set count to 0
           xspot = xbegin;     //set xspot back to Initial step
           zspot += half_size * 2;    //increment zstart
        }
        else{           
           xspot += half_size * 2;    //if not, increment xstart and do the next square
        };
     }

}
void print_array(){
     char filename[11] = "output.txt";
     ofstream outFile( filename );
     for(int j = 0; j < ARRAY_SIZE; j++){
        for(int i = 0; i < ARRAY_SIZE; i++){     
           outFile << "(" << array[i][j].x << ", " << array[i][j].y << ", " << array[i][j].z << ") ";
        }
        outFile << endl;
     }
     cout << "File created 'output.txt'";  
}

int main(){
    int num_cells = (ARRAY_SIZE - 1)^2;
    bool done = false;
    int q = 1;       //quotient for figuring the size of the squares and number of squares

    create_array();
    while(!done){
       diamond_step(num_cells/q, q^2);
       square_step((num_cells/q)/2, q^2);
       q *= 2;
       if(q > num_cells)
           done = true;
    }
    print_array();
    return 1;
}
brad_galloway is offline   Reply With Quote
Old 04-16-2005, 04:54 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
First of all, return 0 on successfull termination. Any non-zero is an error.
Next thing: find a reference on C++ and find out what the "^" does.

If you need more help, then don't give me such a useless link. I am interested in the definition, not "story-like" explanations where I have to re-invent the wheel.
__________________

Last edited by Valmont; 04-16-2005 at 05:17 PM.
Valmont is offline   Reply With Quote
Old 04-17-2005, 07:30 AM   #3 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
That link was the same link I used, and it is a definition for the algorithm.

Changing it the return value from 1 to 0 doesn't make a difference, 1 doesn't cause an error.

the '^' is the exponent operator. Isn't it? When I placed '^2' in the program that was meant to square it.
brad_galloway is offline   Reply With Quote
Old 04-17-2005, 11:21 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Oh brad come on. Google for the C++ operator "^" .
__________________
Valmont is offline   Reply With Quote
Old 04-17-2005, 08:43 PM   #5 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
We may have just stumbled on one of the reasons my code never works. Well, I've been looking and I can't find an exponent operator, so I guess I'll have to write it in a function.
brad_galloway is offline   Reply With Quote
Old 04-17-2005, 08:48 PM   #6 (permalink)
brad_galloway
Registered User
 
brad_galloway's Avatar
 
Join Date: Feb 2005
Location: Western KY
Posts: 24
brad_galloway is on a distinguished road
It Works

IT WORKS, IT WORKS!!! I ACTUALLY GOT SOMETHING TO WORK!
sorry for the all caps but you don't know how happy I am. If I wasn't so sure you're a man I'd offer to kiss you. All these years I've been using that as the exponent operator in my programs. The only question is how did it get stucky in my head that way? Probably Qbasic.

IT WORKS!
brad_galloway is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Munske's Algorithm brad_galloway Standard C, C++ 5 03-18-2005 11:40 PM


All times are GMT -8. The time now is 05:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting