Drawing a Half-Circle

Actionscript:
  1. graphics.beginFill(0xFF0000);
  2. halfCircle(graphics, 200,200, 100);
  3. // original circle function by senocular (www.senocular.com) from here http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
  4. function halfCircle(g:Graphics, x:Number,y:Number,r:Number):void {
  5.     var c1:Number=r * (Math.SQRT2 - 1);
  6.     var c2:Number=r * Math.SQRT2 / 2;
  7.     g.moveTo(x+r,y);
  8.     g.curveTo(x+r,y+c1,x+c2,y+c2);
  9.     g.curveTo(x+c1,y+r,x,y+r);
  10.     g.curveTo(x-c1,y+r,x-c2,y+c2);
  11.     g.curveTo(x-r,y+c1,x-r,y);
  12.     // comment in for full circle
  13.     /*g.curveTo(x-r,y-c1,x-c2,y-c2);
  14.     g.curveTo(x-c1,y-r,x,y-r);
  15.     g.curveTo(x+c1,y-r,x+c2,y-c2);
  16.     g.curveTo(x+r,y-c1,x+r,y);*/
  17. };

A quick way to draw a half-circle... I used to use this to draw circles in flash before the Graphics.drawCircle() method.

This entry was posted in Graphics and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted December 15, 2009 at 1:50 pm | Permalink

    Thank you for source code. With your code i was able to draw half-circles(fill lowlight, fill highlight) for my RoundButtonSkin component.

    Mariush T.
    http://mariusht.com/blog/

  2. Usama Ahmed
    Posted December 21, 2009 at 3:14 am | Permalink

    Thank you. It worked for me

  3. Beth Moursund
    Posted May 4, 2011 at 9:59 am | Permalink

    I adapted this to draw any of the 8 halfCircles starting from multiples of 45 degrees.

    public static function halfCircle(g:Graphics, x:Number,y:Number,r:Number, startAngle:int):void {
    var c1:Number=r * (Math.SQRT2 - 1);
    var c2:Number = r * Math.SQRT2 / 2;
    var start:int = ((startAngle +360) % 360) / 45;
    var controlX:Number;
    var controlY:Number;
    var anchorX:Number;
    var anchorY:Number;

    for (var i:int = start; i < start + 5; i++) {
    switch (i % 8) {
    case 0:
    controlX = x + r;
    controlY = y - c1;
    anchorX = x + r;
    anchorY = y;
    break;
    case 1:
    controlX = x + r;
    controlY = y + c1;
    anchorX = x + c2;
    anchorY = y + c2;
    break;
    case 2:
    controlX = x + c1;
    controlY = y + r;
    anchorX = x;
    anchorY = y + r;
    break;
    case 3:
    controlX = x - c1;
    controlY = y + r;
    anchorX = x - c2;
    anchorY = y + c2;
    break;
    case 4:
    controlX = x - r;
    controlY = y + c1;
    anchorX = x - r;
    anchorY = y;
    break;
    case 5:
    controlX = x - r;
    controlY = y - c1;
    anchorX = x - c2;
    anchorY = y - c2;
    break;
    case 6:
    controlX = x - c1;
    controlY = y - r;
    anchorX = x;
    anchorY = y - r;
    break;
    case 7:
    controlX = x + c1;
    controlY = y - r;
    anchorX = x + c2;
    anchorY = y - c2;
    break;
    }
    if (i == start) {
    g.moveTo(anchorX, anchorY);
    } else {
    g.curveTo(controlX, controlY, anchorX, anchorY);
    }
    }
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*