It works! Yay!
[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
35 G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
36
37 static void graph_class_init(GraphClass *klass) {
38   GObjectClass *gobject_klass;
39   gobject_klass = G_OBJECT_CLASS(klass);
40 }
41
42 static void graph_init(Graph *graph) {
43   graph->symbol = CIRCLE;
44   graph->points = g_ptr_array_new();
45 }
46
47 Graph *graph_new(void) {
48   return g_object_new(GRAPH_TYPE, NULL);
49 }
50
51 int graph_add_point(Graph* graph, double x, double y) {
52   double *point = (double*)malloc(2*sizeof(double));
53   if(point == NULL) return 1;
54   point[0] = x;
55   point[1] = y;
56   g_ptr_array_add(graph->points, (gpointer)point);
57   if(graph->points->len == 1) {
58     graph->maxx = graph->minx = x;
59     graph->maxy = graph->miny = y;
60   }else{
61     if(x > graph->maxx) graph->maxx = x;
62     if(x < graph->minx) graph->minx = x;
63     if(y > graph->maxy) graph->maxy = y;
64     if(y < graph->miny) graph->miny = y;
65   }
66   return 0;
67 }