Commit c9cd80a0 authored by santiaago's avatar santiaago
Browse files

refactor isogrids #41

add files mirror.go and triangles.go
refactor isogrids()
parent 12815f32
Loading
Loading
Loading
Loading
+1 −59
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@ package isogrids
import (
	"image/color"
	"log"
	"math"
	"net/http"

	"github.com/ajstarks/svgo"
@@ -18,10 +17,7 @@ func Hexa(w http.ResponseWriter, key string, colors []color.RGBA, size, lines in
	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)))))
	distance := distanceTo3rdPoint(fringeSize)

	fringeSize = distance
	lines = size / fringeSize
@@ -163,60 +159,6 @@ func isFill2InHexagon(xL, yL, lines int) bool {
	return false
}

// mirrorCoordinates returns an array of coordinates mirrored with respect to x.
func mirrorCoordinates(xs []int, lines, fringeSize, offset int) (xsMirror []int) {

	xsMirror = make([]int, len(xs))
	for i, _ := range xs {
		xsMirror[i] = (lines * fringeSize) - xs[i] + offset
	}
	return
}

// right1stTriangle computes a right oriented triangle '>'
func right1stTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL * fringeSize
	x2 = xL*fringeSize + distance
	x3 = x1
	y1 = yL * fringeSize
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance
	return
}

// left1stTriangle computes the coordinates of a left oriented triangle '<'
func left1stTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL*fringeSize + distance
	x2 = xL * fringeSize
	x3 = x1
	y1 = yL * fringeSize
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance
	return
}

// left2ndTriangle computes the coordinates of a left oriented triangle '<'
func left2ndTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL*fringeSize + distance
	x2 = xL * fringeSize
	x3 = x1
	y1 = yL*fringeSize + fringeSize/2
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance + fringeSize/2
	return
}

// right2ndTriangle computes the coordinates of a right oriented triangle '>'
func right2ndTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL * fringeSize
	x2 = xL*fringeSize + distance
	x3 = x1
	y1 = yL*fringeSize + fringeSize/2
	y2 = yL + fringeSize
	y3 = yL*fringeSize + fringeSize/2 + distance
	return
}

// fillWhite returns the svg style to paint an object white.
func fillWhite() string {
	return "fill:rgb(255,255,255)"
+26 −37
Original line number Diff line number Diff line
@@ -14,58 +14,47 @@ func Isogrids(w http.ResponseWriter, key string, colors []color.RGBA, size, line
	canvas.Start(size, size)

	fringeSize := size / lines
	distance := distanceTo3rdPoint(fringeSize)
	fringeSize = distance
	lines = size / fringeSize

	// triangle grid here:
	for xL := -1; xL <= lines/2; xL++ {
		for yL := -1; yL <= lines; yL++ {
			var x1, x2, x3, y1, y2, y3 int
	for xL := 0; xL < lines/2; xL++ {
		for yL := 0; yL < lines; yL++ {
			var x1, x2, 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
				x1, y1, x2, y2, _, y3 = right1stTriangle(xL, yL, fringeSize, distance)
			} else {
				x1 = (xL + 1) * fringeSize
				x2 = xL * fringeSize
				x3 = x1
				y1 = yL * fringeSize
				y2 = y1 + fringeSize/2
				y3 = (yL + 1) * fringeSize
				x1, y1, x2, y2, _, y3 = left1stTriangle(xL, yL, fringeSize, distance)
			}
			xs := []int{x1, x2, x3}
			xs := []int{x2, x1, x2}
			ys := []int{y1, y2, y3}
			fill1 := draw.FillFromRGBA(draw.PickColor(key, colors, (xL+3*yL+lines)%15))
			canvas.Polygon(xs, ys, fill1)
			var x11, x12, x13, y11, y12, y13 int

			xsMirror := mirrorCoordinates(xs, lines, fringeSize, 0)
			canvas.Polygon(xsMirror, ys, fill1)

			var x11, x12, 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
				x11, y11, x12, y12, _, y13 = left2ndTriangle(xL, yL, fringeSize, distance)

				// in order to have a perfect hexagon,
				// we make sure that the previous triangle and this one touch each other in this point.
				y12 = y3
			} else {
				x11 = (xL) * fringeSize
				x12 = (xL + 1) * fringeSize
				x13 = x11
				y11 = yL*fringeSize + fringeSize/2
				x11, y11, x12, y12, _, y13 = right2ndTriangle(xL, yL, fringeSize, distance)

				// in order to have a perfect hexagon,
				// we make sure that the previous triangle and this one touch each other in this point.
				y12 = y1 + fringeSize
				y13 = (yL+1)*fringeSize + fringeSize/2
			}
			xs1 := []int{x11, x12, x13}
			xs1 := []int{x12, x11, x12}
			ys1 := []int{y11, y12, y13}
			fill2 := draw.FillFromRGBA(draw.PickColor(key, colors, (xL+3*yL+1+lines)%15))
			canvas.Polygon(xs1, ys1, fill2)
			// apply mirror:
			xs[0] = (lines * fringeSize) - xs[0]
			xs[1] = (lines * fringeSize) - xs[1]
			xs[2] = (lines * fringeSize) - xs[2]
			xs1[0] = (lines * fringeSize) - xs1[0]
			xs1[1] = (lines * fringeSize) - xs1[1]
			xs1[2] = (lines * fringeSize) - xs1[2]
			canvas.Polygon(xs, ys, fill1)

			xs1 = mirrorCoordinates(xs1, lines, fringeSize, 0)
			canvas.Polygon(xs1, ys1, fill2)
		}
	}
+10 −0
Original line number Diff line number Diff line
package isogrids

func mirrorCoordinates(xs []int, lines, fringeSize, offset int) (xsMirror []int) {

	xsMirror = make([]int, len(xs))
	for i, _ := range xs {
		xsMirror[i] = (lines * fringeSize) - xs[i] + offset
	}
	return
}
+55 −0
Original line number Diff line number Diff line
package isogrids

import "math"

func distanceTo3rdPoint(AC int) int {
	// distance from center of vector to third point of equilateral triangles
	// ABC triangle, O is the center of AB vector
	// OC = SQRT(AC^2 - AO^2)
	return int(math.Ceil(math.Sqrt((float64(AC) * float64(AC)) - (float64(AC)/float64(2))*(float64(AC)/float64(2)))))

}

// right1stTriangle computes a right oriented triangle '>'
func right1stTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL * fringeSize
	x2 = xL*fringeSize + distance
	x3 = x1
	y1 = yL * fringeSize
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance
	return
}

// left1stTriangle computes the coordinates of a left oriented triangle '<'
func left1stTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL*fringeSize + distance
	x2 = xL * fringeSize
	x3 = x1
	y1 = yL * fringeSize
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance
	return
}

// left2ndTriangle computes the coordinates of a left oriented triangle '<'
func left2ndTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL*fringeSize + distance
	x2 = xL * fringeSize
	x3 = x1
	y1 = yL*fringeSize + fringeSize/2
	y2 = y1 + fringeSize/2
	y3 = yL*fringeSize + distance + fringeSize/2
	return
}

// right2ndTriangle computes the coordinates of a right oriented triangle '>'
func right2ndTriangle(xL, yL, fringeSize, distance int) (x1, y1, x2, y2, x3, y3 int) {
	x1 = xL * fringeSize
	x2 = xL*fringeSize + distance
	x3 = x1
	y1 = yL*fringeSize + fringeSize/2
	y2 = yL + fringeSize
	y3 = yL*fringeSize + fringeSize/2 + distance
	return
}