Skip to content
April 24, 2010 / edeustace

Drawing class for Actionscript 3, for drawing outide of a quarter circle

Here’s a wee drawing class for Actionscript 3. I found many classes that draw pies and arcs, but not one that draws out parts of the pie.

Usage:

private function drawSample( ... ignored ) : void
{
	graphics.beginFill( 0xffffff );
	graphics.drawRect( 0, 0, 500, 500 );
	graphics.endFill();
			
	graphics.beginFill(0x00FF34);
	var shifter : int = 20;
	OuterCircleQuadrant.drawOuterCircleQuadrant( graphics, 0 + shifter, 50 + shifter, 50, OuterCircleQuadrant.SOUTH_WEST );
	OuterCircleQuadrant.drawOuterCircleQuadrant( graphics, 50 + shifter, 50 + shifter, 50, OuterCircleQuadrant.SOUTH_EAST );
	OuterCircleQuadrant.drawOuterCircleQuadrant( graphics, 0 + shifter, 0 + shifter, 50, OuterCircleQuadrant.NORTH_WEST );
	OuterCircleQuadrant.drawOuterCircleQuadrant( graphics, 50 + shifter, 0 + shifter, 50, OuterCircleQuadrant.NORTH_EAST );
          
         graphics.endFill();
          	
          	
         var diamond : MovieClip = new MovieClip();
         diamond.x = 100;
          	
        diamond.graphics.beginFill( 0xff33ff );
        OuterCircleQuadrant.drawOuterCircleQuadrant( diamond.graphics, 50 + shifter, 0 + shifter, 50, OuterCircleQuadrant.SOUTH_WEST );
        OuterCircleQuadrant.drawOuterCircleQuadrant( diamond.graphics, 0 + shifter, 0 + shifter, 50, OuterCircleQuadrant.SOUTH_EAST );
        OuterCircleQuadrant.drawOuterCircleQuadrant( diamond.graphics, 50 + shifter, 50 + shifter, 50, OuterCircleQuadrant.NORTH_WEST );
        OuterCircleQuadrant.drawOuterCircleQuadrant( diamond.graphics, 0 + shifter, 50 + shifter, 50, OuterCircleQuadrant.NORTH_EAST );
        diamond.graphics.endFill();
	addChild( diamond );
}

Gives you:
As sample

Here is the class:

package drawing
{
	import flash.display.Graphics;
	import flash.geom.Point;
	
	public class OuterCircleQuadrant
	{
		public static const NORTH_WEST : String = "NW";
		public static const SOUTH_WEST : String = "SW";
		public static const NORTH_EAST : String = "NE";
		public static const SOUTH_EAST : String = "SE";
		
		public static function drawOuterCircleQuadrant( g : Graphics, x : Number, y : Number, r : Number, type : String ) : void
		{
			
			var startPoint : Point;
	        var curveEndPoint : Point;
	        var curveControlPoint : Point;
	        var cornerPoint : Point;
			
			if( type == SOUTH_WEST || type == NORTH_EAST)
			{
	            startPoint = new Point(x,y);
	            curveEndPoint = new Point(x + r, y + r); 
	            if( type == SOUTH_WEST )
	            {
		            curveControlPoint = new Point(x, y + r); 
	   				cornerPoint = new Point(x, y + r); 
	            }
	            else if( type == NORTH_EAST )
	            {
		            curveControlPoint = new Point(x + r, y); 
		            cornerPoint = new Point(x + r, y); 
	            }
			}
			else if ( type == SOUTH_EAST || type == NORTH_WEST )
			{
				startPoint = new Point(x + r, y);
	            curveEndPoint = new Point(x, y + r);
	            
	            if( type == SOUTH_EAST )
	            {
		            curveControlPoint = new Point(x + r, y + r);
		            cornerPoint = new Point(x + r, y + r);
	            }
	            else if ( type == NORTH_WEST )
				{
		            curveControlPoint = new Point(x, y);
		            cornerPoint = new Point(x, y);
				}
			}
			
			drawOuterQuadrantFromPoints( g, startPoint, curveEndPoint, curveControlPoint, cornerPoint );
		}
		
		
		private static function drawOuterQuadrantFromPoints( g: Graphics, startPoint : Point, curveEndPoint : Point, curveControlPoint : Point, cornerPoint : Point ) : void
		{
				g.moveTo(startPoint.x, startPoint.y);
	            g.curveTo(curveControlPoint.x, curveControlPoint.y, curveEndPoint.x, curveEndPoint.y);
	            g.lineTo(cornerPoint.x, cornerPoint.y );
	            g.lineTo(startPoint.x, startPoint.y);
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: