- Code: Select all
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std ;
#define EPS 1e-7
struct P { double x, y; P() {}; P( double q, double w ) : x( q ), y( w ) {} };
P operator - ( const P &p, const P &q ) { P u( p.x - q.x, p.y - q.y ); return u; }
bool operator !=( const P &p, const P &q ) { return fabs(p.x - q.x)>EPS || fabs(p.y - q.y)>EPS;}
vector<P> JarvisMarch ( vector<P> Q )
{
vector<P> S ;
P p, q ;
int i, n ;
n = Q.size( ) ;
p = Q[0] ;
for ( i=1; i<n; i++ )
if ( Q[i].y < p.y-EPS )
p = Q[i] ;
else
if ( fabs( Q[i].y - p.y ) < EPS )
if ( Q[i].x < p.x-EPS )
p = Q[i] ;
do
{
S.push_back ( p ) ;
p = Q[0] ;
q = S.back( ) ;
for ( i=1; i<n; i++ )
p = ( Q[i] - q ).x * ( p - q ).y - ( Q[i] - q ).y * ( p - q ).x > EPS ||
( fabs(( Q[i] - q ).x * ( p - q ).y - ( Q[i] - q ).y * ( p - q ).x) < EPS && ( Q[i] - q ).x * ( Q[i] - q ).x + ( Q[i] - q ).y * ( Q[i] - q ).y >
( p - q ).x * ( p - q ).x + ( p - q ).y * ( p - q ).y+EPS ) ? Q[i] : p ;
}
while ( p != S[0] ) ;
return S ;
}
vector<P> Q ;
void main() {
P p ;
int n,it,i,j;
double perimeter ;
it=1;
while ( scanf ( "%d", &n ),n ) {
Q.clear ( ) ;
for(i=0; i<n; i++) {
scanf("%lf%lf",&p.x,&p.y) ;
Q.push_back ( p ) ;
}
Q = JarvisMarch ( Q ) ;
n = Q.size ( ) ;
perimeter = 0.0 ;
for ( i=n-1,j=0; j<n; i=j++ )
perimeter += sqrt ( (Q[i].x-Q[j].x)*(Q[i].x-Q[j].x) + (Q[i].y-Q[j].y)*(Q[i].y-Q[j].y) ) ;
printf ( "Region #%d:\n(%.1lf,%.1lf)", it++,Q[0].x+EPS,Q[0].y+EPS ) ;
for ( i=n-1; i>=0; i-- ) printf ( "-(%.1lf,%.1lf)", Q[i].x+EPS, Q[i].y+EPS ) ;
putchar( '\n' ) ;
printf ( "Perimeter length = %.2lf\n\n", perimeter+EPS ) ;
}
}
