Graphics code snippet to draw a lymphocyte
#include <cassert> //For assert #include <cmath> //For std::sqrt #include <algorithm> //For std::min //From http://www.richelbilderbeek.nl/CppDrawLymphocyte.htm void DrawLymphocyte( 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); //Distance to end const double edgeDist = maxDist * 0.7; //Distance to edge 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 <= edgeDist) { const double relDist = dist / edgeDist; const int r = (0.5 + (0.5 * relDist)) * static_cast<double>(rMax); const int g = (0.5 + (0.5 * relDist)) * static_cast<double>(gMax); const int b = (0.5 + (0.5 * 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 if (dist <= maxDist) { const double relDist = (dist - edgeDist) / (maxDist - edgeDist); const int r = (0.5 + (0.5 * (1.0 - relDist))) * static_cast<double>(rMax); const int g = (0.5 + (0.5 * (1.0 - relDist))) * static_cast<double>(gMax); const int b = (0.5 + (0.5 * (1.0 - 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; } } } }