/*************************************************************************
Copyright(c) 2004-2006 Analog Devices, Inc. All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.

File Name: adi_graph_rect.c 

Description - Part of 'adi_graph' library.  See readme file for details.

*********************************************************************************/

#include <math.h>
#include "adi_graph.h"

void ADI__Rect ( ADI_sGraphPrim *gp, bool fill )
{
	int x, y;
	int x1 = gp->x1;
	int x2 = gp->x2;
	int y1 = gp->y1;
	int y2 = gp->y2;
	
	/* Make sure the "1" values are lower than the "2" values */

	ADI_SwapIfGreaterThan ( x1, x2 );
	ADI_SwapIfGreaterThan ( y1, y2 );

	if ( fill )
	{
		/* Optomizing for ADI_FILL_SOLID fills */
		
		void (*fp)(ADI_sGraphPrim *) = &ADI_Point;
		
		if ( ADI_FILL_SOLID != gp->fill_style )
		{
			fp = &ADI_PointConsideringFillPattern;
		}
		
		for ( x = x1; x <= x2; ++x )
			for ( y = y1; y <= y2; ++y  )
			{
				gp->x1 = x;
				gp->y1 = y;
				(*fp) ( gp );
			}
	}
	else
	{
		int dash_seed = 0;
		
		/* Draw clockwise from NW corner (Upper, right, bottom, left) */
		gp->x1 = x1;
		gp->y1 = y2;
		gp->x2 = x2;
		gp->y2 = y2;
		ADI_Line ( gp, &dash_seed );
		
		gp->x1 = x2;
		gp->y1 = max (y2-1, y1);
		gp->x2 = x2;
		gp->y2 = y1;
		ADI_Line ( gp, &dash_seed );

		gp->x1 = max (x2-1, x1);
		gp->y1 = y1;
		gp->x2 = x1;
		gp->y2 = y1;
		ADI_Line ( gp, &dash_seed );

		gp->x1 = x1;
		gp->y1 = y1;
		gp->x2 = x1;
		gp->y2 = max (y2-1, y1);
		ADI_Line ( gp, &dash_seed );
		
	}
}

void ADI_RectFill ( ADI_sGraphPrim *gp )
{
	ADI__Rect ( gp, true );
}

void ADI_Rect ( ADI_sGraphPrim *gp )
{
	ADI__Rect ( gp, false );
}


