Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) PlotSurface

 

ASCII art code snippet that displays a 2D (y-x-ordered) std::vector of double as a surface chart using ASCII art characters (see an example below the code).

 

PlotSurface uses GetAsciiArtGradient, MinElement and MaxElement.

 


#include <vector>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <cmath>

//The 2D std::vector must be y-x-ordered
//From http://www.richelbilderbeek.nl/CppPlotSurface.htm
void PlotSurface(const std::vector<std::vector<double> >& v)
{
  assert(v.empty() == false && "Surface must have a size");
  assert(v[0].size() > 0 && "Surface must have a two-dimensional size");

  //Obtain the ASCII art gradient and its size
  static const std::vector<char> asciiArtGradient = GetAsciiArtGradient();
  static const int nAsciiArtGradientChars = asciiArtGradient.size();

  //Minimum and maximum are not given, so these need to be calculated
  const double minVal = MinElement(v);
  const double maxVal = MaxElement(v);
  assert(minVal != maxVal);

  //Draw the pixels

  //Iterator through all rows
  const std::vector<std::vector<double> >::const_iterator rowEnd = v.end();
  for (std::vector<std::vector<double> >::const_iterator row = v.begin();
    row != rowEnd;
    ++row)
  {
    //Iterate through each row's collumns
    const std::vector<double>::const_iterator colEnd = row->end();
    for (std::vector<double>::const_iterator col = row->begin();
      col != colEnd;
      ++col)
    {
      //Scale the found grey value to an ASCII art character
      assert(maxVal - minVal != 0.0);
      const double greyValueDouble = ( (*col) - minVal) / (maxVal - minVal);
      assert(greyValueDouble >= 0.0 && greyValueDouble <= 1.0);
      const int greyValueInt = greyValueDouble * nAsciiArtGradientChars;
      const int greyValue
        = ( greyValueInt < 0
        ? 0 : (greyValueInt > nAsciiArtGradientChars - 1
          ? nAsciiArtGradientChars - 1: greyValueInt) );
      assert(greyValue >= 0 && greyValue < nAsciiArtGradientChars);
      std::cout << asciiArtGradient[greyValue];
    }
    std::cout << std::endl;
  }
}

 

 

 

 

 

Example

 

`            ``.--://+oossyyyyyyyyssoo+//:--.``              ``.--:/++oossyyyyyyyysso
``         ``..--://++osssyyyyyyyssso++/::--.```         ``..--://++osssyyyyyyyssoo
``````````..--::/++oosssyyyyyysssoo++/::--..``````````..--::/++oosssyyyyyysssoo
..`````...---://++oossssyyyyyssssoo++//:---...`````...---://++oosssyyyyyyssssoo
-......----:://++oossssyyyyyyssssoo++//::----......----:://++oossssyyyyyyssssoo
-------:::://+++oossssyyyyyyyssssoo+++//::::-------:::://+++oosssyyyyyyyyssssoo
::::::////+++ooossssyyyyyyyyyyssssoo+++/////::::::////+++ooossssyyyyyyyyyysssso
/////+++++oooossssyyyyyyyyyyyyyssssoooo+++++/////+++++oooossssyyyyyyyyyyyyyssss
oooooooooosssssyyyyyyyyhhyyyyyyyysssssoooooooooooooooosssssyyyyyyyyhhyyyyyyyyss
sssssssssssyyyyyyyyhhhhhhhhhyyyyyyyysssssssssssssssssssyyyyyyyyhhhhhhhhhyyyyyyy
yyyyyyyyyyyyyhhhhhhhhhhhhhhhhhhhhyyyyyyyyyyyyyyyyyyyyyyyyhhhhhhhhhhhhhhhhhhhhyy
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
dddddddddddhhhhhhhhhhhhhhhhhhhhhhddddddddddddddddddddddhhhhhhhhhhhhhhhhhhhhhhdd
mmmmmmddddddddhhhhhhhhhhhhhhhddddddddmmmmmmmmmmmmmddddddddhhhhhhhhhhhhhhhdddddd
NNNmmmmmmdddddhhhhhhhhhhhhhhdddddmmmmmmNNNNNNNNmmmmmmdddddhhhhhhhhhhhhhhdddddmm
NNNNNmmmmddddhhhhhhyyyhhhhhhddddmmmmNNNNNNNNNNNNNmmmmddddhhhhhhyyyhhhhhhddddmmm
MMNNNNmmmdddhhhyyyyyyyyyyhhhdddmmmNNNNMMMMMMMMNNNNmmmdddhhhyyyyyyyyyyhhhdddmmmN
MMMNNNmmdddhhyyyyysssyyyyyhhdddmmNNNMMMMMMMMMMMNNNmmddhhhyyyyysssyyyyyhhdddmmNN
MMNNNmmddhhyyyssssssssssyyyhhddmmNNNMMMMMMMMMMNNNmmddhhyyyssssssssssyyyhhddmmNN
MMNNmmddhhyysssooooooosssyyhhddmmNNMMMMMMMMMMMNNmmddhhyysssooooooosssyyhhddmmNN
MNNmmddhyyssooo++++++ooossyyhddmmNNMMMMMMMMMMNNmmddhyyssooo++++++ooossyyhddmmNN
NNmmdhhyssoo++///////++oossyhhdmmNNMMMMMMMMMNNmmdhhyssoo++///////++oossyhhdmmNN
Nmddhyysoo++//:::::://++oosyyhddmNNNMMMMMMNNNmddhyysoo++//:::::://++oosyyhddmNN

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict