`
kennyhazel
  • 浏览: 21029 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

图像中心化后坐标转换_代码改进

阅读更多

以前的代码:

float changeCoorinateX(float x){	
	int tempX=(int)x;
	if ((tempX<W/2)){
	   x=x+float(W/2);
	   if(x>W/2){
	   x=x-W;
	   }
	}
	
	if ((tempX>=W/2)){
	x=x-(float)(W/2);
	}
	
	return x;		
}

float changeCoorinateY(float y){
			
	int tempY=(int)y;
	if ((tempY<H/2)){
	   y=y+float(H/2);
	   if(y>H/2){
	   y=y-H;
	   }
	}
	
	if ((tempY>=H/2)){
	  y=y-(float)(H/2);
	}
	
	return y;		
}

 

改进后的代码封装:

 

 

float changeCoorindate(float coorindate,bool isXY){
	int icoorindate=(int)coorindate;
	if(isXY){
		if(icoorindate<W/2){
		coorindate=coorindate+float(W/2);
			if(coorindate>W/2){
			coorindate=coorindate-W;
			}//end if
		}else{
		coorindate=coorindate-(float)(W/2);     
		}
	}else{
		if(coorindate<H/2){
		coorindate=coorindate+float(H/2);
			if(coorindate>H/2){
			coorindate=coorindate-H;
			}
		}else{
		coorindate=coorindate-(float)(H/2);
		}
	
	}

	return coorindate;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics