/* curve.c Mathematically, this is the same as the program on pp 83-84 of the book. It however contains many enhancements, such as axes, a border, a title, your name, your machine name, the date and the time. The book's for-loop is written once and for all into a subroutine called print_path, which actually resides in the file called routines.c. To make things even more interesting, there is also a header file coords.h which contains a defined data-type called point. Nevertheless, with all the added enhancements, it still doesn't really do anything more than the simple program in the book. If there are features that don't interest you, delete them. This program is to be understood in combination with coords.h and routines.c. There is also a (self-contained) pascal version, curve.p. The program is deliberately organized to output all the features first, so that one can easily see them in the output file. The curve, which is a tedious list of data, is arranged to come last. */ /* Written by W. Taylor, Math Dept, Univ. of Colorado, Boulder,Colo 80309-0426, wtaylor@euclid.colorado.edu. Last revised 9/14/95 */ #include #include #include /* this is needed only to get the day and time */ #include /* this is needed only to get the day and time */ #include "coords.h" #define PI 3.1415926 #define STEP 400 /* Number of divisions of the time interval. Here you can experiment for the best value. Too small and you'll see the corners. Too large and you will have more data to deal with. You can save time for yourself and others by keeping this parameter fairly small for rough drafts. */ /* The curve will be defined parametrically with a parameter t which ranges between two real numbers, which are as follows. Edit these to suit the case at hand. */ #define START_T 0.0 /* This, and others, must be written as double */ #define STOP_T (2*PI) /* The next four numbers give the values of the x and y co-ordinates at the sides of the window which will be printed. These also can be changed to suit the case at hand. */ #define LEFT -2.0 /* value of x at left of window */ #define RIGHT 10.0 #define BOTTOM -6.00 /* value of y at bottom of window */ #define TOP 6.00 #define WIDTH 6 /* Width of the curve we will draw, in pixels, i.e. WIDTH/300 is the width in inches. Change it if you prefer a different width. */ /* Here is where we declare the actual function we are graphing. You can make the right hand sides here (the x and y co-ordinates of the curve) any legal C-expressions. `Point' by the way is merely a defined data type which has two double co-ordinates. Its defininiton resides in the header file coords.h which you need to have in the same directory for this to compile. Feel free here to declare auxiliary variables and add extra lines of calculations to get the desired result. */ point f(t) double t; { point p; p.x_coord = t; p.y_coord = sin(2*t); return (p); } /* If you want a second -- or third, etc. -- graph on the same page, do the following: point g(t) double t; { point p; p.x_coord = ... ; p.y_coord = ... ; return (p); } and so on. And then add one line at the end of main() for each new curve, following the pattern you see there. */ main() { point p, f(); char *getlogin(); char hostname[30]; struct tm *localtime(), *tp; struct timeval tv; struct timezone tz; char months[12][20] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; char long_days[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; window(); position_label(); begin_large_text(); printf("One period of the function sin\\(2x\\)."); /* Unbalanced parentheses need escaping; here use \\. Don't use \n. */ end_large_text(); position_login_signature(); gethostname(hostname,30); begin_small_text(); printf("Login name: %s@%s",getlogin(),hostname); end_small_text(); gettimeofday(&tv, &tz); tp = localtime(&tv.tv_sec); position_date_stamp(); begin_small_text(); printf("%s, %s %d, 19%d %d:%d\n", long_days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, tp->tm_year, tp->tm_hour, tp->tm_min); end_small_text(); draw_frame(); label_x_axis(LEFT,RIGHT); label_y_axis(TOP,BOTTOM); draw_axes(LEFT,RIGHT,BOTTOM,TOP); /* This ends the set-up work; now here comes the actual curve. It is best to put the actual curve last, because then the output of the program is easier to examine. */ print_path(START_T,STOP_T,STEP,WIDTH,LEFT,RIGHT,BOTTOM,TOP,f); }