Last active
August 29, 2015 14:06
-
-
Save itarato/e93565148376abac7a71 to your computer and use it in GitHub Desktop.
Go script to generate base matrix for regex based images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
"sync" | |
) | |
type Matrix struct { | |
matrix [][]int | |
} | |
var increment_map = [][]int{{0, 0}, {1, 0}, {1, 1}, {0, 1}} | |
var wc sync.WaitGroup | |
func main() { | |
m := NewMatrix(5) | |
fmt.Println(m) | |
} | |
func NewMatrix(size int) *Matrix { | |
m := &Matrix{} | |
size = int(math.Pow(2.0, float64(size))) | |
m.matrix = make([][]int, size) | |
for i, _ := range m.matrix { | |
m.matrix[i] = make([]int, size) | |
} | |
wc.Add(1) | |
go m.Generate(size, 0, 0, 0) | |
wc.Wait() | |
return m | |
} | |
func (m *Matrix) Generate(size int, x int, y int, code int) { | |
if size > 1 { | |
newSize := size >> 1 | |
wc.Add(4) | |
for i, inc := range increment_map { | |
go m.Generate(newSize, x+inc[0]*newSize, y+inc[1]*newSize, code*10+i+1) | |
} | |
} else { | |
m.matrix[y][x] = code | |
} | |
defer wc.Done() | |
} | |
func (m *Matrix) String() string { | |
var out string | |
for i, _ := range m.matrix { | |
for j, _ := range m.matrix[i] { | |
out = fmt.Sprintf("%s%d ", out, m.matrix[i][j]) | |
} | |
out = out + "\n" | |
} | |
return out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment