func GenerateFractal(im *image.Gray) { z := &cn{} c := &cn{} g := color.Gray{} for x := 0; x < 1000; x++ { for y := 0; y < 1000; y++ { c.r = (*width * float64(x) / float64(1000)) - *width/float64(2) c.i = (*height * float64(y) / float64(1000)) - *height/float64(2) c.r += *centerx c.i += *centery z.r = 0 z.i = 0 zrsqr := z.r * z.r zisqr := z.i * z.i var i uint8 = 0 for zrsqr+zisqr < 4.0 && i < 255 { i++ z.i = square(z.r+z.i) - zrsqr - zisqr z.i += c.i z.r = zrsqr - zisqr + c.r zrsqr = square(z.r) zisqr = square(z.i) } g.Y = i im.SetGray(x, y, g) } } }
func (entity *Entity) reproduce(other *Entity) *Entity { newRects := make([]ShadedRectangle, len(entity.rects)) for i := 0; i < len(entity.rects); i++ { if rand.Intn(2) == 0 { newRects[i] = entity.rects[i] } else { newRects[i] = other.rects[i] } newRects[i].color.Y += uint8(2 * mutationFactor * (rand.Float64() - 0.5) * 256) newRects[i].bounds.Min.X += int(2 * mutationFactor * (rand.Float64() - 0.5) * float64(entity.width)) newRects[i].bounds.Max.X += int(2 * mutationFactor * (rand.Float64() - 0.5) * float64(entity.width)) newRects[i].bounds.Min.Y += int(2 * mutationFactor * (rand.Float64() - 0.5) * float64(entity.height)) newRects[i].bounds.Max.Y += int(2 * mutationFactor * (rand.Float64() - 0.5) * float64(entity.height)) newRects[i].bounds.Min.X = clip(newRects[i].bounds.Min.X, 0, entity.width) newRects[i].bounds.Max.X = clip(newRects[i].bounds.Max.X, 0, entity.width) newRects[i].bounds.Min.Y = clip(newRects[i].bounds.Min.Y, 0, entity.height) newRects[i].bounds.Max.Y = clip(newRects[i].bounds.Max.Y, 0, entity.height) newRects[i].bounds = newRects[i].bounds.Canon() /* enforce max size */ if newRects[i].bounds.Max.X-newRects[i].bounds.Min.X > rectMaxSize { overage := newRects[i].bounds.Max.X - newRects[i].bounds.Min.X - rectMaxSize newRects[i].bounds.Max.X -= overage / 2 newRects[i].bounds.Min.X += overage / 2 } if newRects[i].bounds.Max.Y-newRects[i].bounds.Min.Y > rectMaxSize { overage := newRects[i].bounds.Max.Y - newRects[i].bounds.Min.Y - rectMaxSize newRects[i].bounds.Max.Y -= overage / 2 newRects[i].bounds.Min.Y += overage / 2 } } var newBgColor color.Gray if rand.Intn(2) == 0 { newBgColor = entity.bgColor } else { newBgColor = other.bgColor } newBgColor.Y += uint8(2 * mutationFactor * (rand.Float64() - 0.5) * 256) return &Entity{entity.width, entity.height, newBgColor, newRects} }
func (bilinear) Gray(src *image.Gray, x, y float64) color.Gray { p := findLinearSrc(src.Bounds(), x, y) // Array offsets for the surrounding pixels. off00 := offGray(src, p.low.X, p.low.Y) off01 := offGray(src, p.high.X, p.low.Y) off10 := offGray(src, p.low.X, p.high.Y) off11 := offGray(src, p.high.X, p.high.Y) var fc float64 fc += float64(src.Pix[off00]) * p.frac00 fc += float64(src.Pix[off01]) * p.frac01 fc += float64(src.Pix[off10]) * p.frac10 fc += float64(src.Pix[off11]) * p.frac11 var c color.Gray c.Y = uint8(fc + 0.5) return c }