/*	MiscHexagonShape.m

	Copyright 1996 Uwe Hoffmann.

	This notice may not be removed from this source code.
	The use and distribution of this software is governed by the
	terms of the MiscKit license agreement.  Refer to the license
	document included with the MiscKit distribution for the terms.

	Version 2 (August 1996)
*/

#import <Foundation/Foundation.h>

#import "MiscHexagonShape.h"
#import "MiscUserPath.h"
#import "MiscHitPath.h"
#import "MiscTDUtils.h"

@implementation MiscHexagonShape 

- (NSRect)innerBounds
{
	NSRect br,ir;
	NSPoint m;
	float a,b,cx,cy;
	
	br = [path bounds];
	m.x = br.origin.x + br.size.width / 2;
	m.y = br.origin.y + br.size.height / 2;
	a = br.size.width / 2;
	b = br.size.height / 2;
	cx = a * 0.5;
	cy = b * 0.75;
	ir.origin.x = m.x - cx;
	ir.origin.y = m.y - cy;
	ir.size.width = 2 * cx;
	ir.size.height = 2 * cy;
	return ir;
}

- (MiscShapeType)shapeType
{
	return MiscHexagonShapeType;
}

- (void)calcIntersection:(NSPoint *)ip angle:(float *)alpha toPoint:(NSPoint)aPoint
{
	NSPoint middle,l1,l2,pos;
	float s,width,height,*bbox;
	float hexagonWidth, ausdruck;
	
	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){
		ip->y = middle.y;
		if(aPoint.x > middle.x){
			*alpha = 0;		
			ip->x = pos.x + width;
		} else {
			*alpha = 180;		
			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);
	hexagonWidth = width / 3;
	if(aPoint.y > middle.y){
		if(aPoint.x > middle.x){ 						// I. quadrant
			l1.x = middle.x;
			l1.y = l2.y = pos.y + height;
			l2.x = pos.x + width - hexagonWidth;
			if(miscTD_straddles(l1, l2, middle, aPoint)){
				ip->y = l1.y;
				ip->x = middle.x + (ip->y - middle.y) / s;
			} else {
				l1.x = pos.x + width;
				l1.y = middle.y;
				ausdruck = (l2.y - l1.y) / (l2.x - l1.x);
				ip->x = (middle.y + ausdruck * l1.x - s * middle.x - l1.y) / (ausdruck - s);
				ip->y = middle.y + s * (ip->x - middle.x);
			}
		} else { 										// II. quadrant
			l1.x = middle.x;
			l1.y = l2.y = pos.y + height;
			l2.x = pos.x + hexagonWidth;
			if(miscTD_straddles(l1, l2, middle, aPoint)){
				ip->y = l1.y;
				ip->x = middle.x + (ip->y - middle.y) / s;
			} else {
				l1.x = pos.x;
				l1.y = middle.y;
				ausdruck = (l2.y - l1.y) / (l2.x - l1.x);
				ip->x = (middle.y + ausdruck * l1.x - s * middle.x - l1.y) / (ausdruck - s);
				ip->y = middle.y + s * (ip->x - middle.x);
			}
                        *alpha += 180;
		}
	} else {
		if(aPoint.x > middle.x){ 						// IV. quadrant
			l1.x = middle.x;
			l1.y = l2.y = pos.y;
			l2.x = pos.x + width - hexagonWidth;
			if(miscTD_straddles(l1, l2, middle, aPoint)){
				ip->y = l1.y;
				ip->x = middle.x + (ip->y - middle.y) / s;
			} else {
				l1.x = pos.x + width;
				l1.y = middle.y;
				ausdruck = (l2.y - l1.y) / (l2.x - l1.x);
				ip->x = (middle.y + ausdruck * l1.x - s * middle.x - l1.y) / (ausdruck - s);
				ip->y = middle.y + s * (ip->x - middle.x);
			}
		} else { 										// III. quadrant
			l1.x = middle.x;
			l1.y = l2.y = pos.y;
			l2.x = pos.x + hexagonWidth;
			if(miscTD_straddles(l1, l2, middle, aPoint)){
				ip->y = l1.y;
				ip->x = middle.x + (ip->y - middle.y) / s;
			} else {
				l1.x = pos.x;
				l1.y = middle.y;
				ausdruck = (l2.y - l1.y) / (l2.x - l1.x);
				ip->x = (middle.y + ausdruck * l1.x - s * middle.x - l1.y) / (ausdruck - s);
				ip->y = middle.y + s * (ip->x - middle.x);
			}
                        *alpha += 180;
		}
	}
}


- (void)makePathWithBounds:(NSRect)aRect
{
	float hexagonWidth;
	
	hexagonWidth = aRect.size.width / 3;
	[path moveto:aRect.origin.x + hexagonWidth :aRect.origin.y];
	[path rlineto:(aRect.size.width - 2 * hexagonWidth) :0];
	[path rlineto:hexagonWidth :aRect.size.height /2];
	[path rlineto:-hexagonWidth :aRect.size.height /2];
	[path rlineto:(- aRect.size.width + 2 * hexagonWidth) :0];
	[path rlineto:-hexagonWidth :-aRect.size.height / 2];
	[path closepath];
}
			
- (void)fillParamsWithBounds:(NSRect)aRect
{
	float *params, *bbox, hexagonWidth;
	
	hexagonWidth = aRect.size.width / 3;
	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 + hexagonWidth;
	params[1] = aRect.origin.y;
	params[2] = aRect.size.width - 2 * hexagonWidth;
	params[3] = 0;
	params[4] = hexagonWidth;
	params[5] = aRect.size.height / 2;
	params[6] = -hexagonWidth;
	params[7] = aRect.size.height / 2;
	params[8] =-aRect.size.width + 2 * hexagonWidth;
	params[9] = 0;
	params[10] =-hexagonWidth;
	params[11] = -aRect.size.height / 2;
}

+ (NSSize)calcSizeForInnerSize:(NSSize)aSize
{
	NSSize os;
	float a,b,cx,cy;
	
	cx = aSize.width / 2;
	cy = aSize.height / 2;
	a = cx / 0.5;
	b = cy / 0.75;
	os.width = 2 * a;
	os.height = 2 * b;
	return os;
}
		              
@end


	
