Example #1
0
func (h *Grid) getVCWithinBounds(vc hexcoords.Vertex) hexcoords.Vertex {
	if h.WithinBounds(vc.Hex()) {
		return vc
	}
	var idents = vc.IdenticalVertices()
	for _, id := range idents[1:] {
		if h.WithinBounds(id.Hex()) {
			return id
		}
	}
	return vc
}
Example #2
0
func (h *Grid) genVertices(defaultValue Value) {
	// Make space for vertices/pointers.
	h.v = make([]Vertex, 0, h.expectedNumVertices())
	h.vertices = make([][][]*Vertex, h.n)
	for i := 0; i < h.n; i++ {
		h.vertices[i] = make([][]*Vertex, h.m)
		for j := 0; j < h.m; j++ {
			h.vertices[i][j] = make([]*Vertex, 6)
		}
	}
	// Generate vertices
	for i := 0; i < h.n; i++ {
		for j := 0; j < h.m; j++ {
			var (
				c   = h.hexCoords(i, j)
				hex = h.GetHex(c)
			)
			for k := 0; k < 6; k++ {
				if h.vertices[i][j][k] == nil {
					var (
						vert          = hexcoords.Vertex{c.U, c.V, k}
						identVertices = vert.IdenticalVertices()
						coords        = vert
						value         Value
					)
					if identVertices == nil {
						panic("outofbounds")
					}
					switch defaultValue.(type) {
					case func(hexcoords.Vertex) Value:
						var f = defaultValue.(func(hexcoords.Vertex) Value)
						value = f(coords)
					default:
						value = defaultValue
					}
					h.v = append(h.v, Vertex{Hex: coords, Pos: hex[k], Value: value})
					for _, ident := range identVertices {
						var (
							c              = ident.Hex()
							iIdent, jIdent = h.hexIndex(c)
						)
						if h.WithinBounds(c) {
							h.vertices[iIdent][jIdent][ident.K] = &(h.v[len(h.v)-1])
						}
					}
				}
			}
		}
	}
}