Clarify side-effects in documentation.
[bertos.git] / mware / charts.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2000, 2001, 2003 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief Simple charts on top of mware/gfx routines (implementation).
10  *
11  * Sample usage:
12  *
13  * \code
14  *      bm = chart_init(0, ymax, N_POINTS_CURVE, ymin);
15  *
16  *      chart_drawCurve(bm, curve_y, curve_points + 1);
17  *      gfx_setViewRect(bm, xmin, ymax, xmax, ymin);
18  *      chart_drawDots(bm, samples_x, samples_y, samples_cnt);
19  *
20  *      print_bitmap(bm);
21  * \endcode
22  *
23  * \version $Id$
24  * \author Bernardo Innocenti <bernie@develer.com>
25  */
26
27 /*#*
28  *#* $Log$
29  *#* Revision 1.4  2004/08/25 14:12:09  rasky
30  *#* Aggiornato il comment block dei log RCS
31  *#*
32  *#* Revision 1.3  2004/08/11 19:39:12  bernie
33  *#* Use chart_x_t and chart_y_t for the input dataset.
34  *#*
35  *#* Revision 1.1  2004/08/04 03:16:30  bernie
36  *#* Import simple chart drawing code.
37  *#*
38  *#*/
39
40 #include "charts.h"
41 #include <mware/gfx.h>
42
43
44 void chart_init(Bitmap *bm, vcoord_t xmin, vcoord_t ymin, vcoord_t xmax, vcoord_t ymax)
45 {
46         gfx_ClearBitmap(bm);
47         chart_drawAxis(bm);
48
49         gfx_SetClipRect(bm, CHART_BORDERLEFT, CHART_BORDERTOP,
50                 bm->width - CHART_BORDERRIGHT - 1, bm->height - CHART_BORDERBOTTOM - 1);
51
52         gfx_SetViewRect(bm, xmin, ymin, xmax, ymax);
53
54         //CHECK_WALL(wall_before_raster, WALL_SIZE);
55         //CHECK_WALL(wall_after_raster, WALL_SIZE);
56 }
57
58
59 /*!
60  * Draw the chart axes
61  */
62 void chart_drawAxis(Bitmap *bm)
63 {
64         /* Draw axis */
65         gfx_MoveTo(bm, CHART_BORDERLEFT, 4);
66         gfx_LineTo(bm, CHART_BORDERLEFT, CHART_BORDERTOP + CHART_HEIGHT - 1);
67         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 5, CHART_BORDERTOP + CHART_HEIGHT - 1);
68
69         /* Draw up arrow */
70         gfx_MoveTo(bm, CHART_BORDERLEFT - 2, 3);
71         gfx_LineTo(bm, CHART_BORDERLEFT + 2, 3);
72         gfx_LineTo(bm, CHART_BORDERLEFT, 0);
73         gfx_LineTo(bm, CHART_BORDERLEFT - 2, 3);
74
75         /* Draw right arrow */
76         gfx_MoveTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT - 3);
77         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT + 1);
78         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 1, CHART_BORDERTOP + CHART_HEIGHT - 1);
79         gfx_LineTo(bm, CHART_BORDERLEFT + CHART_WIDTH - 4, CHART_BORDERTOP + CHART_HEIGHT - 3);
80
81         //CHECK_WALL(wall_before_raster, WALL_SIZE);
82         //CHECK_WALL(wall_after_raster, WALL_SIZE);
83 }
84
85
86 /*!
87  * Draw a set of \a curve_cnt connected segments, whose Y coordinates
88  * are identified by the \a curve_y array and X-coordinates are
89  * are evenly spaced by one virtual unit.
90  */
91 void chart_drawCurve(Bitmap *bm, const chart_y_t *curve_y, int curve_cnt)
92 {
93         int i;
94
95         gfx_MoveTo(bm, gfx_TransformX(bm, 0), gfx_TransformY(bm, curve_y[0]));
96
97         for (i = 1; i < curve_cnt; i++)
98                 gfx_LineTo(bm, gfx_TransformX(bm, i), gfx_TransformY(bm, curve_y[i]));
99
100         //CHECK_WALL(wall_before_raster, WALL_SIZE);
101         //CHECK_WALL(wall_after_raster, WALL_SIZE);
102 }
103
104
105 /*!
106  * Disegna dei dot in corrispondenza delle coppie (dotsx[i];dotsy[i])
107  * Se dotsx e' NULL, i punti vengono disegnati ad intervalli regolari.
108  */
109 void chart_drawDots(Bitmap *bm, const chart_x_t *dots_x, const chart_y_t *dots_y, int cnt)
110 {
111         int i;
112         coord_t x, y;
113
114         for (i = 0; i < cnt; i++)
115         {
116                 if (dots_x)
117                         x = gfx_TransformX(bm, dots_x[i]);
118                 else
119                         x = gfx_TransformX(bm, i);
120
121                 y = gfx_TransformY(bm, dots_y[i]);
122
123                 gfx_DrawRect(bm, x - 1, y - 1, x + 1, y + 1);
124
125                 /* Disegna ticks sull'asse X */
126                 gfx_DrawLine(bm, x, bm->height - 1, x, CHART_HEIGHT - 1);
127         }
128
129         //CHECK_WALL(wall_before_raster, WALL_SIZE);
130         //CHECK_WALL(wall_after_raster, WALL_SIZE);
131 }
132