中点分割算法

本文介绍了一种名为中点分割的算法,该算法用于确定线段与矩形的关系,并找到线段两端点最近且位于矩形内的点。通过递归方式不断缩小搜索范围,最终找出符合条件的点。

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

 今天上计算机图形学的时候,讲了个分割算法,觉得很有意思 ,回来实现之,环境(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"cstdio"
#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 ;
 
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值