Created
March 10, 2017 12:41
-
-
Save FredrikAugust/c3cac04e0cf7f28af865e5363c8e5e83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
| "github.com/fogleman/gg" | |
| "math/rand" | |
| ) | |
| func randomFromDistribution(distribution []float64) int { | |
| randFloat := rand.Float64() | |
| sum := 0.0 | |
| for i := range distribution { | |
| sum += distribution[i] | |
| if randFloat <= sum { | |
| return i | |
| } | |
| } | |
| return 0 | |
| } | |
| func calcNextPoint(point [2]float64) [2]float64 { | |
| funcSet := [][7]float64{ | |
| [7]float64{0.000, 0.000, 0.000, 0.600, 0.00, -0.065, 0.1}, | |
| [7]float64{0.440, 0.000, 0.000, 0.550, 0.00, 0.200, 0.18}, | |
| [7]float64{0.343, -0.248, 0.199, 0.429, -0.03, 0.100, 0.18}, | |
| [7]float64{0.343, 0.248, -0.199, 0.429, 0.03, 0.100, 0.18}, | |
| [7]float64{0.280, -0.350, 0.280, 0.350, -0.05, 0.000, 0.18}, | |
| [7]float64{0.280, 0.350, -0.280, 0.350, 0.05, 0.000, 0.18}, | |
| } | |
| distribution := make([]float64, len(funcSet)) | |
| for i := range funcSet { | |
| distribution[i] = funcSet[i][6] | |
| } | |
| funcToUse := funcSet[randomFromDistribution(distribution)] | |
| return [2]float64{ | |
| funcToUse[0]*point[0] + funcToUse[1]*point[1] + funcToUse[4], | |
| funcToUse[2]*point[0] + funcToUse[3]*point[1] + funcToUse[5], | |
| } | |
| } | |
| func main() { | |
| dc := gg.NewContext(1000, 1000) | |
| dc.SetRGB(0, 0, 0) | |
| dc.InvertY() | |
| currentPoint := [2]float64{1.0, 1.0} | |
| for i := 0; i <= 100000; i++ { | |
| dc.Push() | |
| fmt.Printf("%d -> (%f,%f)\n", i, currentPoint[0], currentPoint[1]) | |
| dc.DrawPoint(currentPoint[0]*1000+500, currentPoint[1]*1000+500, 1.0) | |
| dc.Fill() | |
| currentPoint = calcNextPoint(currentPoint) | |
| dc.Pop() | |
| } | |
| dc.SavePNG("output.png") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment