ch3/ch3-02 #187
Replies: 7 comments 1 reply
-
纯新手,感觉练习好难啊!有没有答案什么的 |
Beta Was this translation helpful? Give feedback.
-
3.4
|
Beta Was this translation helpful? Give feedback.
-
练习3.2: package main
import (
"fmt"
"math"
)
const (
width, height = 600, 320
cells = 100
xyrange = 30.0
xyscale = width / 2 / xyrange
zscale = height * 0.4
angle = math.Pi / 6
)
var sin30, cos30 = math.Sin(angle), math.Cos(angle)
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, az := corner(i+1, j)
bx, by, bz := corner(i, j)
cx, cy, cz := corner(i, j+1)
dx, dy, dz := corner(i+1, j+1)
avgZ := (az + bz + cz + dz) / 4
maxZ := 1.
minZ := -1 / math.Pi
relativeZ := int32(math.Round((avgZ - minZ) / (maxZ - minZ) * 255))
color := (relativeZ << 16) | (255 - relativeZ)
fmt.Printf("<polygon points='%g,%g %g,%g %g,%g %g,%g' fill='#%6x'/>\n",
ax, ay, bx, by, cx, cy, dx, dy, color)
}
}
fmt.Println("</svg>\n")
}
func corner(i, j int) (float64, float64, float64) {
x := xyrange * (float64(i)/cells - 0.5)
y := xyrange * (float64(j)/cells - 0.5)
z := f(x, y)
sx := width/2 + (x-y)*cos30*xyscale
sy := height/2 + (x+y)*sin30*xyscale - z*zscale
return sx, sy, z
}
func f(x, y float64) float64 {
r := math.Hypot(x, y)
return math.Sin(r) / r
} |
Beta Was this translation helpful? Give feedback.
-
练习3.1 // Surface computes an SVG rendering of a 3-D surface function. import ( const ( var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°) func main() {
} func corner(i, j int) (float64, float64) {
} func f(x, y float64) float64 { func line_intersect(line1 []float64, line2 []float64) bool {
} |
Beta Was this translation helpful? Give feedback.
-
/* // Surface computes an SVG rendering of a 3-D surface function. import ( type GenFunc func(float64, float64) float64 const ( var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°) func main() { func handle(res http.ResponseWriter, req *http.Request) {
} func corner(i, j int, key string) (float64, float64) {
} func f(x, y float64) float64 { func eggBox(x, y float64) float64 { func saddle(x, y float64) float64 { func moguls(x, y float64) float64 { |
Beta Was this translation helpful? Give feedback.
-
3.3 import ( const ( var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°) func main() {
} func corner(i, j int, zValues [][]float64) (float64, float64) {
} func f(x, y float64) float64 { func getColor(z, minZ, maxZ float64) string { // 鸡蛋盒函数 (Eggbox) // 雪坡函数 (Moguls) // 马鞍面 (Saddle) |
Beta Was this translation helpful? Give feedback.
-
3.4 import ( func main() { func handler(w http.ResponseWriter, r *http.Request) { const ( var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°) func svgplot(w io.Writer) {
} func corner(i, j int, zValues [][]float64) (float64, float64) {
} func f(x, y float64) float64 { func getColor(z, minZ, maxZ float64) string { // 鸡蛋盒函数 (Eggbox) // 雪坡函数 (Moguls) // 马鞍面 (Saddle) |
Beta Was this translation helpful? Give feedback.
-
ch3/ch3-02
中文版
https://golang-china.github.io/gopl-zh/ch3/ch3-02.html
Beta Was this translation helpful? Give feedback.
All reactions