GO语言打印吉祥物地鼠
首先到 https://github.com/fogleman/pt 下载相应的“包”
安装
go get -u github.com/fogleman/pt/pt
例子
在有很多例子可以借鉴!测试,只需要运行,如
cd go/src/github.com/fogleman/ptgo run examples/gopher.go
在新建一个test.go 文件,敲入以下代码。
package mainimport ( "log" "github.com/fogleman/pt/pt")func main() { scene := pt.Scene{} wall := pt.SpecularMaterial(pt.HexColor(0xFCFAE1), 2) scene.Add(pt.NewSphere(pt.Vector{4, 7, 3}, 2, pt.LightMaterial(pt.Color{1, 1, 1}, 1, pt.NoAttenuation))) scene.Add(pt.NewCube(pt.Vector{-30, -1, -30}, pt.Vector{-8, 10, 30}, wall)) scene.Add(pt.NewCube(pt.Vector{-30, -1, -30}, pt.Vector{30, 0.376662, 30}, wall)) material := pt.GlossyMaterial(pt.Color{}, 1.5, pt.Radians(30)) mesh, err := pt.LoadOBJ("examples/gopher.obj", material) if err != nil { log.Fatalln("LoadOBJ error:", err) } mesh.SmoothNormals() scene.Add(mesh) camera := pt.LookAt(pt.Vector{8, 3, 0.5}, pt.Vector{-1, 2.5, 0.5}, pt.Vector{0, 1, 0}, 45) pt.IterativeRender("out%03d.png", 10, &scene, &camera, 2560/4, 1440/4, -1, 16, 4)}
运行
go run test.go
多么漂亮的地鼠呀,这个地鼠可不是一般的地鼠,它是Go语言的吉祥物。几句简单的代码就能生成这么漂亮的图案真是神奇吧。