# 45度角Tile地图通过具体位置获得地图的行列数详解

//45度角地图通过具体位置获得地图的行列数
CCPoint TileMapMove::convertto2d(float x,float y)
{
CCTMXTiledMap *tileMap=(CCTMXTiledMap*)this->getChildByTag(kTagTileMap);
//获取地图的宽高
int mapWidth=tileMap->getMapSize().width*tileMap->getTileSize().width;
int mapHeight=tileMap->getMapSize().height*tileMap->getTileSize().height;

double distanse,sin1,sin11,sin22,cos1;
//d2x,d2y保存传入点在地图上的行列值
int d2x,d2y;

//菱形棱长比例系数（因为对角线之比是2:1，所以根据三角形定理，斜边比是sqrt（5.0）
double mystatic5=sqrt(5.0);
//所以地图格的棱长为（高的一半*棱长比例系数）
double mystatic=16*mystatic5;

if(x>mapWidth/2)
{
distanse=sqrt((x-mapWidth/2)*(x-mapWidth/2)+(mapHeight-y)*(mapHeight-y));

sin1=(mapHeight-y)/distanse;
cos1=(x-mapWidth/2)/distanse;

sin11=(sin1*2-cos1)/mystatic5;
d2y=distanse*5/4*sin11/mystatic;

sin22=(2*sin1+cos1)/mystatic5;
d2x=distanse*5/4*sin22/mystatic;
return ccp(d2x,d2y);
}
else
{
//AC的长度
distanse=sqrt((mapWidth/2-x)*(mapWidth/2-x)+(mapHeight-y)*(mapHeight-y));
//1 = 角ACE
sin1=(mapHeight-y)/distanse;
cos1=(mapWidth/2-x)/distanse;
//11 = 角ACD
sin11=(sin1*2-cos1)/mystatic5;
//sinADC = sin 2*CFO = 4/5
d2x=distanse*5/4*sin11/mystatic;