Simple Noise Example





5.00/5 (2 votes)
This simple code displays a perlin noise like example.
Introduction
This code is constructed to demonstrate a short and easy to understand example. The example is of how noise can be used and generated in basic C++ code.
Using the Code
The code is written in C++ and was last run in "Dev-C++". Please contact me with problems on using the code in other IDEs.
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <windows.h>
#include <stdio.h>
#include <math.h>
using namespace std;
//The height and width of the total area
int gridWidth=1000;
int gridHeight=1000;
//Function for setting start position of "rand()" function values.
void setStartNumber2d(int x,int y){
//The X and Y values are set to valid values by using the "absolute value" to avoid negativ inputs.
// The Absolute Value
// |------------|
x=((int) sqrt(pow(x,2))) %gridWidth;
y=((int) sqrt(pow(y,2))) %gridHeight;
//The rand value is reset to make sure that the start value is always the same.
srand(1);
//The current rand value is found with using a for loop.
for(int i=0;i!=x+y*gridWidth;i++)rand();
//There is no return value as the function only sets the "start position" for the rand value.
}
int main(){
// X & Y position is stored in integers.
int x=0;
int y=0;
// The screen size is adapted to fit a console window.
int screenWidth=80;
int screenHeight=38;
// The "randomValues" array is used to store random integers.
// It is ten units wider than the screen; It is also ten units higher.
int randomValues[(screenWidth+10)*(screenHeight+10)];
int whereInArray=0;
int total;
char totalLow=' ';
char totalMedium=176;
char totalHigh=177;
char totalHigher=178;
// String out is used to output the noise result in characters.
string out="";
while(true){
system("pause");
// GetAsyncKeyState is used to apply movement.
if(GetAsyncKeyState(VK_LEFT))x--;
if(GetAsyncKeyState(VK_RIGHT))x++;
if(GetAsyncKeyState(VK_UP))y--;
if(GetAsyncKeyState(VK_DOWN))y++;
// Here "setStartNumber2d" is used to set our current position.
setStartNumber2d(x,y);
// This for loop is used to reset each integer in the "randomValues" array to zero.
for(int i=0;i<(screenWidth+10)*(screenHeight+10);i++)randomValues[i]=0;
// The first for loop is used to loop through all y-values of the "randomValues"
// the next will loop through all the x-values.
for(int y=0;y<(screenHeight+10);y++){
for(int x=0;x<(screenWidth+10);x++){
//To determine where in the array the value should be applied the
//X value is added to the Y value times the max-X value.
whereInArray=x+y*(screenWidth+10);
//For each X and Y position the value applied to "randomValues"
//will either be zero or one.
if(rand()%1000>500){
randomValues[whereInArray]=1;
}else{
randomValues[whereInArray]=0;
}
}
//This for loops finishes one full line of the grid to later continue
//on the next Y value of the grid.
for(int x=0;x<gridWidth-(screenWidth+10);x++){rand();
}
}
//The "out" string is reset.
out="";
// The first for loop is used to loop through all y-values of the screen
// the next will loop through all the x-values.
for(int y=0;y<screenHeight;y++){
for(int x=0;x<screenWidth;x++){
total=0;
//These loops will run through a circular area to find the total
//of the randomValues in this area.
//The area looks something like this:
// --- xxx
// | xxxxx
// 10| xxxxx
// | xxxxx
// --- xxx
for(int circularSearchY=-5;circularSearchY<5;circularSearchY++){
//Cosine is the X distance from X=0
int cosine=sqrt(pow(5,2)-pow(circularSearchY,2));
//This for loop will through all X values for the current Y position.
for(int circularSearchX=0-cosine;circularSearchX<cosine;circularSearchX++){
//Depending on the stored value zero or one is added
//from the "randomValues" array.
total+=randomValues[(x+5+circularSearchX)+(y+5+circularSearchY)*90];
}
}
//After the total is summed different characters are accordingly
//added to the "out" string.
if(total<33)out+=totalLow;
if(total>32&&total<36)out+=totalMedium;
if(total>35&&total<40)out+=totalHigh;
if(total>39)out+=totalHigher;
}
}
//The previous screen is cleared.
system("cls");
//The out string isprinted to the screen.
cout<<out;
}
}
Points of Interest
Please contact me with questions, suggestions or potential problems on ruben.skrappost@gmail.com.