home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / animutil / anim13 / grafdemo.c < prev    next >
Text File  |  1990-01-13  |  5KB  |  130 lines

  1. /* grafdemo.c
  2.  
  3. This program demonstrates the use of functions load_array(), scale(),
  4. draw_perimeter(), and plot_curve().
  5. With scale() and its associated functions,
  6. truly device independent graphics programs can be written.
  7.  
  8. Dr. Jon Ahlquist, 24 July 1989, 13 Jan 1990.
  9.  
  10. Copyright 1990 by Jon Ahlquist, Department of Meteorology B-161,
  11. Florida State University, Tallahassee, Florida 32306-3034, USA.
  12. Telephone: (904) 644-1558.
  13. Telnet address: ahlquist@metsat.met.fsu.edu (ahlquist@128.186.5.2)
  14.  
  15. This software may be freely copied without charge.
  16. Copyright is made to prevent anyone from trying to impose restrictions
  17. on this software.
  18. All software and documentation is provided "as is" without warranty
  19. of any kind.
  20.  
  21. Development of this material was sponsored by NSF grant ATM-8714674.
  22. */
  23.  
  24.  
  25. /* Declare prototypes. */
  26. #include <stdio.h>    /* printf(), getchar()   */
  27. #include <conio.h>    /* getch()               */
  28. #include <stdlib.h>   /* exit()                */
  29. #include <graphics.h> /* All Borland graphics. */
  30. #include <grafsupp.h> /* scale(), load_array(),
  31.                          draw_perimeter(), plot_curve(),
  32.                          and macros XX(), YY(), XG(), and YG(). */
  33. #include <math.h>     /* cos()                 */
  34.  
  35. #define  NPTS 25
  36. #define  PI    3.14159
  37.  
  38. void main(void)
  39. {
  40. int  graph_driver, graph_mode, graph_error, i;
  41.  
  42. float  time[NPTS],
  43.        temperature[NPTS],
  44.        xgmin, xgmax, ygmin,   ygmax,   /* Generic coordinate limits. */
  45.        tmin,  tmax,  tempmin, tempmax; /* User    coordinate limits. */
  46.  
  47.  
  48. /* Use any graphics mode that works with the user's computer. */
  49.  
  50. graph_driver = DETECT;
  51. initgraph (&graph_driver, &graph_mode, "c:\\turboc");
  52. if ((graph_error = graphresult())  <  0)
  53.    {
  54.    printf("Error while trying to initialize graphics.\n");
  55.    printf("%s\n", grapherrormsg(graph_error));
  56.    exit(1);
  57.    }
  58.  
  59.  
  60. /*   We'll use "generic" coordinates and "user" coordinates.
  61.  
  62. The vertical "generic" coordinate is 0 at the bottom of the user's
  63. computer monitor screen and is 1 at the top of the screen.
  64. In the horizontal direction, 0 to 1 runs from left to right,
  65. is centered on the screen, and covers the same distance on the screen
  66. that 0 to 1 covers vertically.
  67. (With most video modes, pixels are not square, so the number of pixels
  68. between 0 and 1 in the vertical direction is usually different
  69. from the number of pixels between 0 and 1 in the horizontal direction.)
  70.  
  71.      "User" coordinates are whatever coordinates are convenient
  72. for thinking about the data.
  73. For this example, we'll assume that we want to plot temperature in degrees
  74. Fahrenheit as a function of time in hours,
  75. so  the horizontal user coordinate will be time in hours,
  76. and the vertical   user cooridnate will be temperature in degrees.
  77.  
  78. function scale() establishes the transformation constants that
  79. needed to place a graph of time versus temperature into a window
  80. on the computer graphics screen.
  81.  
  82. Define a rectangular area on the monitor screen for our graph.
  83. Horizontally, the rectangle will extend from
  84. 0.2 to 0.8 in terms of generic coordinates,
  85. and the vertical range will be 0.3 to 0.7.
  86. Our time range is 0 to 24 hours, and
  87. the temperature range is 50 to 70 degrees Fahrenheit.
  88. */
  89.  
  90. /* Define a window in terms of generic coordinates. */
  91.           ygmax = 0.7;
  92. xgmin = 0.2;          xgmax = 0.8;
  93.           ygmin = 0.3;
  94.  
  95. /* Define the user coordinates that will lie at the edges of the window. */
  96.         tempmax = 70.;
  97. tmin  = 0.;           tmax  = 24.;
  98.         tempmin = 50.;
  99.  
  100. /* Establish constants needed to transform from generic and user
  101. coordinates to pixel coordinates. */
  102. scale(xgmin, xgmax, ygmin, ygmax, tmin, tmax, tempmin, tempmax);
  103.  
  104.  
  105. /* Load the time array with uniformly spaced values. */
  106. load_array(time, NPTS, tmin, tmax);
  107.  
  108. /* Load the temperature array. */
  109. for (i=0; i< NPTS; i++)
  110.    temperature[i] = 60. + 10 * cos(2.*PI*(time[i]-14.)/24.);
  111.  
  112. /* Write a title which starts at the left edge of the rectangular area
  113. and sits just above it. */
  114. label_plot("Time versus temperature", "Time in hours", "Temperature");
  115. /*outtextxy (XG(xgmin), YG(ygmax+0.05), "Time in hours versus temperature");*/
  116.  
  117.  
  118. /* Draw a box around the window, including tick marks. */
  119. draw_perimeter(4, 6,  2, 10);
  120.  
  121. /* Plot the temperature.
  122. If some array values exceed the limits used to define the window using scale(),
  123. then some of the plot will lie outside the window. */
  124. plot_curve(time, temperature, NPTS);
  125.  
  126. /* End the program. */
  127. outtextxy(XG(0.0), YG(0.1), "Hit any key to end.");
  128. getch();
  129. closegraph();
  130. } /* End of main(). */