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;
int gridWidth=1000;
int gridHeight=1000;
void setStartNumber2d(int x,int y){
x=((int) sqrt(pow(x,2))) %gridWidth;
y=((int) sqrt(pow(y,2))) %gridHeight;
srand(1);
for(int i=0;i!=x+y*gridWidth;i++)rand();
}
int main(){
int x=0;
int y=0;
int screenWidth=80;
int screenHeight=38;
int randomValues[(screenWidth+10)*(screenHeight+10)];
int whereInArray=0;
int total;
char totalLow=' ';
char totalMedium=176;
char totalHigh=177;
char totalHigher=178;
string out="";
while(true){
system("pause");
if(GetAsyncKeyState(VK_LEFT))x--;
if(GetAsyncKeyState(VK_RIGHT))x++;
if(GetAsyncKeyState(VK_UP))y--;
if(GetAsyncKeyState(VK_DOWN))y++;
setStartNumber2d(x,y);
for(int i=0;i<(screenWidth+10)*(screenHeight+10);i++)randomValues[i]=0;
for(int y=0;y<(screenHeight+10);y++){
for(int x=0;x<(screenWidth+10);x++){
whereInArray=x+y*(screenWidth+10);
if(rand()%1000>500){
randomValues[whereInArray]=1;
}else{
randomValues[whereInArray]=0;
}
}
for(int x=0;x<gridWidth-(screenWidth+10);x++){rand();
}
}
out="";
for(int y=0;y<screenHeight;y++){
for(int x=0;x<screenWidth;x++){
total=0;
for(int circularSearchY=-5;circularSearchY<5;circularSearchY++){
int cosine=sqrt(pow(5,2)-pow(circularSearchY,2));
for(int circularSearchX=0-cosine;circularSearchX<cosine;circularSearchX++){
total+=randomValues[(x+5+circularSearchX)+(y+5+circularSearchY)*90];
}
}
if(total<33)out+=totalLow;
if(total>32&&total<36)out+=totalMedium;
if(total>35&&total<40)out+=totalHigh;
if(total>39)out+=totalHigher;
}
}
system("cls");
cout<<out;
}
}
Points of Interest
Please contact me with questions, suggestions or potential problems on ruben.skrappost@gmail.com.