Skip to content

Instantly share code, notes, and snippets.

@mortennobel
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortennobel/8818563 to your computer and use it in GitHub Desktop.
Save mortennobel/8818563 to your computer and use it in GitHub Desktop.
ASCII visualization (used for debugging 2d scalar fields)
#include <string>
#include <array>
#include <algorithm>
#include <functional>
using namespace std;
// assumes that values returned by data is normalized between 0.0 and 1.0
string get_grayscale_image(int width, int height, function<float(int,int)> data){
const int size = 10;
const array<char,size+1> ascii {" .:-=+*#%@"};
string res = "";
for (int y=0;y<height;y++){
for (int x=0;x<width;x++){
float value = data(x,y);
float clampedValue = max(0.0f,min(1.0f, value));
int mappedInt = min(size-1,(int)(clampedValue*10));
res += ascii[mappedInt];
}
res += "\n";
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment