#ifndef lint
static char *sccsid = "@(#)line.c	1.1 (Steve Hill) 3/9/90";
#endif

/* line.c
 *
 * Routines to create and manipulate lines.  A line is a point, and
 * a vector that passes through that point.
 * A line is defined by the parametric equation:
 *
 * (x0 + Vx t, y0 + Vy t, Vz t)
 *
 */

#include <stdio.h>

#include "basetype.h"
#include "cartesian.h"
#include "malloc.h"
#include "error.h"
#include "matrix.h"
#include "point.h"
#include "vector.h"
#include "line.h"


/* Line
 *
 * Create a line from a point and a vector.
 */

line_t *
Line(point, vector)
point_t		*point;
vector_t	*vector;
{
	line_t	*line;

	line = (line_t *) malloc(sizeof(line_t));

	if (line == LineNull)
		FatalError(__FILE__, __LINE__, "Line: Out of memory");

	line->point  = point;
	line->vector = vector;

	return(line);
}


/* LineFree
 *
 * Free a line (also free the vector and point)
 */

void
LineFree(line)
line_t	*line;
{
	if (line == LineNull)
		return;

	VectorFree(line->vector);
	PointFree(line->point);
	free((char *) line);
}


/* LineInstantiate
 *
 * Given a parameter and a line, find the point.
 */

point_t *
LineInstantiate(t, line)
real_t	t;
line_t	*line;
{
	return(Point(
			line->point->x + t * line->vector->x,
			line->point->y + t * line->vector->y,
			line->point->z + t * line->vector->z
		     ));
}


/* LinePrint
 *
 * Print out a line to a file.
 */

void
LinePrint(file, line)
FILE	*file;
line_t	*line;
{
	if (line == LineNull)
	{
		fprintf(file, "Null Line\n");
		return;
	}

	PointPrint(file, line->point);
	VectorPrint(file, line->vector);
}
