/
ah.samir
/
basic-go
Обзор
Документация
Войти
/
ah.samir
/
basic-go
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cmd/skill/surface.go
83 строки
2 KB
Samir
нерфы
27 июн 2026, 15:18
27 июн 2026, 15:18
0918d2c
Код
Авторство
О чём код?
package main import ( "fmt" "io" "math" "net/http" "strconv" ) var ( width, height float64 = 600, 320 //cells = 100 xyrange float64 = 30.0 xyscale float64 = width / 2 / xyrange zscale float64 = height * 0.4 angle float64 = math.Pi / 6 ) var sin30, cos30 = math.Sin(angle), math.Cos(angle) func surfaceHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() cellsStr := r.Form.Get("cells") cells, cellsErr := strconv.Atoi(cellsStr) if cellsErr != nil { cells = 100 } widthStr := r.Form.Get("width") var widthErr error width, widthErr = strconv.ParseFloat(widthStr, 10) if widthErr != nil { width = 600 } heightStr := r.Form.Get("height") var heightErr error height, heightErr = strconv.ParseFloat(heightStr, 10) if heightErr != nil { height = 320 } xyrange = 30.0 xyscale = width / 2 / xyrange zscale = height * 0.4 angle = math.Pi / 6 w.Header().Set("Content-Type", "image/svg+xml") surface(w, cells) } func surface(w io.Writer, cells int) { 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, cells) bx, by := corner(i, j, cells) cx, cy := corner(i, j+1, cells) dx, dy := corner(i+1, j+1, cells) fmt.Fprintf(w, "<polygon points='%g,%g %g,%g %g,%g %g,%g'/>\n", ax, ay, bx, by, cx, cy, dx, dy) } } fmt.Fprintf(w, "</svg>") } func corner(i, j, cells int) (float64, float64) { x := xyrange * (float64(i)/float64(cells) - 0.5) y := xyrange * (float64(j)/float64(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) / r }