(C++) DrawGlobe
February 24, 2017 · View on GitHub
(C++) DrawGlobe
DrawGlobe is a graphics code snippet to draw a globe.
DrawGlobe using the Qt library
- Download the Qt Creator project 'CppDrawGlobe' (zip)
- View the Qt Creator source code of 'CppDrawGlobe' as a web page (htm)
//From http://www.richelbilderbeek.nl/CppDrawGlobe.htm QPixmap DrawGlobe( const int width, const int height, const unsigned char r, const unsigned char g, const unsigned char b) { QPixmap pixmap(width,height); QImage image = pixmap.toImage(); assert(image.bytesPerLine() / width == 4 && "Assume there are 4 bytes per pixel"); const double r_max = boost::numeric_cast<double>(r); const double g_max = boost::numeric_cast<double>(g); const double b_max = boost::numeric_cast<double>(b); const double midX = boost::numeric_cast<double>(width ) / 2.0; const double midY = boost::numeric_cast<double>(height) / 2.0; const double max_dist = std::min(midX,midY); for (int y=0; y!=height; ++y) { unsigned char * const line = static_cast<unsigned char *>(image.scanLine(y)); const double y_d = boost::numeric_cast<double>(y); for (int x=0; x!=width; ++x) { const double x_d = boost::numeric_cast<double>(x); const double dist = std::sqrt( ((x_d - midX) * (x_d - midX)) + ((y_d - midY) * (y_d - midY)) ); if (dist <= max_dist) { const double rel_dist = dist / max_dist; const int r_here = rel_dist * r_max; const int g_here = rel_dist * g_max; const int b_here = rel_dist * b_max; assert( r_here >= 0); assert( r_here < 256); assert( g_here >= 0); assert( g_here < 256); assert( b_here >= 0); assert( b_here < 256); line[x*4+3] = 255; //Alpha value line[x*4+2] = r_here; //Red line[x*4+1] = g_here; //Green line[x*4+0] = b_here; //Blue } else { line[x*4+3] = 0; //Alpha value line[x*4+2] = 0; //Red line[x*4+1] = 0; //Green line[x*4+0] = 0; //Blue } } } pixmap = pixmap.fromImage(image); //Add transparency const QBitmap mask = pixmap.createMaskFromColor(QColor(0,0,0,0).rgb()); pixmap.setMask(mask); return pixmap; }
DrawGlobe using the VCL library
#include <algorithm> //For std::min #include <cassert> //For assert #include <cmath> //For std::sqrt //From http://www.richelbilderbeek.nl/CppDrawGlobe.htm void DrawGlobe( TImage * const image, const unsigned char rMax, const unsigned char gMax, const unsigned char bMax) { assert(image!=0); const int width = image->Picture->Bitmap->Width; const int height = image->Picture->Bitmap->Height; const double midX = static_cast<double>(width ) / 2.0; const double midY = static_cast<double>(height) / 2.0; const double maxDist = std::min(midX,midY); for (int y=0; y!=height; ++y) { unsigned char * const line = static_cast<unsigned char *>(image->Picture->Bitmap->ScanLine[y]); const double yD = static_cast<double>(y); for (int x=0; x!=width; ++x) { const double xD = static_cast<double>(x); const double dist = std::sqrt( ((xD - midX) * (xD - midX)) + ((yD - midY) * (yD - midY)) ); if (dist <= maxDist) { const double relDist = dist / maxDist; const int r = relDist * static_cast<double>(rMax); const int g = relDist * static_cast<double>(gMax); const int b = relDist * static_cast<double>(bMax); assert( r >= 0); assert( r < 256); assert( g >= 0); assert( g < 256); assert( b >= 0); assert( b < 256); line[x*3+2] = (r == 0 ? 1 : r); //Never use a zero for red line[x*3+1] = (g == 0 ? 1 : g); //Never use a zero for green line[x*3+0] = (b == 0 ? 1 : b); //Never use a zero for blue } else { line[x*3+2] = 0; line[x*3+1] = 0; line[x*3+0] = 0; } } } }