Commit ad1e090f authored by santiaago's avatar santiaago
Browse files

add isogrids/hexa/:key isogrid hexagon with no borders

parent 8bf2fac1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ func main() {

	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)

	r.HandleFunc("/isogrids/labs/hexa", isogrids.Hexa)
	r.HandleFunc("/isogrids/labs/hexa/:key", isogrids.Hexa)
	r.HandleFunc("/isogrids/labs/skeleton", isogrids.Skeleton)
	r.HandleFunc("/isogrids/labs/diagonals", isogrids.Diagonals)
	r.HandleFunc("/isogrids/labs/halfdiagonals", isogrids.HalfDiagonals)
+12 −5
Original line number Diff line number Diff line
@@ -5,27 +5,34 @@ import (
	"fmt"
	"image/color"
	"io"
	"log"
	"net/http"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
)

// Hexa is the handler for /isogrids/hexa
// Hexa is the handler for /isogrids/hexa/:key
// builds an hexagon from a 10x10 grid with alternate colors.
func Hexa(w http.ResponseWriter, r *http.Request) {
	var err error
	colorMap := colors.MapOfColorThemes()
	size := extract.Size(r)

	var key string
	if key, err = route.Context.Get(r, "key"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		key = ""
	}
	h := md5.New()
	io.WriteString(h, "hello")
	key := fmt.Sprintf("%x", h.Sum(nil)[:])
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	theme := "frogideas" //extract.Theme(r)
	numColors := 4       //extract.NumColors(r)
	theme := extract.Theme(r)
	numColors := extract.NumColors(r)

	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
+123 −0
Original line number Diff line number Diff line
@@ -192,3 +192,126 @@ func Isogrids(w http.ResponseWriter, key string, colors []color.RGBA, size int)
	}
	canvas.End()
}

// IsogridsHexa builds an image with 10x10 grids of half diagonals
func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size int) {
	canvas := svg.New(w)
	canvas.Start(size, size)

	lines := 8
	fringeSize := size / lines

	// triangle grid here:
	for xL := -1; xL < lines/2; xL++ {
		for yL := -1; yL <= lines; yL++ {

			fill1 := fillWhite()
			fill2 := fillWhite()

			if isFill1InHexagon(xL, yL) {
				fill1 = fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+lines)%15))
			}
			if isFill2InHexagon(xL, yL) {
				fill2 = fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+1+lines)%15))
			}

			if !isFill1InHexagon(xL, yL) && !isFill2InHexagon(xL, yL) {
				continue
			}

			var x1, x2, x3, y1, y2, y3 int
			if (xL % 2) == 0 {
				x1 = (xL) * fringeSize
				x2 = (xL + 1) * fringeSize
				x3 = x1
				y1 = yL * fringeSize
				y2 = y1 + fringeSize/2
				y3 = (yL + 1) * fringeSize
			} else {
				x1 = (xL + 1) * fringeSize
				x2 = xL * fringeSize
				x3 = x1
				y1 = yL * fringeSize
				y2 = y1 + fringeSize/2
				y3 = (yL + 1) * fringeSize
			}
			xs := []int{x1, x2, x3}
			ys := []int{y1, y2, y3}
			canvas.Polygon(xs, ys, fill1)

			// apply mirror:
			xs[0] = (lines * fringeSize) - xs[0]
			xs[1] = (lines * fringeSize) - xs[1]
			xs[2] = (lines * fringeSize) - xs[2]
			canvas.Polygon(xs, ys, fill1)

			var x11, x12, x13, y11, y12, y13 int
			if (xL % 2) == 0 {
				x11 = (xL + 1) * fringeSize
				x12 = (xL) * fringeSize
				x13 = x11
				y11 = yL*fringeSize + fringeSize/2
				y12 = y11 + fringeSize/2
				y13 = (yL+1)*fringeSize + fringeSize/2
			} else {
				x11 = (xL) * fringeSize
				x12 = (xL + 1) * fringeSize
				x13 = x11
				y11 = yL*fringeSize + fringeSize/2
				y12 = y1 + fringeSize
				y13 = (yL+1)*fringeSize + fringeSize/2
			}
			xs1 := []int{x11, x12, x13}
			ys1 := []int{y11, y12, y13}
			canvas.Polygon(xs1, ys1, fill2)
			xs1[0] = (lines * fringeSize) - xs1[0]
			xs1[1] = (lines * fringeSize) - xs1[1]
			xs1[2] = (lines * fringeSize) - xs1[2]
			canvas.Polygon(xs1, ys1, fill2)
		}
	}
	canvas.End()
}

func isFill1InHexagon(xL, yL int) bool {
	if xL == 0 {
		if yL > 1 && yL < 6 {
			return true
		}
	}
	if xL == 1 || xL == 2 {
		if yL > 0 && yL < 7 {
			return true
		}
	}
	if xL == 3 {
		if yL >= 0 && yL <= 7 {
			return true
		}
	}
	return false
}

func isFill2InHexagon(xL, yL int) bool {
	if xL == 0 || xL == 1 {
		if yL > 0 && yL < 6 {
			return true
		}
	}
	if xL == 1 {
		if yL > 0 && yL < 6 {
			return true
		}
	}
	if xL == 2 || xL == 3 {
		if yL >= 0 && yL <= 6 {
			return true
		}
	}
	return false
}

func fillWhite() string {
	return "fill:rgb(255,255,255)"

}