-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.go
More file actions
65 lines (53 loc) · 1.39 KB
/
complex.go
File metadata and controls
65 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package complex
import (
"fmt"
"image"
"image/color"
"image/png"
_ "math"
"math/cmplx"
"math/rand"
"os"
"time"
)
func MakeImage(width, height int) (*image.RGBA, error) {
r := image.Rect(0, 0, width, height)
img := image.NewRGBA(r)
return img, nil
}
func RandomFillImage(img *image.RGBA) (*image.RGBA, error) {
for v := range img.Pix {
x, y := v%img.Stride, v/img.Stride
r, g, b := rand.Intn(255), rand.Intn(255), rand.Intn(255)
c := &color.RGBA{uint8(r), uint8(g), uint8(b), 255}
img.Set(x, y, c)
}
return img, nil
}
func FillWithFunction(img *image.RGBA) (*image.RGBA, error) {
for v := range img.Pix {
x, y := float64(v%img.Stride), float64(v/img.Stride)
// r, g, b := math.Cos(y/50), math.Hypot(x, y), math.Log(x+y)
// r, g, b := 0, 0, math.Log(x+y)
// r, g, b := 0, 0, math.Log(x+y)
z := complex(x, y)
// r, g, b := cmplx.Abs(cmplx.Log(z)), cmplx.Phase(z), cmplx.Abs(cmplx.Sinh(z/500))
r, g, b := cmplx.Phase(1/z), cmplx.Abs(1/z), 0
c := &color.RGBA{uint8(255 * r), uint8(255 * g), uint8(255 * b), 255}
img.Set(int(x), int(y), c)
}
return img, nil
}
func SaveImage(filename string, img *image.RGBA) error {
rand.Seed(time.Now().UTC().UnixNano())
filename = fmt.Sprintf("%v_%d.png", filename, rand.Int())
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
return err
}
return nil
}