/* CIRPLANE	Circular plane generator for VS3D Version 1.00
 *
 *	Generates a clockwise circular polygon with the specified number
 *	of vertices.  For use "capping" a cylinder or as surface detail.
 *
 *	The resultant circular polygon is resting at ground zero (y=0)
 *	and has a radius of 1 such that (-1 <= x <= 1) and (-1 <= z <= 1)
 *
 *	Use:
 *
 *		CLI> CIRPLANE  filename
 *		VS3D 3DG1 Normalized Circular Plane Generator
 *		Copyright (c) 1987 by Thad Floryan
 *		Number of points (3:200)= <n>
 *		Enter the surface color = <n>
 *
 *	where:	filename is typically "geo/name".  You MUST type the path.
 *		color is that used with the EGG and OCT programs.
 *
 *	Copyright (c) 1987 by Thad Floryan.     9-Aug-1987
 *
 * This program, and the source for it, are copyrighted, and are not to be
 * considered in the public domain.  Never-the-less, this program may be
 * copied magnetically or electronically without any additional permission,
 * provided no charge is requested for the program; the only fees permitted
 * are a moderate copying charge, in the case of magnetic distribution (such
 * as distribution by Fred Fish or Amicus), or any normal BBS charges in the
 * case of an electronic download.  Commercial and other uses are negotiable
 * by contacting me.  Feel welcome to use any part of the source for your
 * personal learning.
 *
 *	Building instructions (Manx Aztec C V3.4b2, with IEEE math):
 *
 *		cc +fi cirplane
 *		ln cirplane -lma -lc
 */


#include <stdio.h>
#include <functions.h>

static double radians	  = {6.2831853071795864769};	/* 2 PI */
static double radius	  = {1};
static int valid_colors[] = { 0,  1,  2,  4,  6,  7,  8,  9, 10, 12, 14, 15,
			     16, 17, 18, 20, 22, 23, 24, 25, 26, 28, 30, 31,
			     32, 33, 34, 36, 38, 39, 40, 41, 42, 44, 46, 47,
			     48, 49, 50, 52, 54, 55, 56, 57, 58, 60, 62, 63,
			     -1 };

main(argc,argv)
   int argc;
   char *argv[];
{
   double sin(), cos();

   FILE	*output;
   double step, angle, x, z;
   int i, points, color, abs_color;
   
   printf("VS3D 3DG1 Normalized Circular Plane Generator\n");
   printf("Copyright © 1987 by Thad Floryan\n\n");

   if ( argc != 2 ) {
     printf("Generates a circular polygon with the specified number ");
     printf("of vertices for use\ncapping a cylinder or as surface ");
     printf("detail.  The resultant polygon is resting\nat ground zero ");
     printf("(y=0) and radius=1 such that -1 <= x <= 1 and -1 <= z <= 1.\n");
     printf("\nUsage:\tCLI> %s \033[1mfilename\033[0m\n\n", argv[0]);
     printf("where:\t\033[1mfilename\033[0m is typically `geo/name'; ");
     printf("you \033[1mmust\033[0m type the full path name\n\n");
     printf("You will be prompted for the number of points (vertices ");
     printf("of the polygon) and\none color from among those recognized ");
     printf("by `VideoScape 3D'® and its attendant\nsupport programs ");
     printf("(e.g. EGG, OCT).\n\nOther geometry and utility programs ");
     printf("are planned.  Your ideas, suggestions\nand bug reports are ");
     printf("welcome; I can be contacted on the following systems:\n\n");

     printf("\tAMIC\tThad Floryan\t707/579-0523 (3/12/24)\n");
     printf("\tAmigaOZ\tThad Floryan\t316/283-9210 (3/12/24)\n");
     printf("\tBBS-HT\tThad Floryan\t408/737-0900 (3/12)\n");
     printf("\t\033[1mBBS-JC\033[0m\tThad Floryan\t415/961-7250 (3/12/24)\n");
     printf("\tFAUG\tThad Floryan\t415/595-2479 (3/12)\n");
     printf("\tTemple\tThad Floryan\t408/241-7802 (3/12/24)\n");
     printf("\tBIX\ttfloryan\tTymnet: `bix'\n");
     printf("\tDELPHI\tTHADF\t\tTelenet: `c delphi', or 617/576-0862\n");
     printf("\tuucp\tthad\t\tUSENET ...!well!thad\n\n");

     printf("\033[1mBBS-JC\033[0m is `home base' and is available via ");
     printf("PCPursuit's 408 node\n");
      exit();
   }

   if ((output = fopen(argv[1], "w")) == NULL) {
      printf("Problem opening `%s' for output, aborted\n", argv[1]);
      exit(20);
   }
   
   printf("Number of points (3:200)= \033[33m"); scanf("%d", &points);

   if((points < 3) || (points > 200)) {
      printf("\033[0mIncorrect number of points (%d), aborted\n", points);
      fclose(output);
      exit(20);
   }

   printf("\033[0mEnter the surface color = \033[33m"); scanf("%d", &color);
   abs_color = ( color >= 0) ? color : -color;	/* abs(color) */
   for(i = 0; i <= 48; i++) {
      if (abs_color == valid_colors[i]) break;
      if (-1 == valid_colors[i]) {
         printf("\033[0mInvalid color (%d), aborted\n", color);
         fclose(output);
         exit(20);
      }
   }
   
/* output 3DG1 header information
 */
   fprintf(output, "3DG1\n%d\n", points);

/* output the coordinates of each polygon vertex per:
 *
 *	x = cos(angle) * radius
 *      y = 0
 *      z = sin(angle) * radius
 */
   step = radians/(double)points;
   angle = radians;

   for(i=0; i < points; i++) {
      x = cos(angle) * radius;
      z = sin(angle) * radius;
      fprintf(output, "%f 0 %f\n", x, z);
      angle = angle - step;
   }

/* output the <number of vertices>,<vertex point(n)> ... , <polygon color>
 */
   fprintf(output, "%d ", points);
   for(i=0; i < points; i++) fprintf(output, "%d ", i);
   fprintf(output, "%d\n", color);

/* all done
 */
   fclose(output);
   printf("\033[0mDone.\n");
   exit(0);
}
