/*
		Copyright (c) Uwe Hoffmann, 1995.
                  All Rights Reserved.

Filename: MiscCircleShape.m
Author:   Uwe Hoffmann
Date:	  Sep 07, 1995

$Id: MiscCircleShape.m,v 1.0 1995/09/07 11:22:00 Uwe Hoffmann Exp $
 $Log: MiscCircleShape.m,v $
*/

#import <appkit/appkit.h>
#import "MiscCircleShape.h"
#import "MiscUserPath.h"
#import "MiscHitPath.h"


@implementation MiscCircleShape 

- (NSRect)innerBounds
{
	return [path bounds];
}

- (MiscShapeType)shapeType
{
	return MiscCircleShapeType;
}

- (void)calcIntersection:(NSPoint *)ip angle:(float *)alpha toPoint:(NSPoint)aPoint
{
	NSPoint middle,pos;
	float s,x0,y0,a,b,width,height,*bbox;
	
	bbox = [path bbox];
	pos.x = bbox[0];
	pos.y = bbox[1];
	width = bbox[2] - bbox[0];
	height = bbox[3] - bbox[1];
	middle.x = pos.x + width / 2;
	middle.y = pos.y + height / 2;
	if(aPoint.y == middle.y){
		*alpha = 0;
		ip->y = middle.y;
		if(aPoint.x > middle.x)
			ip->x = pos.x + width;
		else
			ip->x = pos.x;
		return;
	}
	if(aPoint.x == middle.x){
		ip->x = middle.x;
		if(aPoint.y > middle.y){
			ip->y = pos.y + height;
			*alpha = -90;
		} else {
			ip->y = pos.y;
			*alpha = 90;
		}
		return;
	}
	s = (aPoint.y - middle.y) / (aPoint.x - middle.x);
	*alpha = 57.29577951 * atan(s);
	if(aPoint.y > middle.y){
		if(aPoint.x > middle.x){ 						// 4. quadrant
			x0 = middle.x;
			y0 = middle.y;
			a = width / 2;
			b = height / 2;
			ip->x = a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + x0;
			ip->y = (2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			if(ip->y < y0){
				ip->x = -(a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2))) + x0;
				ip->y = (-2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			}
		} else { 										// 3. quadrant
			x0 = middle.x;
			y0 = middle.y;
			a = width / 2;
			b = height / 2;
			ip->x = a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + x0;
			ip->y = (2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			if(ip->y < y0){
				ip->x = -(a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2))) + x0;
				ip->y = (-2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			}
		}
	} else {
		if(aPoint.x > middle.x){ 						// 1. quadrant
			x0 = middle.x;
			y0 = middle.y;
			a = width / 2;
			b = height / 2;
			ip->x = a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + x0;
			ip->y = (2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			if(ip->y > y0){
				ip->x = -(a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2))) + x0;
				ip->y = (-2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			}
		} else { 										// 2. quadrant
			x0 = middle.x;
			y0 = middle.y;
			a = width / 2;
			b = height / 2;
			ip->x = a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + x0;
			ip->y = (2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			if(ip->y > y0){
				ip->x = -(a*b/sqrt(pow(b,2) + pow(a,2)*pow(s,2))) + x0;
				ip->y = (-2*a*b*s/sqrt(pow(b,2) + pow(a,2)*pow(s,2)) + 2*y0)/2;
			}
		}
	}
}


- (void)makePathWithBounds:(NSRect)aRect
{
	[path moveto:(aRect.origin.x + aRect.size.width) :(aRect.origin.y + aRect.size.height / 2)];
	[path curveto:(aRect.origin.x + aRect.size.width) :(aRect.origin.y + aRect.size.height / 2 * 0.445)
						:(aRect.origin.x + aRect.size.width / 2 * 1.555)  :aRect.origin.y
						:(aRect.origin.x + aRect.size.width / 2) :aRect.origin.y];
	[path curveto:(aRect.origin.x + aRect.size.width / 2 * 0.445) :aRect.origin.y
						:aRect.origin.x :(aRect.origin.y + aRect.size.height / 2 * 0.445)
						:aRect.origin.x :(aRect.origin.y + aRect.size.height / 2)];
	[path curveto:aRect.origin.x :(aRect.origin.y + aRect.size.height /2 * 1.555)
						:(aRect.origin.x + aRect.size.width / 2 * 0.445) :(aRect.origin.y + aRect.size.height)
						:(aRect.origin.x + aRect.size.width / 2) :(aRect.origin.y + aRect.size.height)];
	[path curveto:(aRect.origin.x + aRect.size.width / 2 * 1.555) :(aRect.origin.y + aRect.size.height)
						:(aRect.origin.x + aRect.size.width) :(aRect.origin.y + aRect.size.height / 2 * 1.555)
						:(aRect.origin.x + aRect.size.width) :(aRect.origin.y + aRect.size.height / 2)];
}
			
- (void)fillParamsWithBounds:(NSRect)aRect
{
	float *params, *bbox;
	
	params = [path params];
	bbox = [path bbox];
	bbox[0] = aRect.origin.x;
	bbox[1] = aRect.origin.y;
	bbox[2] = aRect.origin.x + aRect.size.width;
	bbox[3] = aRect.origin.y + aRect.size.height;
	params[0] = aRect.origin.x + aRect.size.width;
	params[1] = aRect.origin.y + aRect.size.height / 2;
	params[2] = aRect.origin.x + aRect.size.width;
	params[3] = aRect.origin.y + aRect.size.height / 2 * 0.445;
	params[4] = aRect.origin.x + aRect.size.width / 2 * 1.555;
	params[5] = aRect.origin.y;
	params[6] = aRect.origin.x + aRect.size.width / 2; 
	params[7] = aRect.origin.y;
	params[8] = aRect.origin.x + aRect.size.width / 2 * 0.445;
	params[9] = aRect.origin.y;
	params[10] = aRect.origin.x;
	params[11] = aRect.origin.y + aRect.size.height / 2 * 0.445;
	params[12] = aRect.origin.x;
	params[13] = aRect.origin.y + aRect.size.height / 2;
	params[14] = aRect.origin.x;
	params[15] = aRect.origin.y + aRect.size.height /2 * 1.555;
	params[16] = aRect.origin.x + aRect.size.width / 2 * 0.445;
	params[17] = aRect.origin.y + aRect.size.height;
	params[18] = aRect.origin.x + aRect.size.width / 2;
	params[19] = aRect.origin.y + aRect.size.height;
	params[20] = aRect.origin.x + aRect.size.width / 2 * 1.555;
	params[21] = aRect.origin.y + aRect.size.height;
	params[22] = aRect.origin.x + aRect.size.width;
	params[23] = aRect.origin.y + aRect.size.height / 2 * 1.555;
	params[24] = aRect.origin.x + aRect.size.width;
	params[25] = aRect.origin.y + aRect.size.height / 2;
}

+ (NSSize)calcSizeForInnerSize:(NSSize)aSize
{
	return aSize;
}
		              
@end


	
