Relax assertion.
[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.5  2004/09/14 20:56:39  bernie
30  *#* Make more generic and adapt to new gfx functions.
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 #ifndef CONFIG_CHART_ARROWS
45 #define CONFIG_CHART_ARROWS 0
46 #endif
47
48
49 void chart_init(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax)
50 {
51         gfx_ClearBitmap(bm);
52
53         gfx_SetClipRect(bm, xmin + CHART_BORDERLEFT, ymin + CHART_BORDERTOP,
54                 xmax - CHART_BORDERRIGHT, ymax - CHART_BORDERBOTTOM);
55
56         chart_drawAxis(bm);
57 }
58
59
60 void chart_setScale(Bitmap *bm, chart_x_t xmin, chart_y_t ymin, chart_x_t xmax, chart_y_t ymax)
61 {
62         gfx_SetViewRect(bm, xmin, ymin, xmax, ymax);
63 }
64
65
66 /*!
67  * Draw the chart axes
68  */
69 void chart_drawAxis(Bitmap *bm)
70 {
71 #if CONFIG_CHART_ARROWS
72
73         /* Draw axis */
74         gfx_MoveTo(bm, bm->cr.xmin, bm->cr.ymin + 4);
75         gfx_LineTo(bm, bm->cr.xmin, bm->cr.ymax - 1);
76         gfx_LineTo(bm, bm->cr.xmax - 4 - 1, bm->cr.ymax - 1);
77
78         /* Draw up arrow */
79         gfx_MoveTo(bm, bm->cr.xmin - 2, bm->cr.ymin + 3);
80         gfx_LineTo(bm, bm->cr.xmin + 2, bm->cr.ymin + 3);
81         gfx_LineTo(bm, bm->cr.xmin, bm->cr.ymin);
82         gfx_LineTo(bm, bm->cr.xmin - 2, bm->cr.ymin + 3);
83
84         /* Draw right arrow */
85         gfx_MoveTo(bm, bm->cr.xmax - 4, bm->cr.ymax - 3);
86         gfx_LineTo(bm, bm->cr.xmax - 4, bm->cr.ymax + 1);
87         gfx_LineTo(bm, bm->cr.xmax - 1, bm->cr.ymax - 1);
88         gfx_LineTo(bm, bm->cr.xmax - 4, bm->cr.ymax - 3);
89
90 #else /* CONFIG_CHART_ARROWS */
91
92         /* Draw a box around the chart */
93         gfx_RectDraw(bm, bm->cr.xmin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
94
95 #endif /* CONFIG_CHART_ARROWS */
96
97         //CHECK_WALL(wall_before_raster, WALL_SIZE);
98         //CHECK_WALL(wall_after_raster, WALL_SIZE);
99 }
100
101
102 /*!
103  * Draw a set of \a curve_cnt connected segments, whose Y coordinates
104  * are identified by the \a curve_y array and X-coordinates are
105  * are evenly spaced by one virtual unit.
106  */
107 void chart_drawCurve(Bitmap *bm, const chart_y_t *curve_y, int curve_cnt)
108 {
109         int i;
110
111         gfx_MoveTo(bm, gfx_TransformX(bm, 0), gfx_TransformY(bm, curve_y[0]));
112
113         for (i = 1; i < curve_cnt; i++)
114                 gfx_LineTo(bm, gfx_TransformX(bm, i), gfx_TransformY(bm, curve_y[i]));
115
116         //CHECK_WALL(wall_before_raster, WALL_SIZE);
117         //CHECK_WALL(wall_after_raster, WALL_SIZE);
118 }
119
120
121 /*!
122  * Disegna dei dot in corrispondenza delle coppie (dotsx[i];dotsy[i])
123  * Se dotsx e' NULL, i punti vengono disegnati ad intervalli regolari.
124  */
125 void chart_drawDots(Bitmap *bm, const chart_x_t *dots_x, const chart_y_t *dots_y, int cnt)
126 {
127         int i;
128         coord_t x, y;
129
130         for (i = 0; i < cnt; i++)
131         {
132                 if (dots_x)
133                         x = gfx_TransformX(bm, dots_x[i]);
134                 else
135                         x = gfx_TransformX(bm, i);
136
137                 y = gfx_TransformY(bm, dots_y[i]);
138
139                 /* Draw tick over the curve */
140                 gfx_RectFill(bm,
141                         x - TICKS_WIDTH / 2, y - TICKS_HEIGHT / 2,
142                         x + (TICKS_WIDTH + 1) / 2, y + (TICKS_HEIGHT + 1) / 2);
143
144                 /* Draw vertical line from the curve to the X-axis */
145                 //gfx_DrawLine(bm, x, y, x, bm->cr.ymax - 1);
146         }
147
148         //CHECK_WALL(wall_before_raster, WALL_SIZE);
149         //CHECK_WALL(wall_after_raster, WALL_SIZE);
150 }
151