This repository has been archived on 2023-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
GINPA/Modele/agglo.c

75 lines
1.4 KiB
C

#include "agglo.h"
ErrEnum initAgglo(Agglomerat* a,Point* pts, uint32_t taille)
{
a->agglo=calloc(taille, sizeof(Point*));
if (a->agglo==NULL)
{
fprintf(stderr, "%s\n", strerror(errno));
return ALLOCATION_FAILURE;
}
a->nbPts=taille;
a->moy.lat=0;
a->moy.lon=0;
for (uint32_t i=0; i<taille;i++)
{
a->agglo[i]=&pts[i];
a->moy.lat+=pts[i].pos.lat;
a->moy.lon+=pts[i].pos.lon;
}
a->moy.lat/=a->nbPts;
a->moy.lon/=a->nbPts;
return SUCCESS;
}
void libereAgglo(Agglomerat *const a)
{
free(a->agglo);
a->agglo=NULL;
}
Agglomerat fusionAgglo(Agglomerat const* const a1, Agglomerat const* const a2)
{
if (a1->agglo[0]->date > a2->agglo[0]->date)
return fusionAgglo(a2,a1);
Agglomerat retour;
retour.agglo=calloc(a1->nbPts+a2->nbPts, sizeof(Point*));
if (retour.agglo==NULL)
{
fprintf(stderr, "%s\n", strerror(errno));
return (Agglomerat){ {0, 0}, NULL, 0};
}
retour.nbPts=a1->nbPts+a2->nbPts;
retour.moy.lat=(a1->moy.lat+a2->moy.lat)/2;
retour.moy.lon=(a1->moy.lon+a2->moy.lon)/2;
for(uint32_t i=0; i<a1->nbPts;i++)
{
retour.agglo[i]=a1->agglo[i];
}
for(uint32_t i=0; i<a2->nbPts;i++)
{
retour.agglo[i+a1->nbPts]=a2->agglo[i];
}
return retour;
}
uint32_t ecartTemps(Agglomerat const* const a1, Agglomerat const* const a2)
{
if (a1->agglo[0]->date > a2->agglo[0]->date)
return ecartTemps(a2,a1);
return a2->agglo[0]->date - a1->agglo[a1->nbPts -1]->date;
}