~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/draw/plot/plot_graph.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)
  2 **/
  3 #include <stdio.h>
  4 #include <math.h>
  5 #include <string.h>
  6 #include <tina/sys.h>
  7 #include <tina/math.h>
  8 #include <tina/tv.h>
  9 #include <tina/tvfuncs.h>
 10 #include <tina/vision.h>
 11 #include <tina/draw.h>
 12 #include <tina/drawfuncs.h>
 13 
 14 Vec2    vec2(double , double);
 15 Ipos    ipos(int, int);
 16 
 17 
 18 static int ylegend;
 19 
 20 static char xfmt[16] = "%5.2f";
 21 void pl_xfmt_set(char *newxfmt)
 22 {
 23     strcpy(xfmt, newxfmt);
 24 }
 25 
 26 static char yfmt[16] = "%5.2f";
 27 void pl_yfmt_set(char *newyfmt)
 28 {
 29     strcpy(yfmt, newyfmt);
 30 }
 31 
 32 static Vec2 ll, ur;
 33 Vec2 pl_ll_get()
 34 {
 35     return(ll);
 36 }
 37 
 38 Vec2 pl_ur_get()
 39 {
 40     return(ur);
 41 }
 42 
 43 
 44 void    pl_graphtv(Tv * tv, double xmin, double ymin, double xmax, double ymax)
 45 {
 46     double  cx = (xmax + xmin) / 2, wx = (xmax - xmin) / 2;
 47     double  cy = (ymax + ymin) / 2, wy = (ymax - ymin) / 2;
 48 
 49     if (tv == NULL)
 50         return;
 51     wx = (wx == 0)? 1: wx;
 52     wy = (wy == 0)? 1: wy;
 53     tv_camera2_rect(tv, vec2(cx - 1.3 * wx, cy - 1.3 * wy),
 54                     vec2(cx + 1.3 * wx, cy + 1.3 * wy));
 55 }
 56 
 57 void    tv_xtick(Tv * tv, Ipos p, int n)
 58 {
 59     Ipos    q = {Ipos_id};
 60 
 61     if (tv == NULL)
 62         return;
 63     q.x = p.x;
 64     q.y = p.y + n;
 65     tv_line(tv, p, q);
 66 }
 67 
 68 void    tv_xtick2(Tv * tv, Vec2 p, int n)
 69 {
 70     if (tv == NULL)
 71         return;
 72     tv_xtick(tv, tv_proj2(tv, p), n);
 73 }
 74 
 75 void    tv_xtick3(Tv * tv, Vec3 p, int n)
 76 {
 77     if (tv == NULL)
 78         return;
 79     tv_xtick(tv, tv_proj3(tv, p), n);
 80 }
 81 
 82 void    tv_ytick(Tv * tv, Ipos p, int n)
 83 {
 84     Ipos    q = {Ipos_id};
 85 
 86     if (tv == NULL)
 87         return;
 88     q.x = p.x - n;
 89     q.y = p.y;
 90     tv_line(tv, p, q);
 91 }
 92 
 93 void    tv_ytick2(Tv * tv, Vec2 p, int n)
 94 {
 95     if (tv == NULL)
 96         return;
 97     tv_ytick(tv, tv_proj2(tv, p), n);
 98 }
 99 
