ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/graphics/qplot.c
Revision: 1.3
Committed: Wed Jan 28 23:48:17 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -1 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2 pcg 1.3 * $Id: qplot.c,v 1.2 2003/11/24 17:31:28 pcg Exp $
3 pcg 1.1 */
4    
5     #include <stdio.h>
6     #include <stdlib.h>
7     #include <math.h>
8     #include <signal.h>
9     #include "grxlib.h"
10    
11     #define Real float
12    
13     #ifndef GRX_SCALE
14     # define GRX_SCALE 10000
15     #endif
16    
17     #define DEFAULT_DATA_FILE "data"
18    
19     static void
20     axis_round(Real * min, Real * max, Real * grid_spacing)
21     {
22     int logspace;
23    
24     logspace = (int)(log10((*max - *min) / 10.0) + 0.5);
25     *grid_spacing = pow(10, (double)logspace);
26     *min = (Real) ((int)(*min / (*grid_spacing))) * (*grid_spacing);
27     *max = (Real) ((int)(*max / (*grid_spacing)) + 1) * (*grid_spacing);
28     }
29    
30     static int
31     nice_end(int junk)
32     {
33     CloseGraphics();
34     putchar('\n');
35     exit(EXIT_SUCCESS);
36     return 0;
37     }
38    
39     int
40     main(int argc, char **argv)
41     {
42     char *file = NULL;
43     int Do_Start = 1, tmp;
44     int m, p, i, j, n, nchars, theight, twidth, xclick, yclick;
45     int downx = 1000, downy = 1000, upx, upy;
46     long id, winclick;
47     Real xmax, xmin, ymax, ymin, xdiff, ydiff, xgrid_spacing,
48     ygrid_spacing;
49     Real *x, *y, *nls;
50     LineFunction linetype = StartLine;
51     char axis[100], line[256];
52     FILE *fd;
53    
54     x = (Real *)malloc(1000000 * sizeof(Real));
55     y = (Real *)malloc(1000000 * sizeof(Real));
56     nls = (Real *)malloc(1000 * sizeof(Real));
57     if (x == NULL || y == NULL || nls == NULL) {
58     fprintf(stderr, "Can't allocate initial memory\n");
59     exit(1);
60     }
61    
62     ymax = xmax = -HUGE_VAL;
63     ymin = xmin = HUGE_VAL;
64    
65     for (i = 1; i < argc; i++) {
66     if (*argv[i] == '-') {
67     if (!strcmp(argv[i], "-nl"))
68     linetype = StartPoint;
69     else if (argv[i][1] == '\0') /* use stdin */
70     file = argv[i];
71     else {
72    
73     fprintf(stderr, "Usage:\n\t %s [options] [file]\n\n", argv[0]);
74     fprintf(stderr,
75     "where options include:\n"
76     " -pt plot with points instead of lines\n\n");
77    
78     fprintf(stderr,
79     "file name `-' specifies stdin\n"
80     "if no file name is specified, "
81     "the default is \"%s\"\n\n", DEFAULT_DATA_FILE);
82    
83     return EXIT_FAILURE;
84     }
85     } else
86     file = argv[i];
87     }
88    
89     if (file && !strcmp(file, "-")) {
90     fd = stdin;
91     file = "stdin";
92     } else {
93     if (file == NULL)
94     file = DEFAULT_DATA_FILE;
95    
96     if ((fd = fopen(file, "r")) == NULL) {
97     fprintf(stderr, "%s: can't open file \"%s\"\n", argv[0], file);
98     return EXIT_FAILURE;
99     }
100     }
101     m = 0;
102     p = 0;
103     while (fgets(line, sizeof(line), fd) != NULL) {
104     if (sscanf(line, "%f %f", &x[m], &y[m]) == 2) {
105     if (x[m] > xmax)
106     xmax = x[m];
107     else if (x[m] < xmin)
108     xmin = x[m];
109     if (y[m] > ymax)
110     ymax = y[m];
111     else if (y[m] < ymin)
112     ymin = y[m];
113     m++;
114     } else {
115     nls[p] = m;
116     p++;
117     }
118     }
119     nls[p++] = m;
120    
121     if (m == 0)
122     return;
123    
124     signal(SIGTERM, nice_end);
125     signal(SIGSTOP, nice_end);
126     signal(SIGTSTP, nice_end);
127     signal(SIGINT, nice_end);
128     signal(SIGQUIT, nice_end);
129     if (!InitializeGraphics(1))
130     return EXIT_FAILURE;
131    
132     n = 1;
133     do {
134     axis_round(&xmin, &xmax, &xgrid_spacing);
135     axis_round(&ymin, &ymax, &ygrid_spacing);
136    
137     id = CreateWin(0, 0, GRX_SCALE, GRX_SCALE);
138     if (id == 0) {
139     fprintf(stderr, "Help id = 0\n");
140     return EXIT_FAILURE;
141     }
142     /* Fill the window in black for real eye-catching graphics! */
143     ForeColor(0);
144     StartFill(id);
145     FillArea(0, 0, GRX_SCALE, GRX_SCALE);
146     Done();
147    
148     /* draw outline box in white */
149     ForeColor(7);
150    
151     /* Draw outline box */
152     StartLine(id);
153     Extend(1000, 1000);
154     Extend(1000, 9000);
155     Extend(9000, 9000);
156     Extend(9000, 1000);
157     Extend(1000, 1000);
158     Done();
159    
160     /* Draw the data - either lines or dots */
161     xdiff = 8000 / (xmax - xmin);
162     ydiff = 8000 / (ymax - ymin);
163    
164     for (i = j = 0; j < p; j++) {
165     int n = 0;
166    
167     ForeColor(j % 6 + 1);
168     while (((x[i] < xmin) || (x[i] > xmax) ||
169     (y[i] < ymin) || (y[i] > ymax)) && (i < nls[j]))
170     i++;
171    
172     while (i < nls[j]) {
173     if (n == 0)
174     linetype(id);
175     Extend(1000 + (x[i] - xmin) * xdiff,
176     9000 - (y[i] - ymin) * ydiff);
177     n++;
178     if (n > 450) {
179     Done();
180     n = 0;
181     continue;
182     }
183     i++;
184     while ((i < nls[j]) &&
185     ((x[i] < xmin) || (x[i] > xmax) ||
186     (y[i] < ymin) || (y[i] > ymax)))
187     i++;
188     }
189     if (n > 0)
190     Done();
191     }
192    
193     /* Do axis labels in black */
194     ForeColor(7);
195     QueryWin(id, &twidth, &theight);
196     PlaceText(id, GRX_SCALE / 2, 0, HCENTER_TEXT | TOP_TEXT, file);
197     PlaceText(id, GRX_SCALE / 2, GRX_SCALE, HCENTER_TEXT | BOTTOM_TEXT,
198     "X");
199     PlaceText(id, 0, GRX_SCALE / 2, LEFT_TEXT | VCENTER_TEXT, "Y");
200     sprintf(axis, "%f", ymax);
201     nchars = 1000 / twidth;
202     axis[nchars] = 0;
203     PlaceText(id, GRX_SCALE / 10, GRX_SCALE / 10,
204     RIGHT_TEXT | TOP_TEXT, axis);
205     sprintf(axis, "%f", ymin);
206     axis[nchars] = 0;
207     PlaceText(id, GRX_SCALE / 10, 9 * GRX_SCALE / 10,
208     RIGHT_TEXT | BOTTOM_TEXT, axis);
209     sprintf(axis, "%f", xmax);
210     PlaceText(id, 9 * GRX_SCALE / 10, 9 * GRX_SCALE / 10,
211     HCENTER_TEXT | TOP_TEXT, axis);
212     sprintf(axis, "%f", xmin);
213     PlaceText(id, GRX_SCALE / 10, 9 * GRX_SCALE / 10,
214     HCENTER_TEXT | TOP_TEXT, axis);
215     fflush(stdout);
216    
217     do {
218     n = WaitForCarriageReturn(&winclick, &xclick, &yclick);
219     switch (n) {
220     case 1:
221     downx = xclick;
222     downy = yclick;
223     break;
224     case 2:
225     upx = xclick;
226     upy = yclick;
227     if (upx < downx) {
228     tmp = downx;
229     downx = upx;
230     upx = tmp;
231     }
232     if (upy < downy) {
233     tmp = downy;
234     downy = upy;
235     upy = tmp;
236     }
237     xmin = (xmax - xmin) * (downx - 1000) / (8000) + xmin;
238     xmax = (xmax - xmin) * (upx - 1000) / (8000) + xmin;
239     ymax = ymax - (ymax - ymin) * (downy - 1000) / (8000);
240     ymin = ymax - (ymax - ymin) * (upy - 1000) / (8000);
241     break;
242     }
243     } while (n && (n != 2));
244     } while (n);
245     nice_end(EXIT_SUCCESS);
246     return EXIT_SUCCESS;
247     }
248    
249     /*----------------------- end-of-file (C source) -----------------------*/