#include <stdio.h>
#include <math.h>
struct POINT{
double x;
double y; };
typedef struct POINT Point;
#define PI 3.14
double dist( Point p1, Point p2 );
void GetBorder( int, Point* AllTree, double *, double *, double *, double * );
int InnerPoint( Point , double, double, double, double, double );
int InnerPoint( Point p1, double Circle, double Left, double Right, double Top, double Bottom )
{
double r;
r = Circle / 2;
if( fabs(p1.x - Left) <= r || fabs(p1.x - Right) <= r ||
fabs(p1.y - Top) <= r || fabs(p1.y - Bottom) <= r )
{
return 0;//boundary point
}
return 1;//interior point
}
double dist( Point p1, Point p2 )
{
double distance;
float f;
f = (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y);
distance = sqrt( f );
return distance;
}
void GetBorder( int Num, Point *AllTree, double *Left, double *Right, double *Top, double *Bottom )
{
int i;
double minLeft, maxRight, maxTop, minBottom;
minLeft = 1000;
maxRight = -1000;
maxTop = -1000;
minBottom = 1000;
for( i = 0; i < Num; i++ )
{
if( AllTree[i].x <= minLeft )
{
minLeft = AllTree[i].x;
}
if( AllTree[i].x >= maxRight )
{
maxRight = AllTree[i].x;
}
if( AllTree[i].y <= minBottom )
{
minBottom = AllTree[i].y;
}
if( AllTree[i].y >= maxTop )
{
maxTop = AllTree[i].y;
}
}
*Left = minLeft;
*Right = maxRight;
*Top = maxTop;
*Bottom = minBottom;
return;
}
int main( )
{
FILE *file;
FILE * file1;
int num;
double *pCircle;
double *pSize;
Point *pTree;
int *pGrade;
int i;
int j;
double Left, Right, Top, Bottom;
double *pPressure, r, r2, distance,temp;
file = fopen( "//root//Project//ganranyali//yali//sample5.txt", "r" );
file1 = fopen( "//root//Project//ganranyali//yali//pressure5.txt", "w" );
if( NULL == file )
{
printf( "fail to open file!\n" );
return 0;
}
fscanf( file, "%d\n", &num );
pCircle = (double*)malloc( num * sizeof(double) );
pSize = (double*)malloc( num * sizeof(double) );
pTree = (Point*)malloc( num*sizeof(Point) );
pGrade = (int*)malloc( num*sizeof(int) );
pPressure = (double*)malloc( num*sizeof(double) );
for( i = 0; i < num; i++ )
{
fscanf( file, "%lf ", &temp );
pCircle[i] = temp;
fscanf( file, "%lf ", &temp );
pSize[i] = temp;
fscanf( file, "%lf ", &temp );
pTree[i].x = temp ;
fscanf( file, "%lf ", &temp);
pTree[i].y = temp;
fscanf( file, "%d\n", &pGrade[i] );
// pGrade[i] = temp;
}
fclose( file );
GetBorder( num, pTree, &Left, &Right, &Top, &Bottom );
printf( "Left = %f\n Right = %f\n Top = %f\n Bottom = %f\n", Left, Right, Top, Bottom );
for( i = 0; i < num; i++ )
{
pPressure[i] = -1;
}
for( i = 0; i < num; i++ )
{
if( i == 13 )
{
j = 0;
}
r = pCircle[i] / 2;
if( InnerPoint( pTree[i], pCircle[i], Left, Right, Top, Bottom ) )
{
pPressure[i] = 0;
for( j = 0; j < num; j++ )
{
if( j != i )
{
distance = dist(pTree[i], pTree[j]);
r2 = pCircle[j] / 2;
if( distance <= 0.000001 )
{
distance = 0.1;
}
if( distance <= (r + r2) )
{
if( i == 23 & pGrade[j] != 0 )
printf( "%d\n", j );
pPressure[i] += (pSize[i]*pSize[j]*pGrade[j]) / ( 10000 * PI * PI *distance );
}
}
}
}
}
//print result of pressure
if( NULL == file1 )
{
printf( "fail to open output file\n" );
return 0;
}
for( i = 0; i < num; i++ )
{
fprintf( file1, "%d\t", i+1 );
fprintf( file1, "%f\n", pPressure[i] );
}
fclose( file1 );
}