Commit a508676b authored by santiaago's avatar santiaago
Browse files

add random symetric handlers

parent f3b49f71
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -24,7 +24,10 @@ func main() {
	r.HandleFunc("/grid/random/?", gridRandomHandler)
	r.HandleFunc("/grid/random/[0-8]/?", gridRandomColorHandler)

	r.HandleFunc("/grid/random/symetric?", gridRandomSymetricHandler)
	r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler)
	r.HandleFunc("/grid/random/symetric/x/?", gridRandomSymetricXHandler)
	r.HandleFunc("/grid/random/symetric/y/?", gridRandomSymetricYHandler)

	r.HandleFunc("/grid/random/symetric/[0-8]/?", gridRandomSymetricColorHandler)

	r.HandleFunc("/gradient/?", gradientHandler)
+51 −6
Original line number Diff line number Diff line
@@ -4,15 +4,26 @@ import (
	"image"
	"image/color"
	"log"
	"math"
	"net/http"
)

// handler for "/gird/random/symetric"
// handler for "/gird/random/symetric/x"
// generates a black and white grid random image.
func gridRandomSymetricHandler(w http.ResponseWriter, r *http.Request) {
func gridRandomSymetricXHandler(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	colorMap := MapOfColorPatterns()
	drawRandomSymetricGrid6X6(m, colorMap[0][0], colorMap[0][1])
	drawRandomSymetricInXGrid6X6(m, colorMap[0][0], colorMap[0][1])
	var img image.Image = m
	writeImage(w, &img)
}

// handler for "/gird/random/symetric/y"
// generates a black and white grid random image.
func gridRandomSymetricYHandler(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	colorMap := MapOfColorPatterns()
	drawRandomSymetricInYGrid6X6(m, colorMap[0][0], colorMap[0][1])
	var img image.Image = m
	writeImage(w, &img)
}
@@ -33,9 +44,11 @@ func gridRandomSymetricColorHandler(w http.ResponseWriter, r *http.Request) {
}

// drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant.
func drawRandomSymetricGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
func drawRandomSymetricInYGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	size := m.Bounds().Size()
	quad := size.X / 6
	squares := 6
	quad := size.X / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for x := 0; x < size.X; x++ {
@@ -47,9 +60,41 @@ func drawRandomSymetricGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
		for y := 0; y < size.Y; y++ {
			yQuadrant := y / quad
			if _, ok := colorMap[yQuadrant]; !ok {
				if float64(yQuadrant) < middle {
					colorMap[yQuadrant] = getRandomColor(color1, color2)
				} else {
					colorMap[yQuadrant] = colorMap[squares-yQuadrant-1] //getRandomColor(color1, color2)
				}
			}
			m.Set(x, y, colorMap[yQuadrant])
		}
	}
}

// drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant.
func drawRandomSymetricInXGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	size := m.Bounds().Size()
	squares := 6
	quad := size.X / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for y := 0; y < size.Y; y++ {
		if y/quad != currentQuadrand {
			// when y quadrant changes, clear map
			colorMap = make(map[int]color.RGBA)
			currentQuadrand = y / quad
		}
		for x := 0; x < size.X; x++ {
			xQuadrant := x / quad
			if _, ok := colorMap[xQuadrant]; !ok {
				if float64(xQuadrant) < middle {
					colorMap[xQuadrant] = getRandomColor(color1, color2)
				} else {
					colorMap[xQuadrant] = colorMap[squares-xQuadrant-1]
				}
			}
			m.Set(x, y, colorMap[xQuadrant])
		}
	}
}