* Compiles but segfaults.
[scdataviz] / graph.c
diff --git a/graph.c b/graph.c
index 66b7e6c..4c75464 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -6,20 +6,20 @@
 ** 
 
 
-    Copyright (C) 2008 Joseph Pingenot
+Copyright (C) 2008 Joseph Pingenot
 
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU Affero General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU Affero General Public License for more details.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
 
-    You should have received a copy of the GNU Affero General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 **Many thanks to Davyd Madeley for the excellent Cairo  Tutorial
@@ -53,7 +53,7 @@ Graph *graph_new(void) {
 int graph_add_graph_point(Graph* graph, struct graph_point *pt) {
   if(pt == NULL) return 1;
   g_ptr_array_add(graph->points, pt);
-  if(graph->points->len == 1) {
+  if((graph->points->len == 1) && ((graph->lines == NULL) || (graph->lines->len == 0))) {
     graph->maxx = graph->minx = pt->x;
     graph->maxy = graph->miny = pt->y;
   }else{
@@ -76,9 +76,62 @@ int graph_add_point(Graph* graph, double x, double y, const GString *label) {
   pt->x = x;
   pt->y = y;
   pt->label = (label)?g_string_new(label->str):NULL;
-  /*Easiest way to do a line: the control points are the same as this point*/
-  pt->bezier_from_x = pt->bezier_to_x = x;
-  pt->bezier_from_y = pt->bezier_to_y = y;
   graph_add_graph_point(graph, pt);
   return 0;
 }
+
+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) {
+  if(graph->lines == NULL) {
+    if((graph->points = g_ptr_array_new()) == NULL) return 1;
+  }
+  struct graph_line *l;
+  if((l=(struct graph_line*)malloc(sizeof(struct graph_line))) == NULL) return 2;
+  l->p0_x=p0_x;
+  l->p0_y=p0_y;
+  l->p1_x=p1_x;
+  l->p1_y=p1_y;
+  l->p2_x=p2_x;
+  l->p2_y=p2_y;
+  l->p3_x=p3_x;
+  l->p3_y=p3_y;
+  g_ptr_array_add(graph->lines, l);
+  if((graph->points->len == 0) && (graph->lines->len == 1)) {
+    graph->maxx = graph->minx = p0_x;
+    graph->maxy = graph->miny = p0_y;
+  }else{
+    if(p0_x > graph->maxx) graph->maxx = p0_x;
+    if(p0_x < graph->minx) graph->minx = p0_x;
+    if(p0_y > graph->maxy) graph->maxy = p0_y;
+    if(p0_y < graph->miny) graph->miny = p0_y;
+  }
+  if(p3_x > graph->maxx) graph->maxx = p3_x;
+  if(p3_x < graph->minx) graph->minx = p3_x;
+  if(p3_y > graph->maxy) graph->maxy = p3_y;
+  if(p3_y < graph->miny) graph->miny = p3_y;
+  return 0;
+}
+    
+struct cxt {
+  Graph *graph;
+  struct graph_point *p0;
+};
+    
+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;
+    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;
+}
+    
+void graph_add_linear_connectors(Graph* graph) {
+  struct cxt cxt;
+  cxt.graph = graph;
+  cxt.p0 = NULL;
+  g_ptr_array_foreach(graph->points, &linear_interpolate, &cxt);
+}