/* Matrix_and_Polygon.c */
/* Paul Boersma, November 7, 1995 */

#include "Matrix_and_Polygon.h"

Polygon Matrix_to_Polygon (I) {
	iam (Matrix);
	Polygon thee;
	if (my nx != 2 && my ny != 2)
		return Melder_errorp ("Matrix_to_Polygon: matrix must have exactly 2 rows or columns.");
	if (my ny == 2) {
		if (! (thee = Polygon_create (my nx))) return NULL;
		NUMfvector_copyElements (my z [1], thy x, 1, my nx);
		NUMfvector_copyElements (my z [2], thy y, 1, my nx);
	} else {
		long i;
		if (! (thee = Polygon_create (my ny))) return NULL;
		for (i = 1; i <= my ny; i ++) {
			thy x [i] = my z [i] [1];
			thy y [i] = my z [i] [2];
		}
	}
	return thee;
}

Matrix Polygon_to_Matrix (I) {
	iam (Polygon);
	Matrix thee = Matrix_create (1, my numberOfPoints, my numberOfPoints, 1, 1, 1, 2, 2, 1, 1);
	if (! thee) return NULL;
	NUMfvector_copyElements (my x, thy z [1], 1, my numberOfPoints);
	NUMfvector_copyElements (my y, thy z [2], 1, my numberOfPoints);
	return thee;
}

/* End of file Matrix_and_Polygon.c */
