edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Johan_Ha
Published © CC0

Sin sin

Draw beautiful antialiased circles in a programming environment, that has no functions for circles, like SDL.

BeginnerProtip2 hours40
Sin sin

Things used in this project

Hardware components

UNIHIKER M10 - IoT Python Programming Single Board Computer with Touchscreen
DFRobot UNIHIKER M10 - IoT Python Programming Single Board Computer with Touchscreen
×1

Software apps and online services

Microsoft Visual Studio Code

Story

Read more

Code

Fill an 11 by 11 pixel bitmap with an antialiased circle

C/C++
// Fill 11x11 with a small anti-aliased disk (white center, fading to black)
for (int y = 0; y < 11; ++y) 
{
  Uint8* row = static_cast<Uint8*>(pixels) + y * pitch;
  Uint32* px = reinterpret_cast<Uint32*>(row);
  for (int x = 0; x < 11; ++x) 
  {
    float dx = float(x) - 5.0f;
    float dy = float(y) - 5.0f;
    float dist = std::sqrt(dx*dx + dy*dy);
    // Simple radial falloff: bright center -> darker edge
    float v = 3.0f - dist;     // radius is 3 (middle point of circle outline)
    v = v * v;                 // sharpen
    // Map v to 0..255 and clamp
    int col = map(v, 0.4, 255, 7, 0); 
    std::cout << col << ", ";
    // If you want opaque sprite:
    Uint32 p = SDL_MapRGBA(fmt, Uint8(col), Uint8(col), Uint8(col), 255);
    // If you want transparent outside, use alpha based on distance instead:
    // Uint8 a = Uint8(std::clamp( (3.5f - dist) / 1.0f * 255.0f, 0.0f, 255.0f ));
    // Uint32 p = SDL_MapRGBA(fmt, 255, 255, 255, a);
    px[x] = p; // safe: we write one 32-bit pixel; indexing bounds-checked by loops
  }
  std::cout << "\n";
}

Credits

Johan_Ha
20 projects • 29 followers
Music teacher Composer Coding for fun

Comments