

/*
**  Lathe Program
*/

#include <stdio.h>

float rot = 180.0;
float x[35][35], y[35][35], z[35][35];
float ix[35][1], iy[35][1];
int   first_end, second_end, npolys=4, npts=4;

float sin(), cos();
FILE *fopen(), *fp;

main()
{
int verts, polys, i;
float xx, yy;
char filename[64];

  input_vali("Number of Points ? ", &npts);

  for (i = 0; i<npts; i++) {
    printf("Point %d\n", i+1);
    xx=i+1; yy=i+1;
    input_valf("X value - ", &xx);
    input_valf("Y value - ", &yy);
    ix[i][0] = xx;
    iy[i][0] = yy;
  }

  input_valf("Number of Degrees of Rotation ? ", &rot);
  input_vali("Number of Polygons in Rotation ? ", &npolys);
  input_vali("Close the First End (0=no, 1=yes) ? ", &first_end);
  input_vali("Close the Second End (0=no, 1=yes) ? ", &second_end);

  /* open */
  verts = npolys + 1;
  verts = verts * npts;
  polys = npts - 1;
  polys = polys * npolys;

  input_string("Filename to write ? ", &filename[0]);
  if (strlen(&filename[0]) == 0) {
    printf("Missing filename\n");
    exit();
  }
  if ((fp = fopen(&filename, "w")) == NULL) {
    printf("ERROR OPENING FILE\n");
    exit();
  }

  fprintf(fp, "1 1 2 %d %d ", polys, verts);

  rotate_vertices();
  list_vertices();
  fclose(fp);
  printf("Done.\n");
}

rotate_vertices()
{
float rotperpoly, radrotperpoly, totrot, radians;
int rr, ii;
float yy, work;

  radians = 6.28 / 360;
  rotperpoly = rot / npolys;
  radrotperpoly = rotperpoly * radians;
  totrot = 0;

  for (rr=0; rr<=npolys; rr++) {
    for (ii=0; ii<npts; ii++) {
      x[ii][rr] = ix[ii][0];
      yy = iy[ii][0];
      work = cos(totrot);
      y[ii][rr] = work * yy;
      work = sin(totrot);
      z[ii][rr] = work * yy;
      fprintf(fp,"%f %f %f ", x[ii][rr], y[ii][rr], z[ii][rr]);
    }
  totrot = totrot + radrotperpoly;
  }
}

list_vertices()
{
int np, ii, limit;

  limit = npolys-1;
  limit = npts * limit;
  limit = limit + 1;

  for (np=1; np <= limit; np=np+npts) {
    for (ii=0; ii<=npts-2; ii++) {
      fprintf(fp,"5 ");
      fprintf(fp,"%d ", np+ii);
      fprintf(fp,"%d ", np+npts+ii);
      fprintf(fp,"%d ", np+npts+1+ii);
      fprintf(fp,"%d ", np+1+ii);
      fprintf(fp,"%d ", np+ii);
    }
  }
}


/*
**  input routines
*/

input_vali(prompt,value)
int *value;
char prompt[];
{
int val;
char character;

  printf("%s <%d> - ",prompt,*value);
  scanf("%d",&val);
  if (val != -1) *value = val;
  scanf("%c", &character);           /* eat up c/r, why though? */
}

input_valf(prompt,value)
float *value;
char prompt[];
{
char character;
float val;

  printf("%s <%f> - ",prompt,*value);
  scanf("%lf",&val);
  if (val != -1) *value = val;
  scanf("%c", &character);           /* eat up c/r, why though? */
}

input_string(prompt, string)
char prompt[];
char *string[];
{
char character;
char work_string[64];
int cnt = 0;

  character = 1;
  printf("%s", prompt);
  while (character != NULL) {
    scanf("%c", &character);
    if (character == '\n')
      character = NULL;
    work_string[cnt] = character;
    cnt = cnt + 1;
  }
  strcpy(string, &work_string);           /* string is already pointer */
}


