Mathc gnuplot/Présentation de la librairie

Un livre de Wikilivres.
Mathc gnuplot
Mathc gnuplot
Mathc gnuplot
Sommaire

I - Dessiner

Fichiers h partagés :

Application :

II - Animer

Application :

III - Géométrie de la tortue standard

Application :

IV - Géométrie de la tortue vectorielle

Application :

Annexe

Livre d'or



Préambule[modifier | modifier le wikicode]

La géométrie de la tortue dans Wikipedia.

Dans ce chapitre, nous présenterons un exemple (c01.c) et la librairie (*.h). Le code des fonctions de la librairie ne sont pas à étudier. Dans un premier temps, amusez-vous simplement avec ces fonctions.

L'étude de ce chapitre peut ce faire à l'aide de cette [Playlist]..


Présentation[modifier | modifier le wikicode]

  • Les commandes d'initialisation :
    • **U = GINIT(-10.,10.,-10.,10.);
      • création de la matrice.
      • initialisation de la fenêtre de gnuplot
    • F_mR(U); Destruction de la matrice.
  • Les commandes de déplacement :
    • SETUP(U,angle,x,y); Positionner la tortue.
    • GO(U,+P); Avancer de P pas.
    • GO(U,-P); Reculer de P pas.
    • TU(U,+D); Tourner de D degrés sur la droite.
    • TU(U,-D); Tourner de D degrés sur la gauche.
  • La direction:
    • Les angles positifs tournent dans le sens des aiguilles d'une montre.
      • L'angle 0 est le nord.
    • La direction est mémorisée.

Dessiner[modifier | modifier le wikicode]

c01.c
Dessiner un carré
/* ------------------------------------ */
/*  Save as :   c01.c                   */
/* ------------------------------------ */
#include "v_a.h"
#include "y_o.h"
/* ------------------------------------ */
int main(void)
{
double **U = GINIT(-10.,10.,-10.,10.);
int      i = 4;

   clrscrn();
   
   for(;i--;)
      {GO(U,5.);TU(U,90.);}

   F_mR(U);

  printf("  * open the file main.plt with Gnuplot.\n\n\n");
  getchar();

  return 0;
}

Le résultat :

 # Gnuplot file : load "a_main.plt"
 set zeroaxis
 set size ratio -1
 plot [-10.000:10.000] [-10.000:10.000] \
 "data.plt" with linesp pt 0
Résultat dans gnuplot
Turtles01


Les fichiers h partagés[modifier | modifier le wikicode]

v_a.h
Appel des fichiers
/* ------------------------------------ */
/*      Save as :  v_a.h                */
/* ------------------------------------ */
#include    <stdio.h>
#include   <stdlib.h>
#include   <stddef.h>
#include    <ctype.h>
#include     <time.h>
#include     <math.h>
/* ------------------------------------ */
#include  "vdefine.h"
#include  "vmatini.h"
#include  "vmatbas.h"
#include  "vmatcop.h"
#include  "vmatrot.h"


vdefine.h
Déclaration des defines
/* ------------------------------------ */
/*      Save as : vdefine.h             */
/* ------------------------------------ */
#define C0               0
#define C1               1
#define C2               2
#define C3               3
#define C4               4
#define C5               5

#define R0               0
#define R1               1
#define R2               2
#define R3               3
#define R4               4
#define R5               5

#define OF               0

#define R_SIZE           0
#define C_SIZE           1
#define C_SIZE_A         2
#define FIRST            1

#ifndef PI
#define PI               3.14159265359
#endif

#define MAX(A,B) ((A)>(B) ? (A):(B) )

void clrscrn(void)
{
  printf("\n\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n\n\n\n\n\n\n\n");
}


L'étude sur les matrices fait partie d'un autre livre.


vmatini.h
Création et destruction d'une matrice
/* ------------------------------------ */
/*      Save as : vmatini.h             */
/* -------------------------------------*/
double **I_mR(
int      r,
int      c
)
{
int      i = R0;
int     ar = r + C1;
int     ac = c + C1;
double **A = malloc(ar * sizeof(*A));

     for(; i<ar; i++)
      A[i] = malloc(ac * sizeof(**A));

    A[R_SIZE][OF] = ar;
    A[C_SIZE][OF] = ac;

return(A);
}
/* ------------------------------------ */
void F_mR(
double **A
)
{
int i=R0;
int r=A[R_SIZE][OF];

 if(A) for(;i<r;i++) free(A[i]);

 free(A);
}


