Jolis commentaires 💐🌸💮
This commit is contained in:
parent
3b19bde79e
commit
a8f5d7965a
99
main.c
99
main.c
@ -26,11 +26,14 @@ float valmin[MAXC];
|
||||
float valmax[MAXC];
|
||||
int nc = 0;
|
||||
|
||||
// Defines
|
||||
// # Defines
|
||||
|
||||
// Utilisés pour `mode_affichage`
|
||||
#define HORIZONTAL 0
|
||||
#define VERTICAL 1
|
||||
#define SUPERPOSER 2
|
||||
|
||||
// Utilisés pour `type_dessin`
|
||||
#define LIGNE 0
|
||||
#define LIGNE_AXES 1
|
||||
#define HISTOGRAMME 2 // pas un vrai histogramme
|
||||
@ -40,6 +43,10 @@ int vx1 = 0, vx2 = 400, vy1 = 0, vy2 = 400;
|
||||
int mode_affichage = HORIZONTAL;
|
||||
int type_dessin = LIGNE;
|
||||
|
||||
/*
|
||||
Transforme une valeur `val` présente entre `smin` et `smax` et la retourne
|
||||
présente entre `omin` et `omax`
|
||||
*/
|
||||
float map(float smin, float smax, float val, float omin, float omax)
|
||||
{
|
||||
float percent = (val - smin) / (smax - smin);
|
||||
@ -47,6 +54,12 @@ float map(float smin, float smax, float val, float omin, float omax)
|
||||
return (omax-omin) * percent + omin;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Affiche deux axes allant de :
|
||||
`x1`,`y2` vers `x2`,`y2` et
|
||||
`x2`,`y2` vers `x2`,`y1`
|
||||
*/
|
||||
void axes(float x1, float x2, float y1, float y2)
|
||||
{
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
@ -62,6 +75,13 @@ void axes(float x1, float x2, float y1, float y2)
|
||||
glEnd();
|
||||
}
|
||||
|
||||
/*
|
||||
Trace la courbe à l'index `index` dans le rectangle défini par un point en
|
||||
haut à gauche `hgx` & `hgy` et un point en bas à droite `bdx` & `bdy`
|
||||
|
||||
Fonction soumise au variables globales `type_dessin`, `mode_affichage`,
|
||||
`courbe`, `valmin` et `valmax`
|
||||
*/
|
||||
void ligne(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
{
|
||||
if(index >= nc)
|
||||
@ -81,11 +101,13 @@ void ligne(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
for(unsigned int i = 1; i < size+1; i++)
|
||||
{
|
||||
float pos_x = map(0, size-1, i-1, hgx, bdx);
|
||||
float pos_y = map(valmin[index], valmax[index], courbe[index][i], bdy, hgy);
|
||||
float pos_y = map(valmin[index], valmax[index],
|
||||
courbe[index][i], bdy, hgy);
|
||||
//printf("%d : {%f, %f}\n", i, pos_x, pos_y);
|
||||
|
||||
if(mode_affichage == VERTICAL)
|
||||
{
|
||||
// On inverse les deux lors d'un affichage vertical
|
||||
float temp = pos_x;
|
||||
pos_x = pos_y;
|
||||
pos_y = temp;
|
||||
@ -103,7 +125,8 @@ void ligne(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
for(unsigned int i = 1; i < size+1; i++)
|
||||
{
|
||||
float pos_x = map(0, size-1, i-1, hgx, bdx);
|
||||
float pos_y = map(valmin[index], valmax[index], courbe[index][i], bdy, hgy);
|
||||
float pos_y = map(valmin[index], valmax[index],
|
||||
courbe[index][i], bdy, hgy);
|
||||
if(mode_affichage == VERTICAL)
|
||||
{
|
||||
axes(bdy, pos_y, hgx, pos_x);
|
||||
@ -115,6 +138,15 @@ void ligne(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Trace l'histogramme de la courbe à l'index `index` dans le rectangle défini
|
||||
par un point en haut à gauche `hgx` & `hgy` et
|
||||
un point en bas à droite `bdx` & `bdy`
|
||||
|
||||
Fonction soumise aux variables globales `mode_affichage`, `courbe`,
|
||||
`valmin` et `valmax`
|
||||
*/
|
||||
void histo(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
{
|
||||
if(index >= nc)
|
||||
@ -135,11 +167,14 @@ void histo(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
{
|
||||
float gauche = map(0, size, i-1, hgx, bdx);
|
||||
float droite = map(0, size, i, hgx, bdx);
|
||||
float haut = map(valmin[index], valmax[index], courbe[index][i], bdy, hgy);
|
||||
float haut = map(valmin[index], valmax[index],
|
||||
courbe[index][i], bdy, hgy);
|
||||
float bas = bdy;
|
||||
|
||||
if(mode_affichage == VERTICAL)
|
||||
{
|
||||
/* Si vertical, on change les variable pour faire "tourner" le
|
||||
rectangle */
|
||||
float temp = bas;
|
||||
bas = gauche;
|
||||
gauche = haut;
|
||||
@ -157,6 +192,12 @@ void histo(int index, float hgx, float hgy, float bdx, float bdy)
|
||||
//glEnd();
|
||||
}
|
||||
|
||||
/*
|
||||
Fonction d'affichage appelée par glut
|
||||
|
||||
Fonction soumise au variables globales `type_dessin`, `mode_affichage`,
|
||||
`courbe`, `valmin` et `valmax`
|
||||
*/
|
||||
void afficher(void)
|
||||
{
|
||||
glClearColor(0.0,0.0,0.0,0.0);
|
||||
@ -211,6 +252,10 @@ void afficher(void)
|
||||
glFlush();
|
||||
}
|
||||
|
||||
/*
|
||||
Callback lorsque la fenetre est redimensionnée.
|
||||
Actualise les variables globales `vx2` et `vy2`
|
||||
*/
|
||||
void reshapeCallback(int largeur, int hauteur)
|
||||
{
|
||||
vx2 = largeur;
|
||||
@ -222,35 +267,54 @@ void reshapeCallback(int largeur, int hauteur)
|
||||
//printf("Reshape : [%d, %d]\n", vx2, vy2);
|
||||
}
|
||||
|
||||
/*
|
||||
Initialise la fenètre GLUT, retourne son handle.
|
||||
Attend les valeurs `argc` et `argv` de main ainsi qu'un fonction
|
||||
d'affichage `display`
|
||||
*/
|
||||
int initWindow(int argc, char* argv[], void(*display)(void))
|
||||
{
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
|
||||
glutInitWindowPosition(vx1, vy1);
|
||||
glutInitWindowSize(vx2, vy2);
|
||||
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); // Une seule fenètre, RGB
|
||||
glutInitWindowPosition(vx1, vy1); // Position
|
||||
glutInitWindowSize(vx2, vy2); // Taille
|
||||
|
||||
int window = glutCreateWindow("TP1");
|
||||
|
||||
glutDisplayFunc(display);
|
||||
glutDisplayFunc(display); // Fonction d'affichage
|
||||
|
||||
glutReshapeFunc(reshapeCallback);
|
||||
glutReshapeFunc(reshapeCallback); // Redimensionnement
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
/*
|
||||
Détruit la fenètre passé en paramètre via son handle dans `window`
|
||||
*/
|
||||
void destroyWindow(int window)
|
||||
{
|
||||
glutDestroyWindow(window);
|
||||
}
|
||||
|
||||
/*
|
||||
Retourne la taille d'un fichier `file`
|
||||
|
||||
Le fichier doit être correctement ouvert.
|
||||
*/
|
||||
long getFileSize(FILE* file)
|
||||
{
|
||||
// On va à la fin du fichier et on note notre position dans celui-ci
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
rewind(file);
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
/*
|
||||
Lis le fichier `file` avec une taille `size` et analyse celui-ci pour
|
||||
saisir son contenu dans les variables globales `courbe`,
|
||||
`valmin` et `valmax`
|
||||
*/
|
||||
void parseFile(char* file, long size)
|
||||
{
|
||||
nc = atoi(file);
|
||||
@ -272,18 +336,20 @@ void parseFile(char* file, long size)
|
||||
courbe[line][0] = taille_tableau;
|
||||
//printf(" ->\"%d\" \n", taille_tableau);
|
||||
|
||||
// On suppose que les nombres sont séparés par un espace
|
||||
for(unsigned int i = 1; i < taille_tableau+1; i++)
|
||||
{
|
||||
// On suppose que les nombres sont séparés par un espace
|
||||
pointeur = strchr(pointeur, ' ') + 1;
|
||||
float num = strtof(pointeur, 0);
|
||||
courbe[line][i] = num;
|
||||
|
||||
if(i == 1)
|
||||
{
|
||||
// Première ligne, on alimente directement valmin et valmax
|
||||
valmin[line] = num;
|
||||
valmax[line] = num;
|
||||
}else{
|
||||
// Sinon comparaison
|
||||
if(num < valmin[line])
|
||||
valmin[line] = num;
|
||||
|
||||
@ -296,6 +362,11 @@ void parseFile(char* file, long size)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Charge un fichier au path `nom` et analyse son contenu.
|
||||
Le contenu sera présent dans les variables globales `courbe`, `valmin`
|
||||
et `valmax`
|
||||
*/
|
||||
bool charger(char* nom)
|
||||
{
|
||||
FILE* file = fopen(nom, "r");
|
||||
@ -307,6 +378,7 @@ bool charger(char* nom)
|
||||
|
||||
long fileSize = getFileSize(file);
|
||||
|
||||
// Allouer un tableau dans la mémoire de la tailel du tableau + 1(NULL)
|
||||
char* buffer = (char*)malloc(sizeof(char) * fileSize + 1);
|
||||
if(buffer == NULL)
|
||||
{
|
||||
@ -315,6 +387,7 @@ bool charger(char* nom)
|
||||
}
|
||||
buffer[fileSize] = 0; // NULL à la fin du fichier
|
||||
|
||||
// Lis le fichier dans notre buffer
|
||||
int res = fread(buffer, sizeof(char), fileSize, file);
|
||||
if(res == 0)
|
||||
{
|
||||
@ -322,15 +395,21 @@ bool charger(char* nom)
|
||||
return false;
|
||||
}
|
||||
|
||||
// On libère le fichier
|
||||
fclose(file);
|
||||
|
||||
// On analyse le contenu
|
||||
parseFile(buffer, fileSize);
|
||||
|
||||
// On libère la mémoire, le contenu est stocké dans les variables globales
|
||||
free(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Affiche le contenu de la variable globale `courbe`
|
||||
*/
|
||||
void printCourbeTerminal(void)
|
||||
{
|
||||
for(unsigned int i = 0; i < nc; i++)
|
||||
|
Loading…
Reference in New Issue
Block a user