-
Notifications
You must be signed in to change notification settings - Fork 4
/
sphere-contour.go
72 lines (62 loc) · 1.26 KB
/
sphere-contour.go
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
66
67
68
69
70
71
72
// https://mathinsight.org/spherical_coordinates
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math"
"os"
)
var (
width = flag.Int("width", 1024, "width")
height = flag.Int("height", 1024, "height")
radius = flag.Float64("radius", 1, "radius")
step = flag.Float64("step", 1e-3, "step")
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
m := image.NewRGBA(image.Rect(0, 0, *width, *height))
spherecontour(m, *radius, *step)
f, err := os.Create(flag.Arg(0))
ck(err)
ck(png.Encode(f, m))
ck(f.Close())
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [options] sphere_contour.png")
flag.PrintDefaults()
os.Exit(2)
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func spherecontour(m *image.RGBA, r, s float64) {
b := m.Bounds()
w := float64(b.Dx())
h := float64(b.Dy())
draw.Draw(m, b, image.NewUniform(color.White), image.ZP, draw.Src)
for p := 0.0; p <= math.Pi; p += s {
sp, cp := math.Sincos(p)
for t := 0.0; t <= 2*math.Pi; t += s {
st, ct := math.Sincos(t)
rx := r * sp * ct
ry := r * sp * st
rz := r * cp
x := rx / (1 - rz)
y := ry / (1 - rz)
x += w / 2
y += h / 2
m.SetRGBA(int(x), int(y), color.RGBA{0, 0, 0, 255})
}
}
}