
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************
	9/22/94 - add findchord -rrt

*/

/*	
	places the box variables in the parameter pointers
	returns 0 if box is found, 1 if not.
*/

#include "pcvista.h"
#include <stdio.h>
#include <math.h>

int
getbox(num, psr, psc, pnr, pnc)
int num, *psr, *psc, *pnr, *pnc;
{
	char buf[NBUF], *found;

	sprintf(buf, "box%d", num);
	if ((found = getsym(buf)) == NULL)
		return(1);
	sscanf(found, "%d %d %d %d", psr, psc, pnr, pnc);
	return(0);
}

void 
check_box(boxno, nrow, ncol)
int boxno, nrow, ncol;
{
	int sr, sc, nr, nc;
	
	if (getbox(boxno, &sr, &sc, &nr, &nc))
		error(-1, "no box defined");

	if ((sr + nr) > nrow)
		error(-1, "number of rows in the box larger than image size");
	if ((sc + nc) > ncol)
		error(-1, "number of cols in the box larger than image size");
}


int
getcircle(num, pcr, pcc, prad)
int num, *pcr, *pcc, *prad;
{
	char buf[NBUF], *found;

	sprintf(buf, "circle%d", num);
	if ((found = getsym(buf)) == NULL)
		return(1);
	sscanf(found, "%d %d %d", pcr, pcc, prad);
	return(0);
}

void 
check_circle(circleno, nrow, ncol)
int circleno, nrow, ncol;
{
	int cr, cc, rad;
	
	if (getcircle(circleno, &cr, &cc, &rad))
		error(-1, "no circle defined");

	if ((cr + rad) > nrow)
		error(-1, "number of rows in the circle larger than image size");
	if ((cc - rad) < 0)
		error(-1, "cc too close to edge");
	if ((cr - rad) < 0)
		error(-1, "cr too close to edge");
	if ((cc + rad) > ncol)
		error(-1, "number of cols in the circle larger than image size");
}

/*	Returns the integer chord length of the circle
*/


int find_chord(dr,rad)
int dr,rad;
{
	double c;

	c=sqrt((double)rad*rad - (double)dr*dr +SMALL);
	return (int)( c+0.5);
}

