Drawing an expanding unfilled circle using dynamic sprites.

Started by KodiakBehr, Mon 04/01/2016 03:14:52

Previous topic - Next topic

KodiakBehr

Hi guys,

I could use some help.  I'm trying to use dynamic sprites to draw an unfilled circle whose radius continuously expands until a set point.  I have only been successful in drawing a filled circle.  Is there any obvious way to accomplish what I'm attempting?

The code, for what it's worth, follows:

Code: ags

scanarea = DynamicSprite.Create(800, 600, true);
btnDrawScanZone.Clickable=false;
DrawingSurface *surface = scanarea.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = 2016;
surface.DrawCircle(400, 300, scan_radius);
surface.Release();
btnDrawScanZone.NormalGraphic = scanarea.Graphic;


Gilbert

I don't remember whether you can use colour 0 to draw stuff on a surface to make it transparent, if this is the case, one simple way is to draw a smaller circle filled with colour 0 inside the original circle to puncture it, otherwise you may whip up a function like:

Code: ags

function DrawHollowCircle(DrawingSurface* surface, int ox, int oy, int radius){
  int dd, ii=0;
  int limit = FloatToInt(IntToFloat(radius)/Maths.Sqrt(2.0));
  while(ii<=limit){
    dd = FloatToInt(Maths.Sqrt(IntToFloat(radius*radius - ii*ii)));
    surface.DrawPixel(ox-ii,oy-dd);
    surface.DrawPixel(ox+ii,oy-dd);
    surface.DrawPixel(ox-ii,oy+dd);
    surface.DrawPixel(ox+ii,oy+dd);
    surface.DrawPixel(ox-dd,oy-ii);
    surface.DrawPixel(ox+dd,oy-ii);
    surface.DrawPixel(ox-dd,oy+ii);
    surface.DrawPixel(ox+dd,oy+ii);
    ii++;
  }
}

Edit: This should now work.
Edited again: Just saw the original DrawCircle() function doesn't include the colour either, so I just exercised the colour parameter for simplicity.

Monsieur OUXX

The advantage of using the "circle with color zero onto a temporary sprite" technique is the HUGE performance gain compared to drawing the pixels one by one.
 

KodiakBehr

I was surprised to discover that drawing a transparent circle within an opaque circle worked wonderfully.  In the end, because it needed to be transparent, not opaque black, I had to set the color to -1.

The end result code looked like this.

Code: ags

scanarea = DynamicSprite.Create(500, 500, true);
btnDrawScanZone.Clickable=false;
DrawingSurface *surface = scanarea.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = 2016;
surface.DrawCircle(250, 250, scan_radius);
surface.DrawingColor = -1;
surface.DrawCircle(250, 250, scan_radius-2);
surface.Release();
btnDrawScanZone.NormalGraphic = scanarea.Graphic;


Thank you both!

Crimson Wizard

I must point out that for safety and compatibility reasons it is recommended to use "COLOR_TRANSPARENT" constant as transparent color value (instead of -1).
This is because hypothetically its actual value may be changed in some future release.

KodiakBehr


Monsieur OUXX

Shouldn't the second circle be drawn at (251,251) ?
 

Snarky

No, the coordinates are for the center, and obviously both circles need to have the same center.

SMF spam blocked by CleanTalk