我需要一些帮助才能优化我的代码。即使它起作用,我有强烈的感觉,但它可以用更少的代码行和更有效地制作。但是我自己无法弄清楚。
实际上它甚至还没有完美。
我在中心有一个标记。我希望这个中心成为Google Maps上的多边形六角形网格的中心。
在设置用户的应用程序中,用户可以设置他想在北,东,西方和南方的六角形数量。因此,我想实现的是从我的十六进制网格中心开始绘制所有这些六角形。
在这里,您找到了我的代码,这给了我一些东西,但不是完全正确的。
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函数。以及一些循环功能。向南行驶,您必须在向左或向右的半宽度之间进行迭代。它的外观不那么微不足道;)
感谢您的所有帮助!
我自己找到了答案。如果某人有同样的问题,我将在此处将其发布在此处。
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)
}
}