1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <math.h>
// math.h pour fabs, cos sin et M_PI ( 3.14...) de l'exemple
//---------------------------------------------------------------------------
float area( int Npts, float X[], float Y[])
{
float *DX = new float[Npts];
float *DY = new float[Npts];
float A = 0;
for ( int i=0; i < Npts; i++)
{
DX[i] = (X[(i+1)%Npts] - X[(i+Npts-1)%Npts])/2;
DY[i] = (Y[(i+1)%Npts] - Y[(i+Npts-1)%Npts])/2;
}
for ( int i=0; i < Npts; i++)
A = A + (X[i]*DY[i] - Y[i]*DX[i]);
delete [] DX;
delete [] DY;
return ( fabs(A/2) );
}
// exemple : calcul de l'aire d'1 cercle
float Surface_d_1_cercle(void)
{
//rayon 10
#define Npts 200
#define Ray 10
float U = 2 * M_PI / Npts;
float X[Npts], Y[Npts];
for( short i=0; i < Npts ; i++)
{
X[i] = Ray * cos( i * U );
Y[i] = Ray * sin(i * U );
}
return area(Npts,X,Y); // 314.107.. au lieu de 314.1592...
} |
Partager