Example #1
0
func root(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, W, H))
	b := m.Bounds()
	//	fmt.Printf("Bounds: %v", b)
	nRed := perlin.NewPerlinNoise(time.Now().UnixNano())
	nGreen := perlin.NewPerlinNoise(time.Now().UnixNano())
	nBlue := perlin.NewPerlinNoise(time.Now().UnixNano())

	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			ftree.SetVar("x", float64(x)/W)
			ftree.SetVar("y", float64(y)/H)
			v, err := f.EvalSigmoid()
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			//			fmt.Printf("%v\t%v\t%v\n", v, v * 256.0, uint8(v * 256.0))
			fmt.Printf("%v\n", v)
			red := uint8(nRed.At2d(float64(x)/W, float64(y)/H) * 256)
			red = v
			green := uint8(nGreen.At2d(float64(x)/W, float64(y)/H) * 256)
			green = v
			blue := uint8(nBlue.At2d(float64(x)/W, float64(y)/H) * 256)
			blue = v
			alpha := uint8(255) //uint8(nAlpha.At2d(float64(x) / W, float64(y) / H) * 256)
			m.Set(x, y, color.RGBA{red, green, blue, alpha})
		}
	}

	w.Header().Set("Content-Type", "image/png")
	if err := png.Encode(w, m); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Example #2
0
func NewTestGenerator(seed int64) *TestGenerator {
	perlin := perlin.NewPerlinNoise(seed)

	randSource := rand.NewSource(time.Nanoseconds())
	randGen := rand.New(randSource)

	return &TestGenerator{
		randSource: randSource,
		randGen:    randGen,
		heightSource: &Sum{
			Inputs: []ISource{
				&Turbulence{
					Dx:     &Scale{50, 1, &Offset{20.1, 0, perlin}},
					Dy:     &Scale{50, 1, &Offset{10.1, 0, perlin}},
					Factor: 50,
					Source: &Scale{
						Wavelength: 200,
						Amplitude:  50,
						Source:     perlin,
					},
				},
				&Turbulence{
					Dx:     &Scale{40, 1, &Offset{20.1, 0, perlin}},
					Dy:     &Scale{40, 1, &Offset{10.1, 0, perlin}},
					Factor: 10,
					Source: &Mult{
						A: &Scale{
							Wavelength: 40,
							Amplitude:  20,
							Source:     perlin,
						},
						// Local steepness.
						B: &Scale{
							Wavelength: 200,
							Amplitude:  1,
							Source:     &Add{perlin, 0.6},
						},
					},
				},
				&Scale{
					Wavelength: 5,
					Amplitude:  2,
					Source:     perlin,
				},
			},
		},
	}
}
Example #3
0
func main() {
	w := 512
	h := 512

	perlin := perlin.NewPerlinNoise(0)

	gen := &Sum{
		Inputs: []ISource{
			&Turbulence{
				Dx:     &Scale{50, 1, &Offset{20.1, 0, perlin}},
				Dy:     &Scale{50, 1, &Offset{10.1, 0, perlin}},
				Factor: 100,
				Source: &Scale{
					Wavelength: 200,
					Amplitude:  100,
					Source:     perlin,
				},
			},
			&Turbulence{
				Dx:     &Scale{40, 1, &Offset{20.1, 0, perlin}},
				Dy:     &Scale{40, 1, &Offset{10.1, 0, perlin}},
				Factor: 10,
				Source: &Mult{
					A: &Scale{
						Wavelength: 40,
						Amplitude:  20,
						Source:     perlin,
					},
					// Local steepness.
					B: &Scale{
						Wavelength: 200,
						Amplitude:  1,
						Source:     &Add{perlin, 0.6},
					},
				},
			},
			&Scale{
				Wavelength: 5,
				Amplitude:  2,
				Source:     perlin,
			},
		},
	}

	values := make([]float64, h*h)

	var valueStat Stat

	for row := 0; row < h; row++ {
		for col := 0; col < w; col++ {
			value := gen.At2d(float64(col), float64(row))
			valueStat.Add(value)
			values[row*w+col] = value
		}
	}

	img := image.NewGray(image.Rect(0, 0, w, h))

	base := valueStat.min
	scale := 255 / (valueStat.max - valueStat.min)

	for row := 0; row < h; row++ {
		for col := 0; col < w; col++ {
			scaled := scale * (values[row*w+col] - base)
			img.Set(col, row, color.Gray{uint8(scaled)})
		}
	}

	log.Printf("value stats %#v", valueStat)

	outFile, err := os.Create("output.png")
	if err != nil {
		log.Fatal(err)
	}
	defer outFile.Close()
	png.Encode(outFile, img)
}