poj 2540 Hotter Colder

本文介绍了一种解决半平面相交问题的算法,并通过C++代码实现了该算法。主要功能包括获取直线方程、计算交点坐标以及求解多边形面积等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道半平面相交的题目:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define LL long long
using namespace std;
class Point
{
public:
      double x,y;
      Point(){}
      Point( double xx , double yy ):x(xx),y(yy){}
}p[124],q[124];
void Init( Point &pre )
{
    pre = Point( 0 , 0 );
    p[1] = Point( 0 , 0 );
    p[2] = Point( 0 , 10 );
    p[3] = Point( 10 , 10 );
    p[4] = Point( 10 , 0 );
    p[5] = p[1];
    p[0] = p[4];
}
void Get_line( Point pre, Point cur , double &A, double &B, double &C)
{
    Point a = Point( (pre.x + cur.x)/2.0 , ( pre.y + cur.y )/2.0 );
    A = cur.x - pre.x;
    B = cur.y - pre.y;
    C = -A*a.x - B*a.y;    
}
Point Get_point( Point a,Point b, double A,double B,double C )
{
     double u = fabs( A*a.x + B*a.y + C );
    double v = fabs( A*b.x + B*b.y + C );
    Point ans;
    ans.x = ( u*b.x + v*a.x )/(u+v);
    ans.y = ( u*b.y + v*a.y )/(u+v);
    return ans;    
}
int Solve( int n ,double A,double B,double C)
{
    int m = 0;
       for( int i = 1 ; i <= n ; i ++ )
       {
       if( A*p[i].x + B*p[i].y + C > 0 )
           q[++m] = p[i];
       else
       {
           if( A*p[i-1].x + B*p[i-1].y + C > 0 )
               q[++m] = Get_point( p[i] , p[i-1] ,A,B,C );
           if(A*p[i+1].x + B*p[i+1].y + C > 0)
                  q[++m] = Get_point( p[i] , p[i+1] ,A,B,C );         
       }        
    }
    for( int i =1 ; i <= m; i ++ )
         p[i] = q[i];
    p[m+1] = p[1];
    p[0] = p[m];
    return m;
}
double Area( int n )
{
      double area=0;
      p[n+1] = p[1];
      for( int i = 1; i <= n ; i ++ )
           area += p[i].x*p[i+1].y - p[i].y*p[i+1].x;
      return fabs( area/2.0 );    
}
int main(  )
{
    Point pre,cur;
    bool flag = false;
    char str[12];
    double  A,B,C;
    int n = 4;
    Init( pre );
      while( scanf( "%lf%lf%s",&cur.x,&cur.y,str )==3 )
      {
         if( !flag )
         {
             Get_line( pre , cur , A,B,C );
             if( str[0] == 'C' ) 
             {
                 if( A*pre.x + B*pre.y + C < 0 )
                 {
                     A = -A; B = -B ; C = -C; 
                 }             
             }
             else if( str[0] == 'H' )
                  {
                      if( A*cur.x + B*cur.y + C < 0 )
                      {
                         A = -A ; B = -B ; C = -C;        
                      }
                  }
              else {flag = true;puts( "0.00" );continue;}
                n =  Solve( n ,A ,B ,C );    
             printf("%.2f\n",Area( n ) );
             pre = cur;    
         }
         else puts( "0.00" );          
    }
    //system( "pause" );
    return 0;
}

 

转载于:https://www.cnblogs.com/bo-tao/archive/2012/09/14/2685782.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值