#import "Circle.h"
#import "draw.h"
#import <appkit/nextstd.h>
#import <dpsclient/wraps.h>
#import <math.h>

@implementation Circle : Graphic

- (float)naturalAspectRatio
/*
 * The natural aspect ratio of an oval is 1.0 (a circle).
 */
{
    return 1.0;
}

- draw
{
    if (bounds.size.width < 1.0 || bounds.size.height < 1.0) return self;

    if (!NXEqualColor([self fillColor], NX_COLORCLEAR)) {
	if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) PSgsave();
	[self setFillColor];
	PSFilledOval(bounds.origin.x, bounds.origin.y,
		     bounds.size.width, bounds.size.height);
	if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) PSgrestore();
    }
    if (!NXEqualColor([self lineColor], NX_COLORCLEAR)) {
	[self setLineColor];
	PSFramedOval(bounds.origin.x, bounds.origin.y,
		     bounds.size.width, bounds.size.height);
    }

    return self;
}

- (BOOL)hit:(const NXPoint *)p
/*
 * Hit only if inside the interior of the oval.
 */
{
    NXCoord x, y;
    NXPoint center;
    double angle, radius, diameter;

    if ([super hit:p]) {
	center.x = bounds.origin.x + bounds.size.width / 2.0;
	center.y = bounds.origin.y + bounds.size.height / 2.0;
	diameter = MIN(bounds.size.width, bounds.size.height);
	x = fabs(center.x - p->x) / (bounds.size.width / diameter);
	y = fabs(center.y - p->y) / (bounds.size.height / diameter);
	angle = atan2(y, x);
	radius = diameter / 2.0;
	return(x < radius * cos(angle) && y < radius * sin(angle));
    } else {
	return NO;
    }
}


@end

