【问题标题】:Kotlin - Hexagonal Grip Google Maps API v3 - Optimize CodeKotlin - Hexagonal Grip Google Maps API v3 - 优化代码
【发布时间】:2019-07-14 18:38:05
【问题描述】:

我需要一些帮助来优化我的代码。 尽管它有效,但我有强烈的感觉,这可以用更少的代码行来实现,而且效率更高。 但我自己想不通。

实际上它甚至还不能完美地工作。

我在中心有一个标记。 我希望这个中心成为谷歌地图顶部的六边形多边形网格的中心。

在应用程序设置中,用户可以设置他想要在北、东、西和南方向拥有的六边形数量。 所以我想要实现的是从我的十六进制网格中心开始绘制所有这些六边形。

在这里你可以找到我的代码,它给了我一些东西,但并不完全正确。

private fun drawHexagonGrid(){
        var radius = 3.5  //radius in metre
        var curPos = heartAlveole  //hierdann Heart Alveole aus den Prefs nehmen
        var northAlveoles = 5  //Das alles aus den Prefs holen.
        var eastAlveoles = 10
        var southAlveoles = 5
        var westAlveoles = 5
        val width = radius.toDouble() * 2.0 * Math.sqrt(3.0) / 2


        //North East of Heart

        curPos = heartAlveole
        for (n in 0..northAlveoles){
            //East Alveoles
            for (i in 0..eastAlveoles) {
                drawHorizontalHexagon(curPos, radius, ".")
                curPos = SphericalUtil.computeOffset(curPos, width,90.0);
            }
            curPos = SphericalUtil.computeOffset(curPos, width*eastAlveoles,270.0)
            //Every Second Iteration go half width west or east
            if (n % 2 == 0) {
                curPos = SphericalUtil.computeOffset(curPos, width/2,270.0)
            }
            else {
                curPos = SphericalUtil.computeOffset(curPos, width/2,90.0)
            }
            //go north
            curPos = SphericalUtil.computeOffset(curPos,5.25 ,0.0)

        }

        //North West of Heart

        curPos = heartAlveole
        for (n in 0..northAlveoles){
            //East Alveoles
            for (i in 0..westAlveoles) {
                drawHorizontalHexagon(curPos, radius, ".")
                curPos = SphericalUtil.computeOffset(curPos, width,270.0);
            }
            curPos = SphericalUtil.computeOffset(curPos, width*westAlveoles,90.0)

            //Every Second Iteration go half width west or east
            if (n % 2 == 0) {
                curPos = SphericalUtil.computeOffset(curPos, width/2,270.0)
            }
            else {
                curPos = SphericalUtil.computeOffset(curPos, width/2,90.0)
            }
            //go north
            curPos = SphericalUtil.computeOffset(curPos,5.25 ,0.0)

        }

        //South East of Heart

        curPos= heartAlveole
        for (n in 0..southAlveoles){
            //Every Second Iteration go half width west or east
            if (n % 2 == 0) {
                curPos = SphericalUtil.computeOffset(curPos, width/2,270.0)
            }
            else {
                curPos = SphericalUtil.computeOffset(curPos, width/2,90.0)
            }
            //go south
            curPos = SphericalUtil.computeOffset(curPos,5.25 ,180.0)
            //East Alveoles
            for (i in 0..eastAlveoles) {
                drawHorizontalHexagon(curPos, radius, ".")
                curPos = SphericalUtil.computeOffset(curPos, width,90.0);
            }

            curPos = SphericalUtil.computeOffset(curPos, width*eastAlveoles,270.0)


        }

        //South West of Heart

        curPos= heartAlveole
        for (n in 0..southAlveoles){
            //Every Second Iteration go half width west or east
            if (n % 2 == 0) {
                curPos = SphericalUtil.computeOffset(curPos, width/2,270.0)
            }
            else {
                curPos = SphericalUtil.computeOffset(curPos, width/2,90.0)
            }
            //go south
            curPos = SphericalUtil.computeOffset(curPos,5.25 ,180.0)
            //West Alveoles
            for (i in 0..westAlveoles) {
                drawHorizontalHexagon(curPos, radius, ".")
                curPos = SphericalUtil.computeOffset(curPos, width,270.0);
            }

            curPos = SphericalUtil.computeOffset(curPos, width*westAlveoles,90.0)

        }



    }

    private fun drawHorizontalHexagon(position : LatLng, radius : Double, label : String){
        var coordinates : MutableList<LatLng> = arrayListOf()

        for (angle in 0..360 step 60) {
            coordinates.add(SphericalUtil.computeOffset(position,radius,angle.toDouble()))
        }

        var opts : PolygonOptions = PolygonOptions().addAll(coordinates)
            .fillColor(Color.argb(35,255, 0,0))
            .strokeColor(Color.RED).strokeWidth(3f)

        mMap.addPolygon(opts)


        //Funktioniert theoretisch. Noch überlegen ob ich es wirklich brauche.
        //Müsste noch das Transparent ändern und die Größe der Schrift anpassen.
        //this.showText(position, label)

    }

如你所见,我有 drawHexagon 函数。以及一些循环功能。 向南走,您必须在向左或向右半宽度之间进行迭代。 它并不像看起来那么微不足道;)

感谢您的帮助!

【问题讨论】:

    标签: kotlin android-maps hexagonal-tiles


    【解决方案1】:

    我自己找到了答案。 如果有人有同样的问题,我会把它贴在这里存档。

    private fun drawHexagonGrid(){
        var radius = 3.5  //radius in metre
        var curPos = heartAlveole  //hierdann Heart Alveole aus den Prefs nehmen
        var northAlveoles = 5  //Das alles aus den Prefs holen.
        var eastAlveoles = 10
        var southAlveoles = 5
        var westAlveoles = 5
        val width = radius.toDouble() * 2.0 * Math.sqrt(3.0) / 2
    
    
        //Move to NorthWest Corner of HexGrid
    
        val westMove = if (northAlveoles % 2 == 0) width*westAlveoles else (width*westAlveoles)-(width/2)
    
        //If northAlveoles is unequal, add half width to westMove to
    
        curPos = SphericalUtil.computeOffset(curPos,westMove,270.0)
        curPos = SphericalUtil.computeOffset(curPos,radius * 6/4*northAlveoles,0.0)
    
        //nested for loop to produce hexagonal grid
    
        for (n in 1..northAlveoles+southAlveoles+1){
            //draw horizontal hexagons
            for (e in 1..eastAlveoles+westAlveoles+1) {
                drawHorizontalHexagon(curPos, radius, ".")
                curPos = SphericalUtil.computeOffset(curPos, width,90.0);
            }
    
            //Go back to the initial position + width/2 or - width/2 every second iteration
            if (n % 2 == 0) {
                curPos = SphericalUtil.computeOffset(curPos, width*((westAlveoles+eastAlveoles)+0.5),270.0)
            } else {
                curPos = SphericalUtil.computeOffset(curPos, width*((westAlveoles+eastAlveoles)+1.5),270.0)
            }
            //go south
            curPos = SphericalUtil.computeOffset(curPos,radius * 6/4 ,180.0)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2013-12-18
      • 2015-07-24
      相关资源
      最近更新 更多