Moving files around, starting libtoolization.
[scdataviz] / src / graph.c
diff --git a/src/graph.c b/src/graph.c
new file mode 100644 (file)
index 0000000..cd2b056
--- /dev/null
@@ -0,0 +1,242 @@
+/*
+** Graph.c
+** 
+** Made by (Johnny Q. Hacker)
+** Login   <solarion@johnathan>
+** 
+
+
+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 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/>.
+
+
+**Many thanks to Davyd Madeley for the excellent Cairo  Tutorial
+**  at http://gnomejournal.org/article/34/writing-a--using-cairo-and-gtk28
+
+** Started on  Thu Jul 17 10:51:32 2008 Johnny Q. Hacker
+** Last update Sun May 12 01:17:25 2002 Speed Blue
+*/
+
+#include <graph.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+
+#ifdef DEBUG
+#define DEBUG_AUTOAXIS
+#endif
+
+G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
+
+static void graph_class_init(GraphClass *klass) {
+  GObjectClass *gobject_klass;
+  gobject_klass = G_OBJECT_CLASS(klass);
+}
+
+static void graph_init(Graph *graph) {
+  graph->symbol = CIRCLE;
+  graph->points = g_ptr_array_new();
+  graph->lines = NULL;
+  graph->xaxis = graph->yaxis = NULL;
+}
+
+Graph *graph_new(void) {
+  return g_object_new(GRAPH_TYPE, NULL);
+}
+
+/*1 means you're stupid and passed NULL*/
+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) && ((graph->lines == NULL) || (graph->lines->len == 0))) {
+    graph->maxx = graph->minx = pt->x;
+    graph->maxy = graph->miny = pt->y;
+  }else{
+    if(pt->x > graph->maxx) graph->maxx = pt->x;
+    if(pt->x < graph->minx) graph->minx = pt->x;
+    if(pt->y > graph->maxy) graph->maxy = pt->y;
+    if(pt->y < graph->miny) graph->miny = pt->y;
+  }
+  return 0;
+}
+
+/*
+ *
+ *1 means failed to allocate point
+ *NOTE: label is duplicated if not NULL (using g_strdup)
+ */
+int graph_add_point(Graph* graph, double x, double y, const GString *label) {
+  struct graph_point *pt = (struct graph_point*)malloc(sizeof(struct graph_point));
+  if(pt == NULL) return 1;
+  pt->x = x;
+  pt->y = y;
+  pt->label = (label)?g_string_new(label->str):NULL;
+  graph_add_graph_point(graph, pt);
+  return 0;
+}
+
+int graph_add_graph_line(Graph* graph, struct graph_line *l) {
+  if(l == NULL) return 1;
+  if(graph->lines == NULL) {
+    if((graph->lines = g_ptr_array_new()) == NULL) return 1;
+  }
+  g_ptr_array_add(graph->lines, l);
+  if((graph->points->len == 0) && 
+     (graph->lines->len == 1)) {
+    graph->maxx = graph->minx = l->p0_x;
+    graph->maxy = graph->miny = l->p0_y;
+  }else{
+    if(l->p0_x > graph->maxx) graph->maxx = l->p0_x;
+    if(l->p0_x < graph->minx) graph->minx = l->p0_x;
+    if(l->p0_y > graph->maxy) graph->maxy = l->p0_y;
+    if(l->p0_y < graph->miny) graph->miny = l->p0_y;
+  }
+  if(l->p3_x > graph->maxx) graph->maxx = l->p3_x;
+  if(l->p3_x < graph->minx) graph->minx = l->p3_x;
+  if(l->p3_y > graph->maxy) graph->maxy = l->p3_y;
+  if(l->p3_y < graph->miny) graph->miny = l->p3_y;
+  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) {
+  #ifdef DEBUG
+  fprintf(stderr, "graph_add_line(%d, 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", (unsigned int) graph, p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
+  #endif
+  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;
+  return graph_add_graph_line(graph, l);
+}
+    
+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 + 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;
+}
+    
+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);
+}
+
+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_linear_to_quadratic(double x0, double x3, double *x1) {
+  *x1 = (x3 + x0)/2.0;
+}
+
+void graph_bezier_quadratic_to_cubic(double x0, double x3, double *x1, double *x2) {
+  *x2 = (x3-2.0*(*x1))/3.0;
+  *x1 = (x0-2.0*(*x1))/3.0;
+}
+
+void graph_set_xaxis(Graph *g, struct graph_axis *axis) {
+  if(g->xaxis != NULL) {
+    free(g->xaxis);
+  }
+  g->xaxis=axis;
+}
+void graph_set_yaxis(Graph *g, struct graph_axis *axis) {
+  if(g->yaxis != NULL) {
+    free(g->yaxis);
+  }
+  g->yaxis=axis;
+}
+
+struct graph_axis* autoset_axis(Graph* g, GString* title, double min, double max) {
+  struct graph_axis *axis;
+  double range_mag;
+  double start_mag;
+  double stop_mag;
+  int i, j;
+  if(g->points->len == 0) {
+    return NULL;
+  }
+  if((axis=(struct graph_axis *)malloc(sizeof(struct graph_axis))) == NULL) {
+    return NULL;
+  }
+  if(g->points->len == 1) {
+    axis->major=1;
+    axis->minor=0;
+    axis->subminor=0;
+    axis->major_start=min;
+    axis->major_step=max;
+  }else{
+    range_mag = logb(max - min);
+    if(min == 0) 
+      start_mag = 0;
+    else
+      start_mag = copysign(pow(10, floor(log10(min))), min);
+    if(max == 0) 
+      stop_mag = 0;
+    else
+      stop_mag = copysign(pow(10, ceil(log10(max))), max);
+    for(i=1; i<9; i++) {
+      if(i*start_mag > min) break;
+    }
+    i--;
+    for(j=10; j>1; j--) {
+      if(j*(stop_mag/10) < max) break;
+    }
+    j++;
+    axis->major=10;
+    axis->minor=1;
+    axis->subminor=4;
+    axis->major_start=i*start_mag;
+    axis->major_step=(j*(stop_mag/10) - axis->major_start)/(axis->major);
+  }
+  axis->title=g_string_new(title->str);
+  #ifdef DEBUG_AUTOAXIS
+  fprintf(stderr, "graph_autoset_xaxis: xmin=%g, log10(xmin)=%g, floor=%g, pow=%g round=%g\n", min, log10(min), floor(log10(min)), pow(10, floor(log10(min))), round(pow(10, floor(log10(min)))));
+  fprintf(stderr, "graph_autoset_xaxis: xmax=%g, log10(xmax)=%g, floor=%g, pow=%g round=%g\n", max, log10(max), floor(log10(max)), pow(10, floor(log10(max))), round(pow(10, floor(log10(max)))));
+  fprintf(stderr, "graph_autoset_xaxis: xmin=%g,max=%g start_mag=%g, stop_mag=%g range_mag=%g\n", min, max, start_mag, stop_mag, range_mag);
+  #endif
+  return axis;
+}
+
+int graph_autoset_xaxis(Graph *g, GString *title) {
+  graph_set_xaxis(g, autoset_axis(g, title, g->minx, g->maxx));
+  return 0;
+}
+
+int graph_autoset_yaxis(Graph *g, GString *title) {
+  graph_set_yaxis(g, autoset_axis(g, title, g->miny, g->maxy));
+  return 0;
+}