forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (76 loc) · 2.19 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// ex3.2 prints an SVG rendering of an eggbox or saddle.
package main
import (
"fmt"
"io"
"math"
"os"
)
const (
width, height = 600, 320 // canvas size in pixels
cells = 40 // number of grid cells
xyrange = 30.0 // axis ranges (-xyrange..+xyrange)
xyscale = width / 2 / xyrange // pixels per x or y unit
zscale = height * 0.4 // pixels per z unit
angle = math.Pi / 6 // angle of x, y axes (=30°)
)
var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°)
type zFunc func(x, y float64) float64
func svg(w io.Writer, f zFunc) {
fmt.Fprintf(w, "<svg xmlns='http://www.w3.org/2000/svg' "+
"style='stroke: grey; fill: white; stroke-width: 0.7' "+
"width='%d' height='%d'>", width, height)
for i := 0; i < cells; i++ {
for j := 0; j < cells; j++ {
ax, ay := corner(i+1, j, f)
bx, by := corner(i, j, f)
cx, cy := corner(i, j+1, f)
dx, dy := corner(i+1, j+1, f)
if math.IsNaN(ax) || math.IsNaN(ay) || math.IsNaN(bx) || math.IsNaN(by) || math.IsNaN(cx) || math.IsNaN(cy) || math.IsNaN(dx) || math.IsNaN(dy) {
continue
}
fmt.Fprintf(w, "<polygon style='stroke: %s; fill: #222222' points='%g,%g %g,%g %g,%g %g,%g'/>\n",
"#666666", ax, ay, bx, by, cx, cy, dx, dy)
}
}
fmt.Fprintln(w, "</svg>")
}
func corner(i, j int, f zFunc) (float64, float64) {
// Find point (x,y) at corner of cell (i,j).
x := xyrange * (float64(i)/cells - 0.5)
y := xyrange * (float64(j)/cells - 0.5)
// Compute surface height z.
z := f(x, y)
// Project (x,y,z) isometrically onto 2-D SVG canvas (sx,sy).
sx := width/2 + (x-y)*cos30*xyscale
sy := height/2 + (x+y)*sin30*xyscale - z*zscale
return sx, sy
}
func eggbox(x, y float64) float64 {
return 0.2 * (math.Cos(x) + math.Cos(y))
}
func saddle(x, y float64) float64 {
a := 25.0
b := 17.0
a2 := a * a
b2 := b * b
return (y*y/a2 - x*x/b2)
}
func main() {
usage := "usage: ex3.2 saddle|eggbox"
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
var f zFunc
switch os.Args[1] {
case "saddle":
f = saddle
case "eggbox":
f = eggbox
default:
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
svg(os.Stdout, f)
}