高さに基づくポリゴンの色付け
Go 「プログラミング言語Go」の練習問題3.3の解答を作成しました。「gopl.io/ch3/surface」に、ポリゴンの中心点の高さに基づいて色を返す関数を追加して、SVGの
 「プログラミング言語Go」の練習問題3.3の解答を作成しました。「gopl.io/ch3/surface」に、ポリゴンの中心点の高さに基づいて色を返す関数を追加して、SVGの polygon のところに style=\"fill: #%v; storoke: black;\" を追加したものです。
| 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 | // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // このプログラムは Alan A. A. Donovan & Brian W. Kernighan による gopl.io/ch3/surface を // 改変したものです。 // Exercise 3.3 (p.67) // $ go run main.go > a.svg package main import (         "fmt"         "math" ) 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 main() {         fmt.Printf("<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, aok := corner(i+1, j)                         bx, by, bok := corner(i, j)                         cx, cy, cok := corner(i, j+1)                         dx, dy, dok := corner(i+1, j+1)                         rgb, rgbok := color(i, j)                         if !rgbok {                                 rgb = "ffffff"                         }                         if aok && bok && cok && dok {                                 fmt.Printf("<polygon points=\"%g,%g %g,%g %g,%g %g,%g\" "+                                         "style=\"fill: #%v; storoke: black;\" />\n",                                         ax, ay, bx, by, cx, cy, dx, dy, rgb)                         }                 }         }         fmt.Println("</svg>") } func color(i, j int) (string, bool) {         // Find point (x,y) at corner of cell (i,j).         x1 := xyrange * (float64(i)/cells - 0.5)         x2 := xyrange * (float64(i+1)/cells - 0.5)         y1 := xyrange * (float64(j)/cells - 0.5)         y2 := xyrange * (float64(j+1)/cells - 0.5)         // Compute surface height z.         z1, ok1 := f(x1, y1)         z2, ok2 := f(x1, y2)         z3, ok3 := f(x2, y1)         z4, ok4 := f(x2, y2)         if !ok1 || !ok2 || !ok3 || !ok4 {                 return "", false         }         z := (z1 + z2 + z3 + z4) / 4         zmax := (height / 2) / zscale         red := int(0xff * (z/(2*zmax) + 0.5))         green := int(0x00)         blue := int(0xff * (-z/(2*zmax) + 0.5))         return fmt.Sprintf("%02x%02x%02x", red, green, blue), true } 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, ok := f(x, y)         if !ok {                 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, bool) {         r := math.Hypot(x, y) // distance from (0,0)         if r == 0 {                 return 0, false         }         return math.Sin(r) / r, true } | 
参考文献
- 「プログラミング言語Go」 Alan A.A. Donovan (著), Brian W. Kernighan (著), 柴田 芳樹 (翻訳)
- 「SVGエッセンシャルズ 第2版」 J. David Eisenberg (著), Amelia Bellamy-Royds (著), 原 隆文 (翻訳)