以前的代码:
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;
}