今天上计算机图形学的时候,讲了个分割算法,觉得很有意思 ,回来实现之,环境(vitual c++ 6.0)
//中点分割算法.cpp
/******要点说明******************************************************
x,y的范围,xMin<= x <=xMax ; yMin<=y<=yMax
给定某点(x,y)
bool flagTop: 当y > yMax的时候flagTop = true ,else flagTop = false
bool flagBottom 当y < yMin的时候 flagBottom = true ,else flagBottom = flase
bool flagRight 当x > xMax的时候 flagRight = true ,else flagRight = flase
bool flagLeft 当x < xMin的时候 flagLeft = true ,else flagLeft = flase
算法要点:给定线段的俩个端点,首先找到明显在矩形内的线段或者矩形外的的线段,然后找到离线段俩端点最近
并且在矩形内的点
*********************************************************************/
#include"cstdlib"
#include"iostream"
#include"cmath"
using namespace std ;
#define OVER 10e-6
double xMax ,xMin ,yMax ,yMin ;
//按照上,下,右,左的顺序编码,得到编码的值返回
int getTheCodeValue ( double x , double y)
{
int direct[4] ,codeValue = 0 ;
if ( y > yMax ) //点在上边
{
direct[0] = 1 ;
direct[1] = 0 ;
}
else if ( y < yMin )
{
direct[0] = 0 ;
direct[1] = 1 ;
}
else
{
direct[0] = 0 ;
direct[1] = 0 ;
}
if ( x > xMax) //点在矩形的边
{
direct[2] = 1 ;
direct[3] = 0 ;
}
else if ( x < xMin)
{
direct[2] = 0 ;
direct[3] = 1 ;
}
else
{
direct[2] = 0 ;
direct[3] = 0 ;
}
for ( int i = 0 ; i < 4 ; i ++ )
{
codeValue += direct[i]*pow(2,3-i) ;
}
return codeValue ;
}
bool judgeInRec( int codeValueOne ,int codeValueTwo) // 判断这条线段是不是明显的在矩形内
{
if (codeValueOne ==0 &&codeValueTwo == 0 )
{
return true ;
}
else
{
return false ;
}
}
bool judgeOutRec ( int codeValueOne ,int codeValueTwo) //判断这条线段明显的在矩形外
{
if ( codeValueOne & codeValueTwo)
{
return true ;
}
else
{
return false ;
}
}
void findPoint ( double lowx ,double lowy ,double highx ,double highy)
{
double middlex = ( lowx + highx)/2.0 ;
double middley = ( lowy + highy)/2.0 ;
int valueCodeOne = getTheCodeValue ( lowx ,lowy) ;
int valueCodeTwo = getTheCodeValue(highx ,highy) ;
if ( judgeOutRec ( valueCodeOne ,valueCodeTwo )) //线段明显在矩形外
{
return ;
}
else
{
if ( judgeInRec ( valueCodeOne ,valueCodeTwo) )
{
return ;
}
else
{
if ( (pow(middlex,2)+pow(middley,2)) - (pow(lowx,2) + pow(lowy,2)) < OVER )
{
printf("(%g,%g) ",middlex ,middley);
return ;
}
else
{
findPoint(lowx, lowy ,middlex ,middley );
findPoint(middlex ,middley,highx ,highy) ;
}
}
}
}
int main()
{
double xOne, yOne,xTwo ,yTwo ;
int valueOne ,valueTwo ;
printf("please input the loction of the rec ");
printf("low of loction x :") ;
scanf("%lf",&xMin) ;
printf("low of loction y :") ;
scanf("%lf",&yMin) ;
printf("high of loction x :") ;
scanf("%lf",&xMax) ;
printf("high of loction y:") ;
scanf("%lf",&yMax) ;
printf("please input the two point of the line ");
printf("lineone of loction x :") ;
scanf("%lf",&xOne) ;
printf("lineone of loction y :") ;
scanf("%lf",&yOne) ;
printf("lineTwo of loction x :") ;
scanf("%lf",&xTwo) ;
printf("linetwo of loction y:") ;
scanf("%lf",&yTwo) ;
valueOne = getTheCodeValue ( xOne ,yOne) ;
valueTwo = getTheCodeValue( xTwo ,yTwo) ;
if ( judgeInRec ( valueOne ,valueTwo) )
{
printf("the line is in the rectangle ");
}
else if ( judgeOutRec ( valueOne ,valueTwo))
{
printf("the line is not in the rectangle ");
}
else
{
printf("found the nearest point:");
findPoint(xOne ,yOne ,xTwo ,yTwo);
printf(" ");
}
return 0 ;
}