-
Notifications
You must be signed in to change notification settings - Fork 4
/
server4.go
83 lines (62 loc) · 2.08 KB
/
server4.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
// Server2 is a minimal "echo" and counter server
package main
import (
"fmt"
"log"
"math"
"net/http"
)
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°), cose(30°)
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
header := fmt.Sprintf("<svg xmlns='http://www.w3.org/2000/svg' "+
"style='stroke: grey; fill: white; stroke-width: 0.7' "+
"width='%d' height='%d'>", width, height)
w.Write([]byte(header))
var poly = ""
for i := 0; i < cells; i++ {
for j := 0; j < cells; j++ {
ax, ay, ares := corner(i+1, j)
bx, by, bres := corner(i, j)
cx, cy, cres := corner(i, j+1)
dx, dy, dres := corner(i+1, j+1)
if ares && bres && cres && dres {
poly = fmt.Sprintf("<polygon points='%g,%g,%g,%g,%g,%g,%g,%g' style='fill:#ff0000' />\n", ax, ay, bx, by, cx, cy, dx, dy)
w.Write([]byte(poly))
//fmt.Printf("<polygon points='%g,%g,%g,%g,%g,%g,%g,%g' style='fill:#0000ff' />\n", ax, ay, bx, by, cx, cy, dx, dy)
}
}
}
footer := fmt.Sprintf("</svg>")
w.Write([]byte(footer))
}
func corner(i, j int) (float64, float64, bool) {
// 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)
if math.IsInf(z, 0) {
return 0, 0, false
}
// 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, true
}
func f(x, y float64) float64 {
r := math.Hypot(x, y) // distance from (0,0)
return math.Sin(r) / r
}