【发布时间】: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