home *** CD-ROM | disk | FTP | other *** search
/ MS DOS Archives 1 / MS-DOS_Archives_Volume_One_Walnut_Creek.iso / msdos / graphics / grafxlib.arc / GRAFEX1.C < prev    next >
C/C++ Source or Header  |  1987-08-31  |  1KB  |  72 lines

  1. /*
  2.  * grafex1.c
  3.  *
  4.  * example program using the Grafix library. draws an n-gon and all
  5.  * its diagonals.
  6.  *
  7.  * compile with:
  8.  *  MSC - msc grafex1;
  9.  *        link grafex1,,,grafix
  10.  *
  11.  *  TC  - tcc grafex1 <grafix lib directory>\grafix.lib
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <math.h>
  17. #include "graf.h"
  18.  
  19. #define pi 3.1415926
  20. #define MAXVERT 100
  21.  
  22. main()
  23. {
  24.   int n, i, j, d;
  25.   unsigned c;
  26.   struct g_info inf;
  27.   struct {
  28.     int x, y;
  29.   } vert[MAXVERT];
  30.  
  31.   printf("n: ");
  32.   scanf("%d", &n);
  33.   if (n < 2 || n > MAXVERT) {
  34.     printf("Oop Ack!\n");
  35.     exit(1);
  36.   }
  37.  
  38.   g_init(0);
  39.   g_open(CGA_320);    /* use 4-color mode on CGA. on an EGA, the mode   */
  40.             /* parameter is ignored and 16-color mode is used */
  41.   g_info(&inf);
  42.  
  43. /* space n vertices equally on an ellipse that fits nicely on the screen */
  44.  
  45.   for (i=0; i<n; i++) {
  46.     vert[i].x = inf.xsize/2 + (inf.xsize/3)*sin(2*pi/n*i);
  47.     vert[i].y = inf.ysize/2 - (inf.ysize/3)*cos(2*pi/n*i);
  48.   }
  49.  
  50. /*
  51.  * draw the figure. the colors are selected so that points that are the
  52.  * same distance from each other on the ellipse have the same color.
  53.  */
  54.  
  55.   for (i=0; i<n; i++)
  56.     for (j=i+1; j<n; j++) {
  57.       d = j-i;
  58.       if (d > n/2) d = n - d;
  59.       c = (float)d/(n/2+1)*inf.colormax + 1;
  60.       g_line(vert[i].x, vert[i].y, vert[j].x, vert[j].y, c);
  61.     }
  62.  
  63. /* wait... */
  64.  
  65.   g_writestr(0, 0, "Press any key...", 1, -1);
  66.   getch();
  67.  
  68. /* and clean up. */
  69.  
  70.   g_close();
  71. }
  72.