fix coordinames - ploot - simple plotting tools
 (HTM) git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit eb743275f407b73af41d4c5559f173e3e54d7cbd
 (DIR) parent 543398796b10e8f93841918a617933c853efe0ff
 (HTM) Author: Josuah Demangeon <mail@josuah.net>
       Date:   Mon, 30 Apr 2018 20:39:04 +0200
       
       fix coordinames
       
       Diffstat:
         M Makefile                            |       2 ++
         M ffdraw.c                            |      25 ++++++++++++++-----------
         M ffdraw.h                            |       2 +-
         A ffplot.c                            |       0 
         M font_14x7.c                         |       2 +-
         M main.c                              |     108 ++++++++++++++++++++++++++++---
       
       6 files changed, 117 insertions(+), 22 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       @@ -17,3 +17,5 @@ clean:x
                rm -f *.o ploot
        
        x:
       +
       +$(SRC): arg.h ffdraw.h font.h font_14x7.h
 (DIR) diff --git a/ffdraw.c b/ffdraw.c
       @@ -11,20 +11,24 @@
        #include <stdlib.h>
        
        #include "ffdraw.h"
       -#include "font_14x7.h"
        
        #define WIDTH 100
        #define HEIGHT 100
        
        Color buffer[WIDTH * HEIGHT];
        
       +/*
       + * Convert (x,y) coordinates to (row,col) for printing into the buffer.
       + * The buffer only contain one number, so the coordinate is a single integer:
       + *        width * x + y.
       + */
        void
        ffdraw_pixel(Canvas *can, Color col,
                int x, int y)
        {
       -        if (x >= can->w || y >= can->h)
       +        if (x >= can->h || y >= can->w)
                        return;
       -        memcpy(can->b + x + (can->h - 1 - y) * can->w, col, sizeof(*can->b));
       +        memcpy(can->b + can->w * (can->h - 1 - x) + y, col, sizeof(*can->b));
        }
        
        void
       @@ -87,12 +91,12 @@ ffdraw_char(Canvas *can, Color col, char c, Font *f,
        
                if (c & 0x80)
                        c = '\0';
       -        x -= f->w / 2;
       -        y -= f->h / 2;
       +        x -= f->h / 2;
       +        y -= f->w / 2;
        
       -        for (xf = 0; xf < f->w; xf++)
       -                for (yf = 0; yf < f->h; yf++)
       -                        if (f->b[(int)c][(f->h - yf - 1) * f->w + xf] > 0)
       +        for (xf = 0; xf < f->h; xf++)
       +                for (yf = 0; yf < f->w; yf++)
       +                        if (f->b[(int)c][f->w * (f->h - xf - 1) + yf] > 0)
                                        ffdraw_pixel(can, col, x + xf, y + yf);
        }
        
       @@ -103,13 +107,12 @@ void
        ffdraw_str(Canvas *can, Color col, char *s, Font *f,
                int x, int y)
        {
       -        for (; *s; x += f->w, s++)
       +        for (; *s != '\0'; y += f->w, s++)
                        ffdraw_char(can, col, *s, f, x, y);
       -                
        }
        
        void
        ffdraw_fill(Canvas *can, Color col)
        {
       -        ffdraw_rectangle(can, col, 0, 0, can->w - 1, can->h - 1);
       +        ffdraw_rectangle(can, col, 0, 0, can->h - 1, can->w - 1);
        }
 (DIR) diff --git a/ffdraw.h b/ffdraw.h
       @@ -12,7 +12,7 @@ typedef struct {
        typedef struct {
                int w;                /* width */
                int h;                /* height */
       -        char *b[255];        /* buffer */
       +        char *b[128];        /* buffer */
        } Font;
        
        /* ffdraw.c */
 (DIR) diff --git a/ffplot.c b/ffplot.c
 (DIR) diff --git a/font_14x7.c b/font_14x7.c
       @@ -586,7 +586,7 @@ C(A) = {
                _,_,_,_,_,_,_,
                _,_,_,_,_,_,_,
                _,_,_,X,_,_,_,
       -        _,_,X,_,X,_,_,
       +        _,_,_,X,_,_,_,
                _,_,X,_,X,_,_,
                _,_,X,_,X,_,_,
                _,X,_,_,_,X,_,
 (DIR) diff --git a/main.c b/main.c
       @@ -2,14 +2,101 @@
        
        #include <stdlib.h>
        #include <stdio.h>
       +#include <time.h>
        
        #include "ffdraw.h"
        #include "font_14x7.h"
        
       -#define WIDTH 500
       -#define HEIGHT 100
       +/* as you see, no css skills needed */
        
       -Color buffer[WIDTH * HEIGHT];
       +#define MARGIN 5
       +#define FONT_H 14
       +#define FONT_W 7
       +
       +/* height */
       +
       +#define TITLE_H (MARGIN + FONT_H + MARGIN)
       +#define PLOT_H 100
       +#define XLABEL_H (MARGIN + FONT_H + MARGIN)
       +
       +#define IMAGE_H (TITLE_H + PLOT_H + XLABEL_H)
       +
       +#define TITLE_B (0)
       +#define TITLE_E (TITLE_H)
       +#define PLOT_X_B (TITLE_H)
       +#define PLOT_X_E (IMAGE_H - XLABEL_H)
       +#define XLABEL_B (IMAGE_H - XLABEL_H)
       +#define XLABEL_E (IMAGE_H)
       +
       +/* width */
       +
       +#define YLABEL_W (MARGIN + 50 + MARGIN)
       +#define PLOT_W 500
       +#define LEGEND_W (MARGIN + 70 + MARGIN)
       +
       +#define IMAGE_W (YLABEL_W + PLOT_W + LEGEND_W)
       +
       +#define YLABEL_B (0)
       +#define YLABEL_E (YLABEL_W)
       +#define PLOT_Y_B (YLABEL_W)
       +#define PLOT_Y_E (IMAGE_W - LEGEND_W)
       +#define LEGEND_B (IMAGE_W - LEGEND_W)
       +#define LEGEND_E (IMAGE_W)
       +
       +#define MID(x, y) ((x - y) / 2)
       +
       +Color buffer[IMAGE_W * IMAGE_H];
       +
       +Color c_axis = { 0xffff, 0xffff, 0xffff, 0xfff };
       +Font *font = &font_14x7;
       +
       +void
       +ffplot_xaxis(Canvas *can, Color col, time_t tmax, time_t tstep)
       +{
       +        time_t t;
       +        int x, y, ystep, width;
       +        char str[sizeof("YYYY/MM/DD")], *fmt;
       +
       +        if (tstep < 3600) {
       +                fmt = "%H:%M:%S";
       +                width = sizeof("HH:MM:SS");
       +        } else {
       +                fmt = "%Y/%m/%d";
       +                width = sizeof("YYYY/MM/DD");
       +        }
       +
       +        ystep = MARGIN + FONT_W * width + MARGIN;
       +
       +        t = tmax % tstep;
       +        x = XLABEL_B + FONT_H / 2;
       +        y = PLOT_Y_B + PLOT_W % ystep - width / 2;
       +
       +        while (y > PLOT_Y_B) {
       +                strftime(str, sizeof(str), fmt, localtime(&t));
       +                ffdraw_str(can, col, str, font, x, y);
       +
       +                y -= ystep;
       +                t -= tstep;
       +        }
       +}
       +
       +void
       +ffplot_(Canvas *can)
       +{
       +        (void)can;
       +}
       +
       +void
       +ffplot_graph(Canvas *can)
       +{
       +        (void)can;
       +}
       +
       +void
       +ffplot_legend(Canvas *can)
       +{
       +        (void)can;
       +}
        
        static void
        ffdraw(Canvas *can)
       @@ -18,8 +105,11 @@ ffdraw(Canvas *can)
                Color col2 = { 0x3333, 0xffff, 0x8888, 0xffff };
        
                ffdraw_fill(can, col1);
       +        ffplot_xaxis(can, col2, 3600 * 24 * 30, 360);
       +/*
                ffdraw_line(can, col2, 49,1,9,79);
       -        ffdraw_str(can, col2, "R\\S`T'UaVbWcYdZe\nfghb\tjoi\rklmnopqrstuvwxyz{|}", &font_14x7, 44, 50);
       +        ffdraw_str(can, col2, "R\\S`T'UaVbWcYdZe\nfghb\tjoi\rklmnopqrstuvwxyz{|}", font, 44, 10);
       +*/
        }
        
        int
       @@ -29,14 +119,14 @@ main(void)
                Canvas can;
        
                can.b = buffer;
       -        can.w = WIDTH;
       -        can.h = HEIGHT;
       -        w = htonl(WIDTH);
       -        h = htonl(HEIGHT);
       +        can.w = IMAGE_W;
       +        can.h = IMAGE_H;
       +        w = htonl(IMAGE_W);
       +        h = htonl(IMAGE_H);
                fputs("farbfeld", stdout);
                fwrite(&w, sizeof(w), 1, stdout);
                fwrite(&h, sizeof(h), 1, stdout);
                ffdraw(&can);
       -        fwrite(can.b, WIDTH * HEIGHT, sizeof(*can.b), stdout);
       +        fwrite(can.b, IMAGE_W * IMAGE_H, sizeof(*can.b), stdout);
                return 0;
        }