static void graph_init(Graph *graph) {
graph->symbol = CIRCLE;
graph->points = g_ptr_array_new();
+ graph->lines = NULL;
}
Graph *graph_new(void) {
return g_object_new(GRAPH_TYPE, NULL);
}
-int graph_add_point(Graph* graph, double x, double y) {
- double *point = (double*)malloc(2*sizeof(double));
- if(point == NULL) return 1;
- point[0] = x;
- point[1] = y;
- g_ptr_array_add(graph->points, (gpointer)point);
+/*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->maxx = graph->minx = x;
- graph->maxy = graph->miny = y;
+ graph->maxx = graph->minx = pt->x;
+ graph->maxy = graph->miny = pt->y;
}else{
- if(x > graph->maxx) graph->maxx = x;
- if(x < graph->minx) graph->minx = x;
- if(y > graph->maxy) graph->maxy = y;
- if(y < graph->miny) graph->miny = y;
+ 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;
+ /*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;
+}