draw: simplify draw_graph()

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-12 15:21:25 +02:00
parent 30a19041c9
commit 659f9bd048
1 changed files with 23 additions and 29 deletions

View File

@ -506,59 +506,53 @@ draw_graph_setup(draw_context_t *ctx)
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER); cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
} }
/** Draw a graph /** Draw a graph.
* \param ctx Draw context * \param ctx Draw context.
* \param x x-offset of widget * \param x x offset of widget.
* \param y y-offset of widget * \param y y offset of widget.
* \param w width in pixels * \param w Width in pixels.
* \param from array of starting-point offsets to draw a graph-lines * \param from Array of starting-point offsets to draw a graph lines.
* \param to array of end-point offsets to draw a graph-lines * \param to Array of end-point offsets to draw a graph lines.
* \param cur_index current position in data-array (cycles around) * \param cur_index Current position in data-array (cycles around).
* \param grow put new values to the left or to the right * \param grow Put new values to the left or to the right.
* \param pcolor color at the left * \param pcolor Color at the left.
* \param pcolor_center color in the center * \param pcolor_center Color in the center.
* \param pcolor_end color at the right * \param pcolor_end Color at the right.
*/ */
void void
draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index, draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
position_t grow, area_t patt_rect, position_t grow, area_t patt_rect,
xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end) xcolor_t *pcolor, xcolor_t *pcolor_center, xcolor_t *pcolor_end)
{ {
int i, y, w; int i = -1;
float x; float x = rect.x + 0.5; /* middle of a pixel */
cairo_pattern_t *pat; cairo_pattern_t *pat;
pat = draw_setup_cairo_color_source(ctx, patt_rect, pat = draw_setup_cairo_color_source(ctx, patt_rect,
pcolor, pcolor_center, pcolor_end); pcolor, pcolor_center, pcolor_end);
/* middle of a pixel */
x = rect.x + 0.5;
y = rect.y;
w = rect.width;
i = -1;
if(grow == Right) /* draw from right to left */ if(grow == Right) /* draw from right to left */
{ {
x += w - 1; x += rect.width - 1;
while(++i < w) while(++i < rect.width)
{ {
cairo_move_to(ctx->cr, x, y - from[cur_index]); cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
cairo_line_to(ctx->cr, x, y - to[cur_index]); cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
x -= 1.0; x -= 1.0;
if (--cur_index < 0) if (--cur_index < 0)
cur_index = w - 1; cur_index = rect.width - 1;
} }
} }
else /* draw from left to right */ else /* draw from left to right */
while(++i < w) while(++i < rect.width)
{ {
cairo_move_to(ctx->cr, x, y - from[cur_index]); cairo_move_to(ctx->cr, x, rect.y - from[cur_index]);
cairo_line_to(ctx->cr, x, y - to[cur_index]); cairo_line_to(ctx->cr, x, rect.y - to[cur_index]);
x += 1.0; x += 1.0;
if (--cur_index < 0) if (--cur_index < 0)
cur_index = w - 1; cur_index = rect.width - 1;
} }
cairo_stroke(ctx->cr); cairo_stroke(ctx->cr);