Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
85
Task/Brownian-tree/Go/brownian-tree-1.go
Normal file
85
Task/Brownian-tree/Go/brownian-tree-1.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"math/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
const w = 400 // image width
|
||||
const h = 300 // image height
|
||||
const n = 15000 // number of particles to add
|
||||
const frost = 255 // white
|
||||
|
||||
var g *image.Gray
|
||||
|
||||
func main() {
|
||||
g = image.NewGray(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})
|
||||
// off center seed position makes pleasingly asymetrical tree
|
||||
g.SetGray(w/3, h/3, color.Gray{frost})
|
||||
generate:
|
||||
for a := 0; a < n; {
|
||||
// generate random position for new particle
|
||||
rp := image.Point{rand.Intn(w), rand.Intn(h)}
|
||||
if g.At(rp.X, rp.Y).(color.Gray).Y == frost {
|
||||
// position is already set. find a nearby free position.
|
||||
for {
|
||||
rp.X += rand.Intn(3) - 1
|
||||
rp.Y += rand.Intn(3) - 1
|
||||
// execpt if we run out of bounds, consider the particle lost.
|
||||
if !rp.In(g.Rect) {
|
||||
continue generate
|
||||
}
|
||||
if g.At(rp.X, rp.Y).(color.Gray).Y != frost {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// else particle is in free space. let it wander
|
||||
// until it touches tree
|
||||
for !hasNeighbor(rp) {
|
||||
rp.X += rand.Intn(3) - 1
|
||||
rp.Y += rand.Intn(3) - 1
|
||||
// but again, if it wanders out of bounds consider it lost.
|
||||
if !rp.In(g.Rect) {
|
||||
continue generate
|
||||
}
|
||||
}
|
||||
}
|
||||
// x, y now specify a free position toucing the tree.
|
||||
g.SetGray(rp.X, rp.Y, color.Gray{frost})
|
||||
a++
|
||||
// progress indicator
|
||||
if a%100 == 0 {
|
||||
fmt.Println(a, "of", n)
|
||||
}
|
||||
}
|
||||
f, err := os.Create("tree.png")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = png.Encode(f, g)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
|
||||
var n8 = []image.Point{
|
||||
{-1, -1}, {-1, 0}, {-1, 1},
|
||||
{0, -1}, {0, 1},
|
||||
{1, -1}, {1, 0}, {1, 1}}
|
||||
|
||||
func hasNeighbor(p image.Point) bool {
|
||||
for _, n := range n8 {
|
||||
o := p.Add(n)
|
||||
if o.In(g.Rect) && g.At(o.X, o.Y).(color.Gray).Y == frost {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
79
Task/Brownian-tree/Go/brownian-tree-2.go
Normal file
79
Task/Brownian-tree/Go/brownian-tree-2.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package main
|
||||
|
||||
// Files required to build supporting package raster are found in:
|
||||
// * Bitmap
|
||||
// * Grayscale image
|
||||
// * Write a PPM file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"raster"
|
||||
)
|
||||
|
||||
const w = 400 // image width
|
||||
const h = 300 // image height
|
||||
const n = 15000 // number of particles to add
|
||||
const frost = 65535 // white
|
||||
|
||||
var g *raster.Grmap
|
||||
|
||||
func main() {
|
||||
g = raster.NewGrmap(w, h)
|
||||
// off center seed position makes pleasingly asymetrical tree
|
||||
g.SetPx(w/3, h/3, frost)
|
||||
var x, y int
|
||||
generate:
|
||||
for a := 0; a < n; {
|
||||
// generate random position for new particle
|
||||
x, y = rand.Intn(w), rand.Intn(h)
|
||||
switch p, ok := g.GetPx(x, y); p {
|
||||
case frost:
|
||||
// position is already set. find a nearby free position.
|
||||
for p == frost {
|
||||
x += rand.Intn(3) - 1
|
||||
y += rand.Intn(3) - 1
|
||||
p, ok = g.GetPx(x, y)
|
||||
|
||||
// execpt if we run out of bounds, consider the particle lost.
|
||||
if !ok {
|
||||
continue generate
|
||||
}
|
||||
}
|
||||
default:
|
||||
// else particle is in free space. let it wander
|
||||
// until it touches tree
|
||||
for !hasNeighbor(x, y) {
|
||||
x += rand.Intn(3) - 1
|
||||
y += rand.Intn(3) - 1
|
||||
// but again, if it wanders out of bounds consider it lost.
|
||||
_, ok = g.GetPx(x, y)
|
||||
if !ok {
|
||||
continue generate
|
||||
}
|
||||
}
|
||||
}
|
||||
// x, y now specify a free position toucing the tree.
|
||||
g.SetPx(x, y, frost)
|
||||
a++
|
||||
// progress indicator
|
||||
if a%100 == 0 {
|
||||
fmt.Println(a, "of", n)
|
||||
}
|
||||
}
|
||||
g.Bitmap().WritePpmFile("tree.ppm")
|
||||
}
|
||||
|
||||
var n8 = [][]int{
|
||||
{-1, -1}, {-1, 0}, {-1, 1},
|
||||
{ 0, -1}, { 0, 1},
|
||||
{ 1, -1}, { 1, 0}, { 1, 1}}
|
||||
|
||||
func hasNeighbor(x, y int) bool {
|
||||
for _, n := range n8 {
|
||||
if p, ok := g.GetPx(x+n[0], y+n[1]); ok && p == frost {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue