From 2329e96577a27c47e5287118d8808542fbed12ec Mon Sep 17 00:00:00 2001 From: "trelane@digitasaru.net" Date: Fri, 25 Jul 2008 10:23:49 -0500 Subject: [PATCH] * Added bezier routines. * Ready to do the bowing. --- graph.c | 18 ++++++++++++++---- graph.h | 6 ++++++ graphwidget.c | 9 ++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/graph.c b/graph.c index 1b0f468..423ff84 100644 --- a/graph.c +++ b/graph.c @@ -125,10 +125,10 @@ static void linear_interpolate(gpointer data, gpointer user_data) { struct cxt *cxt = user_data; struct graph_point *p3 = data; if(cxt->p0 != NULL) { - double p1_x = (p3->x - cxt->p0->x)/3.0; - double p1_y = (p3->y - cxt->p0->y)/3.0; - double p2_x = (p3->x + 2*cxt->p0->x)/3.0; - double p2_y = (p3->y + 2*cxt->p0->y)/3.0; + double p1_x = (p3->x + 2*cxt->p0->x)/3.0; + double p1_y = (p3->y + 2*cxt->p0->y)/3.0; + double p2_x = (2*p3->x + cxt->p0->x)/3.0; + double p2_y = (2*p3->y + cxt->p0->y)/3.0; graph_add_line(cxt->graph, cxt->p0->x, cxt->p0->y, p1_x, p1_y, p2_x, p2_y, p3->x, p3->y); } cxt->p0 = p3; @@ -140,3 +140,13 @@ void graph_add_linear_connectors(Graph* graph) { cxt.p0 = NULL; g_ptr_array_foreach(graph->points, &linear_interpolate, &cxt); } + +void graph_bezier_linear_to_cubic(double x0, double x3, double *x1, double *x2) { + *x1 = (x3 + 2.0*x0)/3.0; + *x2 = (2.0*x3 + x0)/3.0; +} + +void graph_bezier_qudratic_to_cubic(double x0, double x3, double *x1, double *x2) { + *x2 = (x3 - x0)/3.0 + (*x1); + *x1 = (x0 - 3.0*(*x1))/2.0; +} diff --git a/graph.h b/graph.h index 3930845..68aa97b 100644 --- a/graph.h +++ b/graph.h @@ -81,5 +81,11 @@ int graph_add_point(Graph* graph, double x, double y, const GString *label); int graph_add_graph_point(Graph* graph, struct graph_point *pt); int graph_add_line(Graph* graph, double p0_x, double p0_y, double p1_x, double p1_y, double p2_x, double p2_y, double p3_x, double p3_y); void graph_add_linear_connectors(Graph* graph); +/*Provide x0, x3; x1 and x2 will be set*/ +void graph_bezier_linear_to_cubic(double x0, double x3, double *x1, double *x2); +/*Provide x0, x1, x2. x1 and x2 will be set appropriately.*/ +void graph_bezier_qudratic_to_cubic(double x0, double x3, double *x1, double *x2); + + #endif /* !GRAPHCLASS_H_ */ diff --git a/graphwidget.c b/graphwidget.c index 7dbfbbd..7f2d944 100644 --- a/graphwidget.c +++ b/graphwidget.c @@ -72,6 +72,9 @@ static void draw_lines(gpointer data, gpointer user_data) { double p2_y = (l->p2_y + cxt->yoffset)*cxt->yscaling; double p3_x = (l->p3_x + cxt->xoffset)*cxt->xscaling; double p3_y = (l->p3_y + cxt->yoffset)*cxt->yscaling; + #ifdef DEBUG + fprintf(stderr, "draw_lines: p0_x=%g, p0_y=%g, p1_x=%g, p1_y=%g, p2_x=%g, p2_y=%g, p3_x=%g, p3_y=%g\n", p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y); + #endif cairo_move_to(cxt->cr, p0_x, p0_y); cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y); } @@ -110,11 +113,15 @@ static void draw(GtkWidget *graph, cairo_t *cr) { cairo_select_font_face (cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size (cr, 0.05); - g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt); cairo_set_source_rgb(cr, 1, 1, 1); cairo_fill_preserve(cr); cairo_set_source_rgb(cr, 0, 0, 0); cairo_stroke(cr); + g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt); + if(gw->graph->lines != NULL) { + g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt); + cairo_stroke(cr); + } cairo_restore(cr); } -- 1.7.9.5