Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
pub:labprog:20181015 [2018/10/22 15:11] lucafavalli |
pub:labprog:20181015 [2018/10/22 15:14] (current) lucafavalli |
||
---|---|---|---|
Line 209: | Line 209: | ||
fmt.Println("E' il", giorno, mese, anno) | fmt.Println("E' il", giorno, mese, anno) | ||
} | } | ||
+ | </code> | ||
+ | <code> | ||
+ | /* | ||
+ | Scrivere un programma Go che converta in Fahrenheit | ||
+ | una temperatura espressa in gradi centigradi. | ||
+ | */ | ||
+ | |||
+ | package main | ||
+ | import "fmt" | ||
+ | |||
+ | func main() { | ||
+ | var celsius, farenheit float64 | ||
+ | fmt.Println("Inserire temperatura in gradi Celsius.") | ||
+ | fmt.Scan(& celsius) | ||
+ | farenheit=(celsius*9/5) + 32 | ||
+ | fmt.Println("Farenheit:", farenheit) | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | //Surface.go | ||
+ | // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. | ||
+ | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | ||
+ | |||
+ | // See page 58. | ||
+ | //!+ | ||
+ | |||
+ | // Surface computes an SVG rendering of a 3-D surface function. | ||
+ | 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 := 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>") | ||
+ | } corner(i, j int) (float64, float64) { | ||
+ | // 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) | ||
+ | |||
+ | // 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 | ||
+ | } | ||
+ | |||
+ | func f(x, y | ||
+ | |||
+ | float64) float64 { | ||
+ | r := math.Hypot(x, y) // distance from (0,0) | ||
+ | return math.Sin(r) / r | ||
+ | } | ||
+ | |||
+ | //!- | ||
+ | |||
</code> | </code> |