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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <math.h>
#include <gtk/gtk.h>
#define TAILLE 100
#define LENGTH 640
#define HEIGHT 480
#define ACCURACY 0.1
static GdkPixmap *pixmap = NULL;
gint var;
static gboolean tracer_courbe (GtkWidget *aire_de_dessin, int var)
{
int t[TAILLE];
const int MIDDLE=(HEIGHT)*2/3;
int i, j=1, STEP;
for (i=1; i<TAILLE; i=i+1)
t[i] = ( MIDDLE - (MIDDLE/3)*cos(i*ACCURACY) )- var;
STEP= 1.3*LENGTH / (TAILLE);
pixmap = gdk_pixmap_new ( aire_de_dessin->window,
aire_de_dessin->allocation.width,
aire_de_dessin->allocation.height, -1 );
// draw a white backgroung
gdk_draw_rectangle( pixmap, aire_de_dessin->style->white_gc, TRUE, 0, 0,
aire_de_dessin->allocation.width,
aire_de_dessin->allocation.height );
// draw the function
for (i=1; i<LENGTH; i+=STEP)
{
gdk_draw_line( pixmap, aire_de_dessin->style->black_gc, i,
MIDDLE-2 , i, MIDDLE+2 );
gdk_draw_line( pixmap, aire_de_dessin->style->black_gc, i, t[j],
i+STEP-var, t[j+1]+var );
++j;
}
gdk_draw_line( pixmap, aire_de_dessin->style->black_gc, 0,
MIDDLE, LENGTH, MIDDLE );
gtk_widget_queue_draw( aire_de_dessin );
return TRUE;
}
/* Tracé initial */
static gboolean realisation (GtkWidget *aire_de_dessin, GdkEventConfigure *event)
{
var=0;
tracer_courbe (aire_de_dessin, var);
return TRUE;
}
void clic(GtkWidget *button, GtkWidget *aire_dessin)
{
var +=20; /* à chaque clic, amplifie le bruit */
tracer_courbe (aire_dessin, var);
}
static gboolean rafraichissement( GtkWidget *aire_de_dessin, GdkEventExpose *event )
{
/*
gdk_window_clear_area (aire_de_dessin->window,
event->area.x, event->area.y,
event->area.width, event->area.height);
*/
gdk_draw_drawable (aire_de_dessin->window,
aire_de_dessin->style->fg_gc[GTK_WIDGET_STATE(aire_de_dessin)],
pixmap, event->area.x, event->area.y,
event->area.x, event->area.y, -1, -1);
return FALSE;
}
int main(int argc, char **argv)
{
GtkWidget *fenetre, *box, *button, *aire_de_dessin;
gtk_init(&argc, &argv);
fenetre = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title( GTK_WINDOW(fenetre), "Cosinus" );
gtk_widget_set_size_request( fenetre, LENGTH, HEIGHT );
g_signal_connect( G_OBJECT(fenetre), "destroy", G_CALLBACK(gtk_main_quit), NULL );
box = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (fenetre), box);
aire_de_dessin = gtk_drawing_area_new();
gtk_box_pack_start (GTK_BOX(box), aire_de_dessin, TRUE, TRUE, 0);
g_signal_connect( G_OBJECT(aire_de_dessin), "realize", G_CALLBACK(realisation), NULL );
g_signal_connect( G_OBJECT(aire_de_dessin), "expose_event", G_CALLBACK(rafraichissement), NULL );
button = gtk_button_new_with_label ("Dessiner");
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (clic), (gpointer)aire_de_dessin);
gtk_box_pack_end (GTK_BOX(box), button, FALSE, FALSE, 0);
gtk_widget_show_all(fenetre);
gtk_main();
return 0;
} |
Partager