Commit 8510b998 authored by santiaago's avatar santiaago
Browse files

fix hexagon error

parent 8db44cd6
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package isogrids

import (
	"image/color"
	"log"
	"math"
	"net/http"

@@ -15,11 +16,15 @@ func Hexa(w http.ResponseWriter, key string, colors []color.RGBA, size, lines in
	canvas.Start(size, size)

	fringeSize := size / lines
	log.Println(fringeSize)
	// distance of center of vector to third point of equilateral triangles
	// ABC triangle, O is the center of AB vector
	// OC = SQRT(AC^2 - AO^2)
	distance := int(math.Ceil(math.Sqrt((float64(fringeSize) * float64(fringeSize)) - (float64(fringeSize)/float64(2))*(float64(fringeSize)/float64(2)))))
	fringeSize = distance
	lines = size / fringeSize
	offset := size - fringeSize*lines
	log.Println(offset)
	for xL := 0; xL < lines/2; xL++ {
		for yL := 0; yL < lines; yL++ {

@@ -54,35 +59,35 @@ func Hexa(w http.ResponseWriter, key string, colors []color.RGBA, size, lines in
			}
			xs := []int{x1, x2, x3}
			ys := []int{y1, y2, y3}

			if lines%4 != 0 {
				xs[0] = x2
				xs[1] = x1
				xs[2] = x2
			}

			canvas.Polygon(xs, ys, fill1)

			xs[0] = (lines * fringeSize) - xs[0]
			xs[1] = (lines * fringeSize) - xs[1]
			xs[2] = (lines * fringeSize) - xs[2]
			canvas.Polygon(xs, ys, fill1)
			xsMirror := []int{0, 0, 0}
			xsMirror[0] = (lines * fringeSize) - xs[0]
			xsMirror[1] = (lines * fringeSize) - xs[1]
			xsMirror[2] = (lines * fringeSize) - xs[2]

			canvas.Polygon(xsMirror, ys, fill1)

			var x11, x12, x13, y11, y12, y13 int
			if (xL % 2) == 0 {
				x11 = xL*fringeSize + distance
				x12 = (xL) * fringeSize
				x12 = xL * fringeSize
				x13 = x11
				y11 = yL*fringeSize + fringeSize/2
				y12 = y11 + fringeSize/2
				y13 = yL*fringeSize + (fringeSize / 2) + distance
				y12 = y3 // to have a perfect hexagon, we make sure that previews triangle and this one touch each other in this point
				y13 = yL*fringeSize + fringeSize/2 + distance
			} else {
				x11 = xL * fringeSize
				x12 = xL*fringeSize + distance
				x13 = x11
				y11 = yL*fringeSize + fringeSize/2
				y12 = y1 + fringeSize
				y13 = yL*fringeSize + (fringeSize / 2) + distance
				y13 = yL*fringeSize + fringeSize/2 + distance
			}
			xs1 := []int{x11, x12, x13}
			ys1 := []int{y11, y12, y13}
@@ -96,6 +101,7 @@ func Hexa(w http.ResponseWriter, key string, colors []color.RGBA, size, lines in
			xs1[0] = (lines * fringeSize) - xs1[0]
			xs1[1] = (lines * fringeSize) - xs1[1]
			xs1[2] = (lines * fringeSize) - xs1[2]

			canvas.Polygon(xs1, ys1, fill2)
		}
	}