From a954a12134959eab351047489430c57f6281f6c7 Mon Sep 17 00:00:00 2001 From: Joseph Pingenot Date: Tue, 22 Jul 2008 21:55:55 -0500 Subject: [PATCH] It works! Yay! --- graph.c | 2 +- graph.h | 4 ++-- graphwidget.c | 29 +++++++++++++++++++++-------- graphwidget.h | 3 +-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/graph.c b/graph.c index a3a05da..6da0fb6 100644 --- a/graph.c +++ b/graph.c @@ -56,7 +56,7 @@ int graph_add_point(Graph* graph, double x, double y) { g_ptr_array_add(graph->points, (gpointer)point); if(graph->points->len == 1) { graph->maxx = graph->minx = x; - graph->maxy = graph->miny = x; + graph->maxy = graph->miny = y; }else{ if(x > graph->maxx) graph->maxx = x; if(x < graph->minx) graph->minx = x; diff --git a/graph.h b/graph.h index 44ec6d7..33112a5 100644 --- a/graph.h +++ b/graph.h @@ -32,7 +32,7 @@ typedef enum {SQUARE, CIRCLE, STAR, PLUS, MINUS, CROSS} graph_symbol; typedef struct _Graph { - GObject parent; + GObject parent_instance; /*Array of n_points sets of x,y coords*/ GPtrArray *points; double maxx; @@ -43,7 +43,7 @@ typedef struct _Graph { } Graph; typedef struct _GraphClass { - GObject parent; + GObjectClass parent_class; } GraphClass; #define GRAPH_TYPE (graph_get_type()) diff --git a/graphwidget.c b/graphwidget.c index ed9cf4b..3dec40e 100644 --- a/graphwidget.c +++ b/graphwidget.c @@ -38,19 +38,29 @@ struct drawing_context { GtkWidget *widget; cairo_t *cr; double radius; + double xoffset; + double yoffset; + double xscaling; + double yscaling; }; static void draw_point(gpointer data, gpointer user_data) { struct drawing_context *cxt = (struct drawing_context *) user_data; double *d = (double*)data; - cairo_arc(cxt->cr, d[0], d[1], cxt->radius, 0, 2*M_PI); + fprintf(stderr, "draw_point(x=%g(%g) y=%g(%g)) xscaling=%g yscaling=%g xoffset=%g yoffset=%g\n", d[0], (d[0] + cxt->xoffset)*cxt->xscaling, d[1], (d[1] + cxt->yoffset)*cxt->yscaling, cxt->xscaling, cxt->yscaling, cxt->xoffset, cxt->yoffset); + cairo_arc(cxt->cr, (d[0] + cxt->xoffset)*cxt->xscaling, (d[1] + cxt->yoffset)*cxt->yscaling, cxt->radius, 0, 2*M_PI); + cairo_set_source_rgb(cxt->cr, 1, 1, 1); + cairo_fill_preserve(cxt->cr); + cairo_set_source_rgb(cxt->cr, 0, 0, 0); + cairo_stroke(cxt->cr); } static void draw(GtkWidget *graph, cairo_t *cr) { struct drawing_context cxt; + GraphWidget *gw = GRAPH_WIDGET(graph); #ifdef DEBUG - fprintf(stderr, "draw(%d, %d)\n", graph, cr); - fprintf(stderr, "allocation.x=%d, allocation.y=%d, allocation.height=%d, allocation.width=%d\n", graph->allocation.x, graph->allocation.y, graph->allocation.height, graph->allocation.width); + fprintf(stderr, "draw(%d, %d)\n", graph, cr); + fprintf(stderr, "allocation.x=%d, allocation.y=%d, allocation.height=%d, allocation.width=%d\n", graph->allocation.x, graph->allocation.y, graph->allocation.height, graph->allocation.width); #endif double x0=graph->allocation.x; double y0=graph->allocation.y; @@ -58,17 +68,20 @@ static void draw(GtkWidget *graph, cairo_t *cr) { double width=graph->allocation.width; cxt.widget = graph; cxt.cr = cr; - cxt.radius = ((height < width)? height : width)*0.01; + cxt.radius = 0.1; + cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx)); + cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny)); + cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx); + cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny); #ifdef DEBUG fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width); fprintf(stderr, "move_to(%d, %g->%g, %g->%g)\n", cr, graph_data[0], graph->allocation.x + graph_data[0]*(graph->allocation.width), graph_data[1], graph->allocation.y + graph_data[1]*(graph->allocation.height)); #endif cairo_save(cr); - cairo_translate(cr, x0, y0); - cairo_scale(cr, width, height); + cairo_translate(cr, x0, y0+height); + cairo_scale(cr, width, -height); + cairo_set_line_width(cr, 0.05); g_ptr_array_foreach(GRAPH_WIDGET(graph)->graph->points, &draw_point, (gpointer)&cxt); - cairo_set_source_rgb(cr, 0, 0, 0); - cairo_stroke(cr); cairo_restore(cr); } diff --git a/graphwidget.h b/graphwidget.h index be1ebaa..3b57af9 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -33,13 +33,12 @@ #include typedef struct _GraphWidget { - GtkDrawingArea parent; + GtkDrawingArea parent_instance; Graph *graph; } GraphWidget; typedef struct _GraphWidgetClass { GtkDrawingAreaClass parent_class; - } GraphWidgetClass; #define GRAPH_WIDGET_TYPE (graph_widget_get_type()) -- 1.7.9.5