1 /* $Id: tic.c,v 1.5 2015/07/06 08:05:44 kristaps Exp $ */
2 /*
3 * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include "compat.h"
18
19 #include <assert.h>
20 #include <cairo.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "kplot.h"
25 #include "extern.h"
26
27 void
kplotctx_tic_init(struct kplotctx * ctx)28 kplotctx_tic_init(struct kplotctx *ctx)
29 {
30 double offs, v;
31 size_t i;
32
33 kplotctx_ticln_init(ctx, &ctx->cfg.ticline);
34
35 for (i = 0; i < ctx->cfg.xtics; i++) {
36 offs = 1 == ctx->cfg.xtics ? 0.5 :
37 i / (double)(ctx->cfg.xtics - 1);
38 v = kplotctx_line_fix(ctx,
39 ctx->cfg.ticline.sz,
40 ctx->offs.x + offs * ctx->dims.x);
41 if (TIC_BOTTOM_IN & ctx->cfg.tic) {
42 cairo_move_to(ctx->cr, v,
43 ctx->offs.y + ctx->dims.y);
44 cairo_rel_line_to(ctx->cr,
45 0.0, -ctx->cfg.ticline.len);
46 }
47 if (TIC_BOTTOM_OUT & ctx->cfg.tic) {
48 cairo_move_to(ctx->cr, v,
49 ctx->offs.y + ctx->dims.y);
50 cairo_rel_line_to(ctx->cr,
51 0.0, ctx->cfg.ticline.len);
52 }
53 if (TIC_TOP_IN & ctx->cfg.tic) {
54 cairo_move_to(ctx->cr, v, ctx->offs.y);
55 cairo_rel_line_to(ctx->cr,
56 0.0, ctx->cfg.ticline.len);
57 }
58 if (TIC_TOP_OUT & ctx->cfg.tic) {
59 cairo_move_to(ctx->cr, v, ctx->offs.y);
60 cairo_rel_line_to(ctx->cr,
61 0.0, -ctx->cfg.ticline.len);
62 }
63 }
64
65 for (i = 0; i < ctx->cfg.ytics; i++) {
66 offs = 1 == ctx->cfg.ytics ? 0.5 :
67 i / (double)(ctx->cfg.ytics - 1);
68 v = kplotctx_line_fix(ctx,
69 ctx->cfg.ticline.sz,
70 ctx->offs.y + offs * ctx->dims.y);
71 if (TIC_LEFT_IN & ctx->cfg.tic) {
72 cairo_move_to(ctx->cr, ctx->offs.x, v);
73 cairo_rel_line_to(ctx->cr,
74 ctx->cfg.ticline.len, 0.0);
75 }
76 if (TIC_LEFT_OUT & ctx->cfg.tic) {
77 cairo_move_to(ctx->cr, ctx->offs.x, v);
78 cairo_rel_line_to(ctx->cr,
79 -ctx->cfg.ticline.len, 0.0);
80 }
81 if (TIC_RIGHT_IN & ctx->cfg.tic) {
82 cairo_move_to(ctx->cr,
83 ctx->offs.x + ctx->dims.x, v);
84 cairo_rel_line_to(ctx->cr,
85 -ctx->cfg.ticline.len, 0.0);
86 }
87 if (TIC_RIGHT_OUT & ctx->cfg.tic) {
88 cairo_move_to(ctx->cr,
89 ctx->offs.x + ctx->dims.x, v);
90 cairo_rel_line_to(ctx->cr,
91 ctx->cfg.ticline.len, 0.0);
92 }
93 }
94
95 cairo_stroke(ctx->cr);
96 }
97