#include <fitplot.h>
#include <math.h>

void FindRange
	(
	double max, 
	double min,
	double *inc, 
	double *start, 
	double *end
	)
{
	double range;

	if ((max == 0) && (min == 0))
		{
		max = 1;
		min = -1;
		}
	
	if ((fabs(max-min) > fabs(min)) ||
		(fabs(max-min) > fabs(max)) ||
		(max == min))
		{
		if (min >= 0) 
			{
			if (max == min)
				{
				max *= 2;
				}
			range=max;

			*start = 0;
			double tempInc = pow(10,( floor(log10(range)) ) );
			*inc = tempInc;
			int count = 0;
			*end = 0;
			while (*end < max)
				{
				count++;
				*end = *inc * count;
				}
			if (count <= 3)
				{
				*inc = tempInc / 2.0;
				count = 0;
				*end = 0;
				while (*end < max)
					{
					count++;
					*end = *inc * count;
					}
				if (count < 3)
					{
					*inc = tempInc / 5.0;
					count = 0;
					*end = 0;
					while (*end < max)
						{
						count++;
						*end = *inc * count;
						}
					
					}
				}
				
			} 
		else 
			{
			if (max <= 0)
				{
				if (max == min)
					{
					min *= 2;
					}
				range = fabs(min);
				
				*end = 0;
				double tempInc = pow(10,( floor(log10(range)) ) );
				*inc = tempInc;
				int count = 0;
				*start = 0;
				while (*start > min)
					{
					count++;
					*start = -(*inc * count);
					}
				if (count <= 3)
					{
					*inc = tempInc / 2.0;
					count = 0;
					*start = 0;
					while (*start > min)
						{
						count++;
						*start = -(*inc * count);
						}
					if (count < 3)
						{
						*inc = tempInc / 5.0;
						count = 0;
						*start = 0;
						while (*start > min)
							{
							count++;
							*start = -(*inc * count);
							}
						
						}
					}
				}
			else
				{
				range=max-min;
				double tempInc = pow(10,( floor(log10(range)) ) );
				*inc = tempInc;
				int pcount = 0;
				int ncount = 0;
				*start = 0;
				*end = 0;
				while (*end < max)
					{
					pcount++;
					*end = *inc * pcount;
					}
				while (*start > min)
					{
					ncount++;
					*start = -(*inc * ncount);
					}
					
				if (pcount + ncount <= 6)
					{
					*inc = tempInc / 2.0;
					pcount = 0;
					ncount = 0;
					*start = 0;
					*end = 0;
					while (*end < max)
						{
						pcount++;
						*end = *inc * pcount;
						}
					while (*start > min)
						{
						ncount++;
						*start = -(*inc * ncount);
						}	
						
					if (pcount + ncount < 6)
						{
						*inc = tempInc / 5.0;
						pcount = 0;
						ncount = 0;
						*start = 0;
						*end = 0;
						while (*end < max)
							{
							pcount++;
							*end = *inc * pcount;
							}
						while (*start > min)
							{
							ncount++;
							*start = -(*inc * ncount);
							}
						}
					}
				else if (pcount + ncount > 10)
					{
					*inc = tempInc * 2.0;
					pcount = 0;
					ncount = 0;
					*start = 0;
					*end = 0;
					while (*end < max)
						{
						pcount++;
						*end = *inc * pcount;
						}
					while (*start > min)
						{
						ncount++;
						*start = -(*inc * ncount);
						}	
					}				
				}
			}
		} 
	else 
		{
		if (min >= 0) 
			{
			range=max-min;
			double tempInc = pow(10,( floor(log10(range)) ) );
			*inc = tempInc;
			int count = 0;
			*start = 0;
			*end = 0;
			while ((*inc * (count+1)) < min)
				{
				count++;
				*start = *inc * count;
				}
			*end = *start;
			count = 0;
			while (*end < max)
				{
				count++;
				*end = *start + *inc * count;
				}
			if (count <= 3)
				{
				*inc = tempInc / 2.0;
				count = 0;
				*start = 0;
				*end = 0;
				while ((*inc * (count+1)) < min)
					{
					count++;
					*start = *inc * count;
					}
				*end = *start;
				count = 0;
				while (*end < max)
					{
					count++;
					*end = *start + *inc * count;
					}
				if (count < 3)
					{
					*inc = tempInc / 5.0;
					count = 0;
					*start = 0;
					*end = 0;
					while ((*inc * (count+1)) < min)
						{
						count++;
						*start = *inc * count;
						}
					*end = *start;
					count = 0;
					while (*end < max)
						{
						count++;
						*end = *start + *inc * count;
						}
					}
				}
			} 
		else 
			{
			range=fabs(max-min);
			double tempInc = pow(10,( floor(log10(range)) ) );
			*inc = tempInc;
			int count = 0;
			*start = 0;
			*end = 0;
			while (-(*inc * (count+1)) > max)
				{
				count++;
				*end = -(*inc * count);
				}
			*start = *end;
			count = 0;
			while (*start > min)
				{
				count++;
				*start = *end - *inc * count;
				}
			if (count <= 3)
				{
				*inc = tempInc / 2.0;
				count = 0;
				*start = 0;
				*end = 0;
				while (-(*inc * (count+1)) > max)
					{
					count++;
					*end = -(*inc * count);
					}
				*start = *end;
				count = 0;
				while (*start > min)
					{
					count++;
					*start = *end - *inc * count;
					}
				if (count < 3)
					{
					*inc = tempInc / 5.0;
					count = 0;
					*start = 0;
					*end = 0;
					while (-(*inc * (count+1)) > max)
						{
						count++;
						*end = -(*inc * count);
						}
					*start = *end;
					count = 0;
					while (*start < max)
						{
						count++;
						*start = *end - *inc * count;
						}
					}
				}			
			}
		}
}
