六边形格子地图坐标计算与转换

本文详细探讨了如何在JavaScript中进行六边形格子地图的坐标计算与转换。从基础的六边形网格布局到复杂的坐标转换算法,内容涵盖了数学原理在地图坐标处理中的应用,适合对地图编程和六边形网格感兴趣的开发者阅读。

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

// 世界场景的一些数据管理,如提供坐标转换之类的接口

var WorldMapManager = function () {
    this.mapSize = null;     // 地图大小,像素
    this.curViewPos = null;  // 当前大地图视野坐标

    // 初始化世界地图的数据
    this.init = function (mapSize, tileSize) {
        this.mapSize = {
                width  : globalConsts.WorldMapSize.width * globalConsts.TileSize.width + globalConsts.TileSize.width / 2,
                height : globalConsts.WorldMapSize.height * ((globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2 + globalConsts.TileSize.hex) + 
                            (globalConsts.TileSize.height - globalConsts.TileSize.hex) / 2
            };
        this.tileSize = globalConsts.TileSize;
    };

    // 大地图坐标转成蜂窝cell
    this.mapPosToTile = function (pos) {
        // 算出缩放成正六边形后边长 a 的值
        var a = this.tileSize.width / Math.sqrt(3);
        var x = pos.x, y = (this.mapSize.height - pos.y) / this.tileSize.height * a * 2 + a / 2;    // 加 a / 2 是因为矩形网格计算时会在底部增加 a / 2
        
        //位于矩形网格边线上的三个CELL中心点
        var points = new Array(cc.p(0, 0), cc.p(0, 0), cc.p(0, 0));
        //当前距离的平方
        var dist;
        //      index:被捕获的索引
        var i, index;
        //二分之根号3 边长的平方,如果距离比它还小,就必然捕获
        var g_MinDistance2 = Math.pow(a * Math.sqrt(3) / 2, 2);
        // 网格宽、高
        var g_unitx = a * Math.sqrt(3);     //sq
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值