-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotter.c
67 lines (54 loc) · 1.54 KB
/
plotter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <gtk/gtk.h>
#include <math.h>
const int windowWidth = 500;
const int windowHeight = 500;
const double xO = 250, yO = 0;
const double xScale = 1, yScale = 10;
const float colors[][3] = {
{1.0, 1.0, 1.0},
{1.0, 0.5, 0.5},
{0.5, 1.0, 0.5},
{0.5, 0.5, 1.0},
{1.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{0.0, 1.0, 1.0},
};
static double function(int x) {
//return sin(sqrt(x)) * cos(sqrt(x));
return log(x+sqrt(1+x*x));
}
static double coordConvert(double function(int), double x) {
return windowHeight/2 - (function(x + xO - windowWidth/2) - yO) * yScale;
}
static gboolean draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data) {
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_set_line_width(cr, 2);
cairo_move_to(cr, -100, windowHeight/2);
for (int x = 0; x < windowWidth; x++) {
cairo_line_to(cr, x, coordConvert(function, x));
// if (x % 10 == 9) {
// cairo_set_source_rgb(cr,
// colors[(x/10)%7][0],
// colors[(x/10)%7][1],
// colors[(x/10)%7][2]);
// cairo_stroke(cr);
// }
}
cairo_stroke(cr);
return FALSE;
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *da = gtk_drawing_area_new();
gtk_widget_set_size_request(da, windowWidth, windowHeight);
g_signal_connect(da, "draw", G_CALLBACK(draw_cb), NULL);
gtk_container_add(GTK_CONTAINER(window), da);
gtk_widget_show(da);
gtk_widget_show(window);
gtk_main();
return 0;
}