Commit 12052504 authored by santiaago's avatar santiaago
Browse files

make hexa isogrids available for 6 and 8 lines and expose hexaline parameter

parent 7d9ee7b3
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ func Hexa(w http.ResponseWriter, r *http.Request) {

	theme := extract.Theme(r)
	numColors := extract.NumColors(r)

	lines := int(extract.Hexalines(r))
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap["base"][0]
@@ -56,5 +56,5 @@ func Hexa(w http.ResponseWriter, r *http.Request) {
		colors = append(colors, bg, fg)
	}
	write.ImageSVG(w)
	draw.IsogridsHexa(w, key, colors, size)
	draw.IsogridsHexa(w, key, colors, size, lines)
}
+79 −30
Original line number Diff line number Diff line
@@ -194,11 +194,10 @@ func Isogrids(w http.ResponseWriter, key string, colors []color.RGBA, size int)
}

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

	lines := 8
	fringeSize := size / lines

	// triangle grid here:
@@ -207,15 +206,14 @@ func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size i

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

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

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

@@ -237,6 +235,13 @@ func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size i
			}
			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)

			// apply mirror:
@@ -263,6 +268,12 @@ func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size i
			}
			xs1 := []int{x11, x12, x13}
			ys1 := []int{y11, y12, y13}
			if lines%4 != 0 {
				xs1[0] = x12
				xs1[1] = x11
				xs1[2] = x12
			}

			canvas.Polygon(xs1, ys1, fill2)
			xs1[0] = (lines * fringeSize) - xs1[0]
			xs1[1] = (lines * fringeSize) - xs1[1]
@@ -273,7 +284,22 @@ func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size i
	canvas.End()
}

func isFill1InHexagon(xL, yL int) bool {
func isFill1InHexagon(xL, yL, lines int) bool {
	if lines%6 == 0 {
		half := lines / 2
		start := half / 2
		if xL < start+1 {
			if yL > start-1 && yL < start+half+1 {
				return true
			}
		}
		if xL == half-1 {
			if yL > start-1-1 && yL < start+half+1+1 {
				return true
			}
		}
		return false
	} else if lines%4 == 0 {
		if xL == 0 {
			if yL > 1 && yL < 6 {
				return true
@@ -291,8 +317,30 @@ func isFill1InHexagon(xL, yL int) bool {
		}
		return false
	}
	return false
}

func isFill2InHexagon(xL, yL, lines int) bool {
	if lines%6 == 0 {
		half := lines / 2
		start := half / 2

func isFill2InHexagon(xL, yL int) bool {
		if xL < start {
			if yL > start-1 && yL < start+half {
				return true
			}
		}
		if xL == 1 {
			if yL > start-1-1 && yL < start+half+1 {
				return true
			}
		}
		if xL == half-1 {
			if yL > start-1-1 && yL < start+half+1 {
				return true
			}
		}
	} else if lines%4 == 0 {
		if xL == 0 || xL == 1 {
			if yL > 0 && yL < 6 {
				return true
@@ -308,6 +356,7 @@ func isFill2InHexagon(xL, yL int) bool {
				return true
			}
		}
	}
	return false
}

+12 −0
Original line number Diff line number Diff line
@@ -96,3 +96,15 @@ func NumColors(r *http.Request) int64 {
	}
	return 2
}

func Hexalines(r *http.Request) int64 {
	s := strings.ToLower(r.FormValue("hexalines"))
	if len(s) > 0 {
		if n, err := strconv.ParseInt(s, 0, 64); err == nil {
			if n%6 == 0 || n%4 == 0 {
				return n
			}
		}
	}
	return 6
}