I need some help to optimize my code. Even though it works I have the strong feeling, this can be made in WAY less lines of code and much more efficient. But I can't figure it out myself.
Actually it doesn't even work perfectly yet.
I have a marker at a center. I want this center to be the center of a hexagonal grid of polygons on top of Google Maps.
In the Apps Setting the user can set the number of Hexagons he wants to have in north, east, west and south direction. So what I want to achieve is to draw all these Hexagons starting from my center of the HEX Grid.
Here you find my code which is giving me something but not exactly the right thing.
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)
}
As you can see, I have the drawHexagon function. As well as some Looping functions. Going south you have to iterate between going half width to the left or to the right. It's not as trivial as it looks ;)
Thanks for all your help!