Creator3.x 绘制六边形

 绘制六边形


    /** 绘制六边形集合并平铺 */
    private initSexAngles() {
        let _node = new Node();
        _node.parent = find("bg", this.node);
        _node.layer = Layers.Enum.UI_2D;
        _node.position = Vec3.ZERO;
        G.tool.getOrAddCompent(_node, UITransform).contentSize = new Size(600, 600);
        // 添加绘制组件
        let _graphics = G.tool.getOrAddCompent(_node, Graphics) as Graphics;
        _graphics.lineWidth = 2;
        _graphics.strokeColor = new Color(255, 255, 0);
        _graphics.fillColor = new Color(255, 255, 0);
        _graphics.lineJoin = 2;
        _graphics.lineCap = 0;

        let _startPos = new Vec3(-300, -300);
        let radius = 50;
        /** 中心点到线段的垂直距离 */
        let halfDis = Math.sqrt(radius * radius - 1 / 2 * radius * 1 / 2 * radius);
        for (let i = 0; i < 10; i++) {
            for (let j = 0; j < 10; j++) {
                let _pos = new Vec3(_startPos.x  +  (1 / 2 + 1) * radius * i, _startPos.y + 2 * halfDis * j + (i % 2 ? halfDis : 0));
                this.calcEachSexAngle(_graphics, _pos, 50);
            }
        }
        _graphics.stroke();
    }

    /** 画一个六角形 */
    private calcEachSexAngle(_graphics: Graphics, pos: Vec3 = Vec3.ZERO, radius: number = 50) {
        let angle = 2 * Math.PI;
        let eachAngle = angle / 6;
        /** 中心点到线段的垂直距离 */
        let halfDis = Math.sqrt(radius * radius - 1 / 2 * radius * 1 / 2 * radius);
        // 左上角第一个点;
        _graphics.moveTo(-1 / 2 * radius + pos.x, halfDis + pos.y);
        // 左角
        _graphics.lineTo(-radius + pos.x, pos.y);
        // 左下角
        _graphics.lineTo(-1 / 2 * radius + pos.x, -halfDis + pos.y);
        // 右下角
        _graphics.lineTo(1 / 2 * radius + pos.x, -halfDis + pos.y);
        // 右角
        _graphics.lineTo(radius + pos.x, pos.y);
        // 右上角
        _graphics.lineTo(1 / 2 * radius + pos.x, halfDis + pos.y);
        // 闭合角度
        _graphics.close();
        // 绘制当前或已经存在的路径
    }

效果如下图 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值