Mathc gnuplot/Présentation de la librairie vectorielle

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). Les 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 = G_main(-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.
  • vo(U,0,+P); Avancer de P unités.
  • vo(U,0,-P); Reculer de P unités.
  • vo(U,D,0); Contrôler la "D"irection.

La direction :

  • Suit les règles du cercle trigonométrique mais en degrés. Les angles positifs sont mesurés dans le sens inverse des aiguilles d'une montre, à partir de l'axe des x positifs.
  • À chaque déplacement il faut lui indiquer une direction.

Dessiner[modifier | modifier le wikicode]

Dessiner un carré.

c01.c
Dessiner un carré.
/* ------------------------------------ */
/* Save as : c01.c                      */
/* ------------------------------------ */
#include "v_a.h"
#include "y_r.h"
/* ------------------------------------ */
int main(void)
{
double **U   = G_main(-10.,10.,-10.,10.);
double angle = 0.;
double  side = 5.;

   for(;angle<360;angle+=90)
       vo(U,angle,side);

   F_mR(U);

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

  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_r.h
La librairie de géométrie de la tortue vectorielle
/* ------------------------------------ */
/*      Save as : y_r.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 **g_main(
double **A,
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);
 
        A[R1][C1] = 0.;
        A[R2][C1] = 0.;
   
        pd(A);
        
return(A);                         
}
/* ------------------------------------ */   
double **G_main(
double xmin,
double xmax,
double ymin,
double ymax
)
{ 
 return(g_main(I_mR(R2,C1),xmin,xmax,ymin,ymax));     
}
/* ------------------------------------ */   
void set(
double **A,
double x,
double y
)
{
   A[R1][C1] = x;
   A[R2][C1] = y;
   
   pd(A);
}
/* ------------------------------------ */   
void setup(
double **A,
double x,
double y
)
{
   A[R1][C1] = x;
   A[R2][C1] = y;
   
   pu(A);
}
/* ------------------------------------ */   
void vo(
double **A,
double alpha,
double side
)
{
double **T = I_mR(R2,C2);
double **B = I_mR(R2,C1);
double **C = I_mR(R2,C1);

   B[R1][C1] = side;
   B[R2][C1] = 0.;
   
   rot2D_mR(T,PI/180.*(alpha));
     mul_mR(T,B,C);
       c_mR(A,B);
     add_mR(B,C,A);
   
   pd(A);   
   
F_mR(C);
F_mR(B);                        
F_mR(T);
}
/* ------------------------------------ */   
void vu(
double **A,
double alpha,
double side
)
{
double **T = I_mR(R2,C2);
double **B = I_mR(R2,C1);
double **C = I_mR(R2,C1);

   B[R1][C1] = side;
   B[R2][C1] = 0.;
   
   rot2D_mR(T,PI/180.*(alpha));
     mul_mR(T,B,C);
       c_mR(A,B);
     add_mR(B,C,A);
   
   pu(A);   
   
F_mR(C);
F_mR(B);                        
F_mR(T);
}