-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathmain.go
123 lines (111 loc) · 3.15 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ex3.3 prints an svg image, coloring its vertices based on their height.
package main
import (
"fmt"
"io"
"math"
"os"
)
const (
width, height = 600, 320 // canvas size in pixels
cells = 100 // 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°)
func svg(w io.Writer) {
zmin, zmax := minmax()
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)
bx, by := corner(i, j)
cx, cy := corner(i, j+1)
dx, dy := corner(i+1, j+1)
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",
color(i, j, zmin, zmax), ax, ay, bx, by, cx, cy, dx, dy)
}
}
fmt.Fprintln(w, "</svg>")
}
func main() {
svg(os.Stdout)
}
// minmax returns the min and max values for z given the min/max values of x
// and y and assuming a square domain.
func minmax() (min float64, max float64) {
min = math.NaN()
max = math.NaN()
for i := 0; i < cells; i++ {
for j := 0; j < cells; j++ {
for xoff := 0; xoff <= 1; xoff++ {
for yoff := 0; yoff <= 1; yoff++ {
x := xyrange * (float64(i+xoff)/cells - 0.5)
y := xyrange * (float64(j+yoff)/cells - 0.5)
z := f(x, y)
if math.IsNaN(min) || z < min {
min = z
}
if math.IsNaN(max) || z > max {
max = z
}
}
}
}
}
return
}
func color(i, j int, zmin, zmax float64) string {
min := math.NaN()
max := math.NaN()
for xoff := 0; xoff <= 1; xoff++ {
for yoff := 0; yoff <= 1; yoff++ {
x := xyrange * (float64(i+xoff)/cells - 0.5)
y := xyrange * (float64(j+yoff)/cells - 0.5)
z := f(x, y)
if math.IsNaN(min) || z < min {
min = z
}
if math.IsNaN(max) || z > max {
max = z
}
}
}
color := ""
if math.Abs(max) > math.Abs(min) {
red := math.Exp(math.Abs(max)) / math.Exp(math.Abs(zmax)) * 255
if red > 255 {
red = 255
}
color = fmt.Sprintf("#%02x0000", int(red))
} else {
blue := math.Exp(math.Abs(min)) / math.Exp(math.Abs(zmin)) * 255
if blue > 255 {
blue = 255
}
color = fmt.Sprintf("#0000%02x", int(blue))
}
return color
}
func corner(i, j int) (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 f(x, y float64) float64 {
r := math.Hypot(x, y) // distance from (0,0)
return math.Sin(r) / r
}