Browse Source

surface

master
dewjjj 1 year ago
parent
commit
e648de66b4
  1. 3
      README.md
  2. 45
      surface/surface.go

3
README.md

@ -22,4 +22,5 @@
| 18 | floatToInt | 浮点数损失 |
| 19 | runeLiteral | 文字符号 |
| 20 | float | 浮点数 |
| 21 | InfNaN | 无穷非数 |
| 21 | InfNaN | 无穷非数 |
| 22 | surface | 三维曲面函数图像 |

45
surface/surface.go

@ -0,0 +1,45 @@
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 := corner(i+1, j)
bx, by := corner(i, j)
cx, cy := corner(i, j+1)
dx, dy := corner(i+1, j+1)
fmt.Printf("<polygon points='%g,%g %g,%g %g,%g %g,%g'/>\n", ax, ay, bx, by, cx, cy, dx, dy)
}
fmt.Println("</svg>")
}
}
func corner(i, j int) (float64, float64) {
x := xyrange * (float64(i)/cells - 0.5)
y := xyrange * (float64(i)/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
}
func f(x, y float64) float64 {
r := math.Hypot(x, y)
return math.Sin(r)
}
Loading…
Cancel
Save