1b0f4685303145fd16d756b21f1202184e63917c
[scdataviz] / graph.c
1 /*
2 ** Graph.c
3 ** 
4 ** Made by (Johnny Q. Hacker)
5 ** Login   <solarion@johnathan>
6 ** 
7
8
9 Copyright (C) 2008 Joseph Pingenot
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
25 **Many thanks to Davyd Madeley for the excellent Cairo  Tutorial
26 **  at http://gnomejournal.org/article/34/writing-a--using-cairo-and-gtk28
27
28 ** Started on  Thu Jul 17 10:51:32 2008 Johnny Q. Hacker
29 ** Last update Sun May 12 01:17:25 2002 Speed Blue
30 */
31
32 #include <graph.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35
36 G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
37
38 static void graph_class_init(GraphClass *klass) {
39   GObjectClass *gobject_klass;
40   gobject_klass = G_OBJECT_CLASS(klass);
41 }
42
43 static void graph_init(Graph *graph) {
44   graph->symbol = CIRCLE;
45   graph->points = g_ptr_array_new();
46   graph->lines = NULL;
47 }
48
49 Graph *graph_new(void) {
50   return g_object_new(GRAPH_TYPE, NULL);
51 }
52
53 /*1 means you're stupid and passed NULL*/
54 int graph_add_graph_point(Graph* graph, struct graph_point *pt) {
55   if(pt == NULL) return 1;
56   g_ptr_array_add(graph->points, pt);
57   if((graph->points->len == 1) && ((graph->lines == NULL) || (graph->lines->len == 0))) {
58     graph->maxx = graph->minx = pt->x;
59     graph->maxy = graph->miny = pt->y;
60   }else{
61     if(pt->x > graph->maxx) graph->maxx = pt->x;
62     if(pt->x < graph->minx) graph->minx = pt->x;
63     if(pt->y > graph->maxy) graph->maxy = pt->y;
64     if(pt->y < graph->miny) graph->miny = pt->y;
65   }
66   return 0;
67 }
68
69 /*
70  *
71  *1 means failed to allocate point
72  *NOTE: label is duplicated if not NULL (using g_strdup)
73  */
74 int graph_add_point(Graph* graph, double x, double y, const GString *label) {
75   struct graph_point *pt = (struct graph_point*)malloc(sizeof(struct graph_point));
76   if(pt == NULL) return 1;
77   pt->x = x;
78   pt->y = y;
79   pt->label = (label)?g_string_new(label->str):NULL;
80   graph_add_graph_point(graph, pt);
81   return 0;
82 }
83
84 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) {
85   #ifdef DEBUG
86   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);
87   #endif
88   if(graph->lines == NULL) {
89     if((graph->lines = g_ptr_array_new()) == NULL) return 1;
90   }
91   struct graph_line *l;
92   if((l=(struct graph_line*)malloc(sizeof(struct graph_line))) == NULL) return 2;
93   l->p0_x=p0_x;
94   l->p0_y=p0_y;
95   l->p1_x=p1_x;
96   l->p1_y=p1_y;
97   l->p2_x=p2_x;
98   l->p2_y=p2_y;
99   l->p3_x=p3_x;
100   l->p3_y=p3_y;
101   g_ptr_array_add(graph->lines, l);
102   if((graph->points->len == 0) && 
103      (graph->lines->len == 1)) {
104     graph->maxx = graph->minx = p0_x;
105     graph->maxy = graph->miny = p0_y;
106   }else{
107     if(p0_x > graph->maxx) graph->maxx = p0_x;
108     if(p0_x < graph->minx) graph->minx = p0_x;
109     if(p0_y > graph->maxy) graph->maxy = p0_y;
110     if(p0_y < graph->miny) graph->miny = p0_y;
111   }
112   if(p3_x > graph->maxx) graph->maxx = p3_x;
113   if(p3_x < graph->minx) graph->minx = p3_x;
114   if(p3_y > graph->maxy) graph->maxy = p3_y;
115   if(p3_y < graph->miny) graph->miny = p3_y;
116   return 0;
117 }
118     
119 struct cxt {
120   Graph *graph;
121   struct graph_point *p0;
122 };
123     
124 static void linear_interpolate(gpointer data, gpointer user_data) {
125   struct cxt *cxt = user_data;
126   struct graph_point *p3 = data;
127   if(cxt->p0 != NULL) {
128     double p1_x = (p3->x - cxt->p0->x)/3.0;
129     double p1_y = (p3->y - cxt->p0->y)/3.0;
130     double p2_x = (p3->x + 2*cxt->p0->x)/3.0;
131     double p2_y = (p3->y + 2*cxt->p0->y)/3.0;
132     graph_add_line(cxt->graph, cxt->p0->x, cxt->p0->y, p1_x, p1_y, p2_x, p2_y, p3->x, p3->y);
133   }
134   cxt->p0 = p3;
135 }
136     
137 void graph_add_linear_connectors(Graph* graph) {
138   struct cxt cxt;
139   cxt.graph = graph;
140   cxt.p0 = NULL;
141   g_ptr_array_foreach(graph->points, &linear_interpolate, &cxt);
142 }