Skip to content

Instantly share code, notes, and snippets.

@fgm
Last active January 2, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgm/f92bfc9704d2d8e68d2b to your computer and use it in GitHub Desktop.
Save fgm/f92bfc9704d2d8e68d2b to your computer and use it in GitHub Desktop.
2D graphics example using the g2 library
#! /bin/bash
set -x
# Exécution d'un programme utilisant la bibliothèque graphique g2
name=`basename -s.c $1`
cc $name.c -L/usr/local/lib -lg2 -L/usr/X11R6/lib -lm && ./a.out
main()
{ int i,j; // des variables à nous
int id, id1, id2; // des variables pour g2
// Ouverture d'une fenêtre de travail de 1000x1000 ou de ce que vous voulez
id1 = g2_open_X11(1000, 1000);
// Si on désire en plus une version postcript aussi, on décommentera ces deux lignes
// id2 = g2_open_PS("test.ps", g2_A4, g2_PS_land);
// id2 = g2_open_EPSF("test.ps");
id = g2_open_vd(); // On ouvre le bouffeur
g2_attach(id, id1); // On attache aux unités de sortie
g2_attach(id, id2);
g2_rectangle(id, 10, 10, 990, 990); // Auto-explicatif, non ?
g2_set_font_size(id, 64);
g2_string(id, 20, 420, "g2 text");
g2_pen(id, 19); // Choix d'une couleur
for (i=0; i<35; ++i) // Dessin d'une grille 35x35 de carrés jointifs ou non
{ for(j=0; j<35; ++j)
{
g2_set_line_width(id, 2); // Deux lignes elles aussi auto-explicatives
g2_rectangle(id, 100+20*i, 100+20*j, 100+20*(i+1), 100+20*(j+1));
// g2_flush(id); // Décommentez pour le voir travailler...
// sleep(1); // ... et décommentez ça aussi si votre machine est rapide.
}
}
for (i=0; i<5;++i) for (j=0;j<5; ++j)
{ g2_pen(id, 5*i+j); /// Choix d'autres couleurs quii changent à chaque fois
g2_filled_circle(id, 40+40*i, 40+40*j, 18); // et on trace des cercles (centre, rayon)
}
printf("Appuyez sur ENTREE pur sortir. "); // Celle-là, je l'adore :-D )
getchar(); // On pourrait récupérer le caractère pour changer les couleurs, etc.
// Si vous êtes courageux, vous pouvez même réécrire Corel Draw.
g2_close(id); // Finissons propre
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment