   Drawing a 2D antialiased circle?
  Hi,Does anyone have a link or some example on how to draw an antialiased 2D circle?I have an image buffer and just want to set the pixels myself. This is for a mobile device which does not have any Graphics package to do such a thing. I'm having trouble finding something ready made though it seems like there should be something out there already,Thanks   
Quote:Original post by markwwHi,Does anyone have a link or some example on how to draw an antialiased 2D circle?I have an image buffer and just want to set the pixels myself. This is for a mobile device which does not have any Graphics package to do such a thing. I'm having trouble finding something ready made though it seems like there should be something out there already,ThanksAre you sure there's no vector graphics library that would work on the device?I've been playing around with some different vector graphics libraries, but since I'm working on a PC, I really couldn't tell you which of them (if any) would work on a mobile device. While researching though, I'm pretty sure I've seen mention of such libraries.You might check out the 'SVG wiki'; among other things, it has (IIRC) a list of various libraries/implementations. Wikipedia may have some info as well.   ;  A simple way of doing this is to first copy from the target rect on the target buffer to a temporary buffer that is twice the height and twice the width. You double each pixel horizontally and each row vertically in the process, to stretch the image to the larger buffer size.Then draw a non-antialiased circle twice as big as planned onto that, and then copy the temporary buffer back over the target buffer, averaging each 2x2 block of pixels as you go.If you need it smoother you can go up to 4x4, if you have enough memory and time to spare.There should also be ways of doing it without the large temporary buffer, but they are no doubt much more complex.If you only need it of about one size then you could just get it pre-rendered and include an alpha mask and alpha-blend it on.   ;Quote:Original post by iMalcA simple way of doing this is to first copy from the target rect on the target buffer to a temporary buffer that is twice the height and twice the width. You double each pixel horizontally and each row vertically in the process, to stretch the image to the larger buffer size.Then draw a non-antialiased circle twice as big as planned onto that, and then copy the temporary buffer back over the target buffer, averaging each 2x2 block of pixels as you go.If you need it smoother you can go up to 4x4, if you have enough memory and time to spare.There should also be ways of doing it without the large temporary buffer, but they are no doubt much more complex.If you only need it of about one size then you could just get it pre-rendered and include an alpha mask and alpha-blend it on.Shouldn't be possible to use exactly this algoritm on the fly, that is without using the temporary buffer but instead calculating 'what every pixel value would be if I used a larger buffer and the reduced its size'?   ;  One way I've done this in the past is to calculate the distance from the pixel centre to the circle's centre. Fade the pixel intensity from 1 to 0 as the distance ranges from R to R+1. Here's some code from a small app I wrote a while back:void CircleApp::Circle( float x, float y, float r, const Rgb& rColour ){if ( !m_replaying )m_replayContainer.push_back( ReplayElement( rColour, x, y, r ) );int s = (int)ceilf(r+2);int sx = max( (int)floorf(x-s), 0 );int sy = max( (int)floorf(y-s), 0 );int ex = min( (int)ceilf(x+s), (int)m_width );int ey = min( (int)ceilf(y+s), (int)m_height );// Write the colourRgb* pDstRow = m_pData + sy * m_width;for ( int j = sy; j < ey; ++j ){float dy = j - y + 0.5f;float dy2 = dy * dy;for ( int i = sx; i < ex; ++i ){float dx = i - x + 0.5f;float dx2 = dx * dx;float d = sqrtf( dx2 + dy2 );float alpha = 0.8f * ( r + 1.0f - d );alpha = max( min( alpha, 1.0f ), 0.0f );pDstRow.m_r = pDstRow.m_r + alpha * ( rColour.m_r - pDstRow.m_r );pDstRow.m_g = pDstRow.m_g + alpha * ( rColour.m_g - pDstRow.m_g );pDstRow.m_b = pDstRow.m_b + alpha * ( rColour.m_b - pDstRow.m_b );}pDstRow += m_width;}UpdateTextureData( sx, sy, ex, ey );}This isn't fast, but it's simple and gives decent results. Google knows a better way.   