100 void    tv_ytick3(Tv * tv, Vec3 p, int n)
101 {
102     if (tv == NULL)
103         return;
104     tv_ytick(tv, tv_proj3(tv, p), n);
105 }
106 
107 void    tv_cctext(Tv * tv, char *string, Ipos p)
108 {
109     int     xmin, xmax, ymin, ymax;
110     int     offx, offy;
111 
112     if (tv == NULL)
113         return;
114 
115     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
116     offx = -(xmax + xmin) / 2;
117     offy = -(ymax + ymin) / 2;
118     tv_offset_text(tv, string, p, offx, offy);
119 }
120 
121 void    tv_uctext(Tv * tv, char *string, Ipos p)
122 {
123     int     xmin, xmax, ymin, ymax;
124     int     offx, offy;
125 
126     if (tv == NULL)
127         return;
128 
129     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
130     offx = -(xmax + xmin) / 2;
131     offy = -ymin + 5;
132     tv_offset_text(tv, string, p, offx, offy);
133 }
134 
135 void    tv_uctext2(Tv * tv, char *string, Vec2 p)
136 {
137     if (tv == NULL)
138         return;
139     tv_uctext(tv, string, tv_proj2(tv, p));
140 }
141 
142 void    tv_uctext3(Tv * tv, char *string, Vec3 p)
143 {
144     if (tv == NULL)
145         return;
146     tv_uctext(tv, string, tv_proj3(tv, p));
147 }
148 
149 void    tv_lctext(Tv * tv, char *string, Ipos p)
150 {
151     int     xmin, xmax, ymin, ymax;
152     int     offx, offy;
153 
154     if (tv == NULL)
155         return;
156 
157     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
158     offx = -(xmax + xmin) / 2;
159     offy = -5;
160     tv_offset_text(tv, string, p, offx, offy);
161 }
162 
163 void    tv_lctext2(Tv * tv, char *string, Vec2 p)
164 {
165     if (tv == NULL)
166         return;
167     tv_lctext(tv, string, tv_proj2(tv, p));
168 }
169 
170 void    tv_lctext3(Tv * tv, char *string, Vec3 p)
171 {
172     if (tv == NULL)
173         return;
174     tv_lctext(tv, string, tv_proj3(tv, p));
175 }
176 
177 void    tv_crtext(Tv * tv, char *string, Ipos p)
178 {
179     int     xmin, xmax, ymin, ymax;
180     int     offx, offy;
181 
182     if (tv == NULL)
183         return;
184 
185     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
186     offx = -xmax - 5;
187     offy = -(ymax + ymin) / 2;
188     tv_offset_text(tv, string, p, offx, offy);
189 }
190 
191 void    tv_crtext2(Tv * tv, char *string, Vec2 p)
192 {
193     if (tv == NULL)
194         return;
195     tv_crtext(tv, string, tv_proj2(tv, p));
196 }
197 
198 void    tv_crtext3(Tv * tv, char *string, Vec3 p)
199 {
200     if (tv == NULL)
201         return;
202     tv_crtext(tv, string, tv_proj3(tv, p));
203 }
204 
205 void    tv_cltext(Tv * tv, char *string, Ipos p)
206 {
207     int     xmin, xmax, ymin, ymax;
208     int     offx, offy;
209 
210     if (tv == NULL)
211         return;
212 
213     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
214     offx = 5;
215     offy = -(ymax + ymin) / 2;
216     tv_offset_text(tv, string, p, offx, offy);
217 }
218 
219 void    tv_cltext2(Tv * tv, char *string, Vec2 p)
220 {
221     if (tv == NULL)
222         return;
223     tv_cltext(tv, string, tv_proj2(tv, p));
224 }
225 
226 void    tv_cltext3(Tv * tv, char *string, Vec3 p)
227 {
228     if (tv == NULL)
229         return;
230     tv_cltext(tv, string, tv_proj3(tv, p));
231 }
232 
233 /* ARGSUSED quieten lint */
234 void    pl_xaxis_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
235 {
236     if (tv == NULL)
237         return;
238     tv_line2(tv, vec2(xmin, ypos), vec2(xmax, ypos));
239 }
240 
241 /* ARGSUSED quieten lint */
242 void    pl_xtics_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
243 {
244     double  x;
245 
246     if (tv == NULL)
247         return;
248     for (x = xmin; x <= xmax + xinc / 2; x += xinc)
249         tv_xtick2(tv, vec2(x, ypos), 3);
250 }
251 
252 void    pl_xtext_plot(Tv * tv, double xmin, double xmax, double xinc, double xpos, double ypos)
253 {
254     double  x;
255 
256     if (tv == NULL)
257         return;
258     for (x = xmin; x <= xmax + xinc / 2; x += xinc)
259     {
260         if (fabs(x - xpos) >= xinc / 2)
261         {
262             char    string[32];
263 
264             (void) sprintf(string, xfmt, x);
265             tv_uctext2(tv, string, vec2(x, ypos));
266         }
267     }
268 }
269 
270 /* ARGSUSED quieten lint */
271 void    pl_yaxis_plot(Tv * tv, double ymin, double ymax, double yinc, double xpos, double ypos)
272 {
273     if (tv == NULL)
274         return;
275     tv_line2(tv, vec2(xpos, ymin), vec2(xpos, ymax));
276 }
277 
278 /* ARGSUSED quieten lint */
279 void    pl_ytics_plot(Tv * tv, double ymin, double ymax, double yinc, double xpos, double ypos)
280 {
281     double  y;
282 
283     if (tv == NULL)
284         return;
285     for (y = ymin; y <= ymax + yinc / 2; y += yinc)
286         tv_ytick2(tv, vec2(xpos, y), 3);
287 }
288 
289 void    pl_ytext_plot(Tv * tv, double ymin, double ymax, double yinc, double ypos, double xpos)
290 {
291     double  y;
292 
293     if (tv == NULL)
294         return;
295     for (y = ymin; y <= ymax + yinc / 2; y += yinc)
296     {
297         char    string[32];
298 
299         if (fabs(y - ypos) >= yinc / 2)
300         {
301             (void) sprintf(string, yfmt, y);
302             tv_crtext2(tv, string, vec2(xpos, y));
303         }
304     }
305 }
306 
307 void    pl_axes_plot(Tv * tv, Pl_axes axes)
308 {
309     tv_set_color(tv, axes.color);
310     tv_uctext(tv, axes.title, ipos(tv->width / 2, 10));
311 
312     if (axes.xaxis)
313         pl_xaxis_plot(tv,
314                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos);
315 
316     if (axes.xtics)
317         pl_xtics_plot(tv,
318                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos);
319 
320     if (axes.xtext)
321         pl_xtext_plot(tv,
322                axes.xmin, axes.xmax, axes.xinc, axes.xpos, axes.ypos);
323 
324     if (axes.yaxis)
325         pl_yaxis_plot(tv,
326                axes.ymin, axes.ymax, axes.yinc, axes.xpos, axes.ypos);
327 
328     if (axes.ytics)
329         pl_ytics_plot(tv,
330                axes.ymin, axes.ymax, axes.yinc, axes.xpos, axes.ypos);
331 
332     if (axes.ytext)
333         pl_ytext_plot(tv,
334                axes.ymin, axes.ymax, axes.yinc, axes.ypos, axes.xpos);
335     
336     ll = vec2(axes.xmin, axes.ymin);
337     ur = vec2(axes.xmax, axes.ymax);
338 }
339 
340 static void legend(Tv *tv, int color, char *legend)
341 {
342     int xmin, xmax, ymin, ymax, w, h;
343     tv_get_text_extent(tv, legend, &xmin, &xmax, &ymin, &ymax);
344     w = xmin+xmax;
345     h = ymin+ymax;
346     tv_set_color(tv, black);
347     tv_text(tv, legend, ipos(tv->width-35-w, ylegend-h/2));
348     tv_set_color(tv, color);
349     tv_linexy(tv, tv->width-30, ylegend, tv->width-10, ylegend);
350     h = -ymin+ymax;
351     ylegend += h+5;
352 }
353 
354 void    pl_graph_plot(Tv * tv, Pl_graph * graph, Pl_axes axes)
355 {
356     int     i, n = graph->n;
357     float  *x, *y;
358     float xmin = (float)axes.xmin;
359     float xmax = (float)axes.xmax;
360     float ymin = (float)axes.ymin;
361     float ymax = (float)axes.ymax;
362     void *mask;
363 
364     if (tv == NULL || graph == NULL)
365         return;
366 
367     x = graph->x;
368     y = graph->y;
369     if(strcmp(graph->legend, "") != 0)
370         legend(tv, graph->color, graph->legend);
371     tv_set_color(tv, graph->color);
372     mask = tv_clip_mask2_get(tv, vec2(xmin, ymin), vec2(xmax, ymax));
373     tv_mask_use(tv, mask);
374     switch(graph->style)
375     {
376         case PL_LINE:
377             for (i = 1; i < n; i++)
378                 tv_line2(tv, vec2(x[i - 1], y[i - 1]), vec2(x[i], y[i]));
379             break;
380         case PL_CROSS:
381             for (i = 0; i < n; i++)
382                 tv_cross2(tv, vec2(x[i], y[i]), 5);
383             break;
384         case PL_DOT:
385             for (i = 0; i < n; i++)
386                 tv_dot2(tv, vec2(x[i], y[i]));
387             break;
388         case PL_POINT:
389             for (i = 0; i < n; i++)
390                 tv_point2(tv, vec2(x[i], y[i]));
391             break;
392     }
393     tv_mask_free(tv, mask);
394 }
395 
396 void    pl_scatter_plot(Tv * tv, Pl_graph * graph)
397 {
398     int     i, n = graph->n;
399     float  *x, *y;
400 
401     if (tv == NULL || graph == NULL)
402         return;
403 
404     x = graph->x;
405     y = graph->y;
406     tv_set_color(tv, graph->color);
407     for (i = 0; i < n; i++)
408         tv_cross2(tv, vec2(x[i], y[i]), 5);
409 }
410 
411 
412 void    pl_list_plot(Tv * tv, List * list, Pl_axes axes)
413 {
414     List   *ptr;
415 
416     ylegend = 10;
417 
418     if (tv == NULL || list == NULL)
419         return;
420 
421     for (ptr = list; ptr != NULL; ptr = ptr->next)
422     {
423         switch (ptr->type)
424         {
425         case PL_GRAPH_TYPE:
426             {
427                 Pl_graph *graph = (Pl_graph *) ptr->to;
428 
429                 pl_graph_plot(tv, graph, axes);
430                 break;
431             }
432         case PL_CTR_TYPE:
433             {
434                 Pl_ctr *ctr = (Pl_ctr *) ptr->to;
435 
436                 pl_ctr_plot(tv, ctr);
437                 break;
438             }
439         case PL_SCATTER_TYPE:
440             {
441                 Pl_graph *graph = (Pl_graph *) ptr->to;
442 
443                 pl_scatter_plot(tv, graph);
444                 break;
445             }
446         }
447     }
448 }
449 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.