# Drawing a Half-Circle

Actionscript:
1. graphics.beginFill(0xFF0000);
2. halfCircle(graphics, 200,200, 100);
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.

1. Mariush T.
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 % {
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);
}
}
}

• 