Antialiased
Circle and Disc
Drawing
Site Map Feedback
Translucent Circle Drawing
Up Buttons Circles Curves Lines
Antialiased Disc Drawing
To draw fast translucent circles or antialiased discs on the PixelBlock, mix in the CCircleStencil class below and define the abstract method (BlendPixel) as follows:
class CCirclePainter : public CPixelBlock, public CCircleStencil {
  void BlendPixel(WORD x, WORD y, DWORD Colour, BYTE Transparency) {CPixelBlock::BlendPixel(x,y, Colour, Transparency);}
};
The discs are missing the centre pixel because they are designed for selecting points on 3D objects. Being able to see what is under the centre pixel is valuable. Since it is easy to add a pixel, but impossible to remove it, the Disc has been kept with the transparent centre pixel.
Here's the Stencil:
class CCircleStencil {
  virtual void BlendPixel(WORD x, WORD y, DWORD Colour, BYTE Transparency) =0;
public:
  void Circle(WORD xCenter, WORD yCenter, WORD Radius, DWORD Colour, BYTE Transparency=255) {
    if(Radius==0) return;
    int x=0;
    int y=Radius;
    int p=(5-(Radius<<2))>>2; // Midpoint Algorithm
    BlendPixel(xCenter  , yCenter+y, Colour, Transparency);
    BlendPixel(xCenter  , yCenter-y, Colour, Transparency);
    BlendPixel(xCenter+y, yCenter  , Colour, Transparency);
    BlendPixel(xCenter-y, yCenter  , Colour, Transparency);
    while(x++<=y) {
      if(p<0) p+=1+(x<<1);
      else    p+=1+((x- --y)<<1);
      BlendPixel(xCenter+x, yCenter+y, Colour, Transparency);
      BlendPixel(xCenter+x, yCenter-y, Colour, Transparency);
      if(x) { // don't overdraw vertical line
        BlendPixel(xCenter-x, yCenter+y, Colour, Transparency);
        BlendPixel(xCenter-x, yCenter-y, Colour, Transparency);
      }
      if(x==y) continue; // don't overdraw diagonal line
      BlendPixel(xCenter+y, yCenter+x, Colour, Transparency);
      BlendPixel(xCenter-y, yCenter+x, Colour, Transparency);
      if(x==0) continue; // don't overdraw horizontal line
      BlendPixel(xCenter+y, yCenter-x, Colour, Transparency);
      BlendPixel(xCenter-y, yCenter-x, Colour, Transparency);
  } }

  void Disc(WORD xCenter, WORD yCenter, WORD Radius, DWORD Colour) { // Antialiased, Filled apart from centre pixel:
    if(Radius==0) return;
    WORD Radius2=Radius*Radius;
    for(WORD y=Radius; y; --y) {
      for(WORD x=0; x<=y; ++x) {
        WORD r2=x*x+y*y;
        if(r2>=Radius2) continue;
        BYTE Transparency=BYTE(~(((Radius2-r2)<<8)/Radius2));
        if(Transparency==0) continue;
        BlendPixel(xCenter+x, yCenter+y, Colour, Transparency);
        BlendPixel(xCenter+x, yCenter-y, Colour, Transparency);
        if(x) { // don't overdraw vertical line
          BlendPixel(xCenter-x, yCenter+y, Colour, Transparency);
          BlendPixel(xCenter-x, yCenter-y, Colour, Transparency);
        }
        if(x==y) continue; // don't overdraw diagonal line
        BlendPixel(xCenter+y, yCenter+x, Colour, Transparency);
        BlendPixel(xCenter-y, yCenter+x, Colour, Transparency);
        if(x==0) continue; // don't overdraw horizontal line
        BlendPixel(xCenter+y, yCenter-x, Colour, Transparency);
        BlendPixel(xCenter-y, yCenter-x, Colour, Transparency);
  } } }
};
You can mix in as many 'Stencils' as you like to provide all the primitives you need.

The Colours, Effects and Patterns sections have further extensions.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.