codice:
//Headers
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define UNDEFINED 0
#define DEF_X 1
#define DEF_Y 2
#define VERSION "v0.2.1\0"
using namespace std;
//Global Var
double GL_angle=0;
double GL_area=0;
int GL_Definition_switch=UNDEFINED;
int GL_Objects=1;
//Global Vectors
double* GL_cg;
double* GL_inertial_moment;
double* GL_central_inertial_moment;
double* GL_radius_ellipse;
//Declarations
double area();
int definition_central_rotation(double* _inertial_moment);
double* cg(double _area);
double* ellipse_radius(double* _central_inertial_moment, double _area);
double rotation_angle(double* _inertial_moment);
double* central_inertial_moment(double* _inertial_moment);
double* inertial_moment(double* __cg);
//Class Declarations
class CLASS_rectangle
{
private:
double* PR_vertex;
double PR_height;
double PR_base;
public:
double* PB_relative_inertial_moment;
double* PB_relative_cg;
double PB_area;
CLASS_rectangle(const CLASS_rectangle&);
void assign(double _vertex_x, double _vertex_y, double _base, double _height)
{
PR_vertex=(double*)malloc(2*sizeof(double));
PR_vertex[0]=_vertex_x;
PR_vertex[1]=_vertex_y;
PR_height=_height;
PR_base=_base;
PB_area=_base*_height;
PB_relative_cg=(double*)malloc(2*sizeof(double));
PB_relative_cg[0]=PR_vertex[0]+_base/2;
PB_relative_cg[1]=PR_vertex[1]+_height/2;
PB_relative_inertial_moment=(double*)malloc(3*sizeof(double));
PB_relative_inertial_moment[0]=pow(_height, 3.0)*_base*(1.0/12.0);
PB_relative_inertial_moment[1]=pow(_base, 3.0)*_height*(1.0/12.0);
PB_relative_inertial_moment[2]=0;
};
double* absolute_inertial_moment(double* _cg)
{
double* V_absolute_inertial_moment;
V_absolute_inertial_moment=(double*)malloc(3*sizeof(double));
V_absolute_inertial_moment[0]=(PB_relative_inertial_moment[0]+pow((PB_relative_cg[0]-_cg[0]),2.0)*PB_area);
V_absolute_inertial_moment[1]=(PB_relative_inertial_moment[1]+pow((PB_relative_cg[1]-_cg[1]),2.0)*PB_area);
V_absolute_inertial_moment[2]=(PB_relative_inertial_moment[2]+(PB_relative_cg[0]-_cg[0])*(PB_relative_cg[1]-_cg[1])*PB_area);
return V_absolute_inertial_moment;
};
~CLASS_rectangle()
{
free((void*)PB_relative_inertial_moment);
free((void*)PR_vertex);
free((void*)PB_relative_cg);
};
};
//Objects declarations
CLASS_rectangle* rectangle;
//Implementation
double area()
{
double temp_area=0.0;
int Counter=0;
while ( Counter<=(GL_Objects-1) )
{
temp_area=temp_area+rectangle[Counter].PB_area;
Counter+=1;
}
return temp_area;
};
int definition_central_rotation(double* _inertial_moment)
{
if (_inertial_moment[0]>=_inertial_moment[1])
return DEF_X;
else
return DEF_Y;
};
double* cg(double _area)
{
double* V_cg;
V_cg=(double*)malloc(2*sizeof(double));
int Counter=0;
while ( Counter <= (GL_Objects-1) )
{
V_cg[0]=V_cg[0]+(rectangle[Counter].PB_relative_cg[0]*rectangle[Counter].PB_area);
V_cg[1]=V_cg[1]+(rectangle[Counter].PB_relative_cg[1]*rectangle[Counter].PB_area);
Counter+=1;
};
V_cg[0]=V_cg[0]/_area;
V_cg[1]=V_cg[1]/_area;
return V_cg;
};
double* ellipse_radius(double* _central_inertial_moment, double _area)
{
double* V_ellipse_radius;
V_ellipse_radius=(double*)malloc(2*sizeof(double));
V_ellipse_radius[0]=sqrt(_central_inertial_moment[0]/_area);
V_ellipse_radius[1]=sqrt(_central_inertial_moment[1]/_area);
return V_ellipse_radius;
};
double rotation_angle(double* _inertial_moment)
{
if ( inertial_moment[2] != 0 )
return (double)(0.5*atan(((-2)*_inertial_moment[2])/(_inertial_moment[0]-_inertial_moment[1])));
else
return 0.0;
};
double* central_inertial_moment(double* _inertial_moment)
{
double* V_iner_cent_temp;
V_iner_cent_temp=(double*)malloc(2*sizeof(double));
if ( _inertial_moment[2] != 0 )
{
if ( _inertial_moment[0]>=_inertial_moment[1] )
{
V_iner_cent_temp[0]=_inertial_moment[0];
V_iner_cent_temp[1]=_inertial_moment[1];
return V_iner_cent_temp;
}
else
{
V_iner_cent_temp[1]=_inertial_moment[0];
V_iner_cent_temp[0]=_inertial_moment[1];
return V_iner_cent_temp;
}
}
else
{
V_iner_cent_temp[1]=(0.5*(_inertial_moment[0]+_inertial_moment[1])+sqrt(pow((0.5*fabs((_inertial_moment[0]-_inertial_moment[1]))),2.0)+pow(_inertial_moment[2],2.0)));
V_iner_cent_temp[2]=(0.5*(_inertial_moment[0]+_inertial_moment[1])-sqrt(pow((0.5*fabs((_inertial_moment[0]-_inertial_moment[1]))),2.0)+pow(_inertial_moment[2],2.0)));
return V_iner_cent_temp;
}
};
double* inertial_moment(double* __cg)
{
double* V_inertial_moment;
V_inertial_moment=(double*)malloc(3*sizeof(double));
int Counter=0;
while ( Counter <= (GL_Objects-1) )
{
double* V_temp_im;
V_temp_im=(double*)malloc(3*sizeof(double));
V_temp_im=rectangle[Counter].absolute_inertial_moment(__cg);
int i=0;
for (i=0; i<3; i++)
V_inertial_moment[i]=V_inertial_moment[i]+V_temp_im[i];
free((void*)V_temp_im);
Counter+=1;
}
return V_inertial_moment;
}
//Other implementations
//### MAIN ###
int main()
{
printf("Nirvana1289 2010\n----------------------------------------\nGEOMETRIA DELLE AREE %s \n----------------------------------------\n\n", VERSION);
//Obtain datas
int Counter=0;
do
{
printf("Inserire il numero di rettangoli che compongono la figura: "); scanf("%d", &GL_Objects);printf("\n");
}
while ( GL_Objects<=0 );
//Creating Objects
rectangle=(CLASS_rectangle*)malloc(GL_Objects*sizeof(CLASS_rectangle));
while ( Counter <= (GL_Objects-1) )
{
double temp_vertex_X, temp_vertex_Y, temp_base, temp_height;
temp_vertex_X=temp_vertex_Y=temp_base=temp_height=0.0;
printf("Rettangolo %d:\n", (Counter+1));
printf("Inserire posizione vertice inferiore sinistro (x,y): "); scanf("%f,%f",&temp_vertex_X, &temp_vertex_Y);
printf("Inserire base: "); scanf("%f", &temp_base);
printf("Inserire altezza: "); scanf("%f", &temp_height);
rectangle[Counter].assign(temp_vertex_X, temp_vertex_Y, temp_base, temp_height);
Counter+=1;
}
//Calculate
printf("\nCalcolo...\n");
printf("Espansione vettori...\n");
GL_cg=(double*)malloc(2*sizeof(double));
GL_inertial_moment=(double*)malloc(3*sizeof(double));
GL_central_inertial_moment=(double*)malloc(2*sizeof(double));
GL_radius_ellipse=(double*)malloc(2*sizeof(double));
printf("Definizione risultati...\n");
GL_area=area();
GL_cg=cg(GL_area);
GL_inertial_moment=inertial_moment(GL_cg);
GL_angle=rotation_angle(GL_inertial_moment);
GL_Definition_switch=definition_central_rotation(GL_inertial_moment);
GL_central_inertial_moment=central_inertial_moment(GL_inertial_moment);
GL_radius_ellipse=ellipse_radius(GL_central_inertial_moment, GL_area);
printf("... completato!\n");
//Printing results
printf("\n--- RISULTATI ---\n");
printf("\n1 - BARICENTRO\n\n");
printf("Oggetto \t Area\t Baricentro (relativo)\n\n");
Counter=0;
while ( Counter<=(GL_Objects-1) )
{
printf("Rettangolo %d\t%7.3f m^2\t%7.3f m\t%7.3f m\n", (Counter+1), rectangle[Counter].PB_area, rectangle[Counter].PB_relative_cg[0], rectangle[Counter].PB_relative_cg[1]);
Counter+=1;
}
printf("\nSistema \t%7.3f m^2\t%7.3f m\t%7.3f m\n", GL_area, GL_cg[0], GL_cg[1]);
printf("\n2 - MOMENTO DI INERZIA (relativo al G dei rettangoli)\n\n");
printf("Oggetto \t MomIn x'\t MomIn y'\t MomIn x'y'\n\n");
Counter=0;
while ( Counter<=(GL_Objects-1) )
{
printf("Rettangolo %d\t%7.3f m^4\t%7.3f m^4\t%7.3f m^4\n", (Counter+1), rectangle[Counter].PB_relative_inertial_moment[0], rectangle[Counter].PB_relative_inertial_moment[1], rectangle[Counter].PB_relative_inertial_moment[2]);
Counter+=1;
}
printf("\n3 - MOMENTO DI INERZIA (relativo al G del sistema)\n\n");
printf("Oggetto \t MomIn X\t MomIn Y\t MomIn XY\n\n");
printf("Sistema \t%7.3f m^4\t%7.3f m^4\t%7.3f m^4\n", GL_inertial_moment[0], GL_inertial_moment[1], GL_inertial_moment[2]);
printf("\n4 - MOMENTI CENTRALI DI INERZIA\n\n");
switch (GL_Definition_switch)
{
case UNDEFINED:
printf("ERRORE :(\n");
break;
case DEF_X:
printf("[PSI] -> rotazione di X\n[ETA] -> rotazione di Y\n");
break;
case DEF_Y:
printf("[PSI] -> rotazione di Y\n[ETA] -> rotazione di X\n");
break;
}
printf("\nAngolo di rotazione: %7.3f rad\n", GL_angle);
printf("\nMomenti centrali di inerzia:\nMomIn[PSI] = %7.3f m^4\nMomIn[ETA] = %7.3f m^4\n", GL_central_inertial_moment[0], GL_central_inertial_moment[1]);
printf("\n5 - RAGGI ELLISSE DI INERZIA\n\n");
printf("[RHO][PSI] = %7.3f m\n[RHO][ETA] = %7.3f m\n",GL_radius_ellipse[0],GL_radius_ellipse[1]);
printf("\n...DONE... \n");
return 0;
};