/*
 * PLCircle.j
 * Tutorial-Drawing-part2
 *
 * Created by Philippe Laval on 2009/04/21.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import "PLGraphic.j"

@implementation PLCircle : PLGraphic

- (CGPath)bezierPathForDrawing {

	var path = CGPathCreateMutable();
	var rect = [self bounds];
	var useArc = NO;			// Bug for Opera 9.64 : can not use CGPathAddArc.

	if ((CGRectGetWidth(rect) == CGRectGetHeight(rect)) && useArc)
	{
		CGPathAddArc(path, nil, CGRectGetMidX(rect), CGRectGetMidY(rect), CGRectGetWidth(rect) / 2.0, 0.0, 2.0 * Math.PI, YES);
	}
	else
	{
		var KAPPA = 4.0 * ((Math.SQRT2 - 1.0) / 3.0);
		
	    var axis = CGSizeMake(CGRectGetWidth(rect) / 2.0, CGRectGetHeight(rect) / 2.0),
	        center = CGPointMake(CGRectGetMinX(rect) + axis.width, CGRectGetMinY(rect) + axis.height);
	
	    CGPathMoveToPoint(path, nil, center.x, center.y - axis.height);
	
	    CGPathAddCurveToPoint(path, nil, center.x + (KAPPA * axis.width), center.y - axis.height,  center.x + axis.width, center.y - (KAPPA * axis.height), center.x + axis.width, center.y);
	    CGPathAddCurveToPoint(path, nil, center.x + axis.width, center.y + (KAPPA * axis.height), center.x + (KAPPA * axis.width), center.y + axis.height, center.x, center.y + axis.height);
	    CGPathAddCurveToPoint(path, nil, center.x - (KAPPA * axis.width), center.y + axis.height, center.x - axis.width, center.y + (KAPPA * axis.height), center.x - axis.width, center.y);
	    CGPathAddCurveToPoint(path, nil, center.x - axis.width, center.y - (KAPPA * axis.height), center.x - (KAPPA * axis.width), center.y - axis.height, center.x, center.y - axis.height);
	}

	CGPathCloseSubpath(path);

    return path;
}

// Better thing to implement witc cappuccino
/*
- (BOOL)isContentsUnderPoint:(CPPoint)point {
    // Just check to see if the point is in the path.
    return [[self bezierPathForDrawing] containsPoint:point];
}
*/

@end