| 12
 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
 99
 
 | #include <iostream>
#include <math.h>
#include <gtk/gtk.h>
 
#define TAILLE 100
#define LENGHT 640
#define HEIGHT 480
#define ACCURACY 0.1
 
using namespace std;
 
 
static GdkPixmap *pixmap = NULL	;
 
float fonction(int i,float acc)
{
      float f,x;
      x=i*acc;
      f=cos(x);
      return f;      
 
}
 
static gboolean realisation( GtkWidget *aire_de_dessin, GdkEventConfigure *event )
{
 
    int t[TAILLE];
    const int MIDDLE=(HEIGHT)*2/3;
    int X,Y;// Y=middle-toint(t[i])
    int j=1; //for t
    int STEP;
 
 
 
      for (int i=1; i<(TAILLE); i=i+1)
      {
          Y=(MIDDLE - (MIDDLE/3)*fonction(i,ACCURACY));
 
          t[i]=Y; 
 
      }
    STEP= 1.3*LENGHT / (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 (int i=1;i<(LENGHT);i=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, t[j+1] );
	    ++j;
 
    }
 
    gdk_draw_line( pixmap, aire_de_dessin->style->black_gc, 0, MIDDLE, LENGHT, MIDDLE );
 
 
	gtk_widget_queue_draw( aire_de_dessin )										;
 
	return TRUE													;
}
 
static gboolean rafraichissement( GtkWidget *aire_de_dessin, GdkEventExpose *event )
{
	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												;
	GtkWidget *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, LENGHT, HEIGHT )								;
	g_signal_connect( G_OBJECT(fenetre), "destroy", G_CALLBACK(gtk_main_quit), NULL )				;
 
 
	aire_de_dessin = gtk_drawing_area_new() 		                       					;
	gtk_container_add( GTK_CONTAINER(fenetre), aire_de_dessin )				                        ;
	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 )		;
 
 
	gtk_widget_show_all(fenetre)											;
	gtk_main()													;
 
	return 0;
} | 
Partager