vmatbas.h
Additionner et multiplier des matrices
/* ------------------------------------ */
/*      Save as : vmatbas.h             */
/* ------------------------------------ */
double **add_mR(
double **A,
double **B,
double **AplsB
)
{
int r;
int c;

   for (r=FIRST;r<A[R_SIZE][OF];r++)
    for(c=FIRST;c<A[C_SIZE][OF];c++)
        AplsB[r][c]=A[r][c]+B[r][c];

return(AplsB);
}
/* ------------------------------------ */
double **mul_mR(
double **A,
double **B,
double **AB
)
{
int i,j,k;

  for  (k=FIRST;           k<A[R_SIZE][OF];k++)
   for (j=FIRST;           j<B[C_SIZE][OF];j++)
    for(i=FIRST,AB[k][j]=0;i<A[C_SIZE][OF];i++)
       AB[k][j]+=A[k][i]*B[i][j];

return(AB);
}


vmatcop.h
Copier une matrice
/* ------------------------------------ */
/*      Save as : vmatcop.h             */
/* ------------------------------------ */
double ** c_mR(
double **A,
double **B
)
{
int r;
int c;

for( r=FIRST;r<A[R_SIZE][OF];r++)
 for(c=FIRST;c<A[C_SIZE][OF];c++)
     B[r][c]=A[r][c];

return(B);
}
/* ------------------------------------ */
double  **c_a_A_mR(
double  a[],
double  **A
)
{
int r;
int c;
int i=0;

for( r=FIRST; r<A[R_SIZE][OF]; r++)
 for(c=FIRST; c<A[C_SIZE][OF]; c++)
     A[r][c] = a[i++];
            
return(A);
}


vmatrot.h
Matrice de rotation
/* ------------------------------------ */
/*      Save as : vmatrot.h             */
/* ------------------------------------ */
double **rot2D_mR(
double  **A,
double alpha
)
{
 A[1][1]=cos(alpha);A[1][2]=-sin(alpha);
 A[2][1]=sin(alpha);A[2][2]= cos(alpha);

return(A);
}


y_o.h
La librairie de géométrie de la tortue standard
/* ------------------------------------ */
/*      Save as : y_o.h                 */
/* ------------------------------------ */
void PD(
double **A
)
{
FILE * fp = fopen("data.plt","a");
       
fprintf(fp,"  %+.3f  %+.3f \n",
           A[R1][C1],A[R2][C1]);
 fclose(fp);
}
/* ------------------------------------ */
void PU(
double **A
)
{
FILE *fp = fopen("data.plt","a");
       
fprintf(fp,"\n  %+.3f  %+.3f \n",
           A[R1][C1],A[R2][C1]);
 fclose(fp);
}
/* ------------------------------------ */
double **Ginit(
double **U,
double xmin,
double xmax,
double ymin,
double ymax
)
{
FILE *fp;

        fp = fopen("a_main.plt","w");
fprintf(fp,"# Gnuplot file : load \"a_main.plt\" \n"
           "reset\n" 
           "set zeroaxis\n" 
           "set size ratio -1\n" 
           "plot [%0.3f:%0.3f] [%0.3f:%0.3f] \\\n"
           "\"data.plt\" with linesp pt 0\n"
           ,xmin,xmax,ymin,ymax);
 fclose(fp);

        fp = fopen("data.plt","w");
 fclose(fp);

        U[R0][C1] = 0.;/* angle */
        U[R1][C1] = 0.;/* x     */
        U[R2][C1] = 0.;/* y     */

        PD(U);

return(U);
}
/* ------------------------------------ */
double **GINIT(
double xmin,
double xmax,
double ymin,
double ymax
)
{
return( Ginit(I_mR(R2,C1),xmin,xmax,ymin,ymax) );
}
/* ------------------------------------ */
void SET(
double **U,
double angle,
double x,
double y
)
{
   U[R0][C1] = angle;
   U[R1][C1] = x;
   U[R2][C1] = y;

   PD(U);
}
/* ------------------------------------ */
void SETUP(
double **U,
double angle,
double x,
double y
)
{
   U[R0][C1] = angle;
   U[R1][C1] = x;
   U[R2][C1] = y;

   PU(U);
}
/* ------------------------------------ */
void GO(
double **U,
double Step
)
{
double **T = I_mR(R2,C2);
double **B = I_mR(R2,C1);
double **C = I_mR(R2,C1);

double angle=U[R0][C1];

   B[R1][C1] = 0.;
   B[R2][C1] = Step;

   rot2D_mR(T,PI/180.*(-angle));
   mul_mR(T,B,C);
   c_mR(U,B);
   add_mR(B,C,U);

   PD(U);

F_mR(C);
F_mR(B);
F_mR(T);
}
/* ------------------------------------ */
void GU(
double **U,
double Step
)
{
double **T = I_mR(R2,C2);
double **B = I_mR(R2,C1);
double **C = I_mR(R2,C1);

double angle=U[R0][C1];

   B[R1][C1] = 0.;
   B[R2][C1] = Step;

   rot2D_mR(T,PI/180.*(-angle));
   mul_mR(T,B,C);
   c_mR(U,B);
   add_mR(B,C,U);

   PU(U);

F_mR(C);
F_mR(B);
F_mR(T);
}
/* ------------------------------------ */
void TU(
double **U,
double angle
)
{
  U[R0][C1]+=angle;
}