Skip to content

Instantly share code, notes, and snippets.

@mrmrs
Created November 24, 2020 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmrs/6434a0f4d70e8dd5881bc648b3f1fba1 to your computer and use it in GitHub Desktop.
Save mrmrs/6434a0f4d70e8dd5881bc648b3f1fba1 to your computer and use it in GitHub Desktop.
striped-cube.go
package main
import "github.com/fogleman/ln/ln"
type StripedCube struct {
ln.Cube
Stripes int
}
func (c *StripedCube) Paths() ln.Paths {
var paths ln.Paths
x1, y1, z1 := c.Min.X, c.Min.Y, c.Min.Z
x2, y2, z2 := c.Max.X, c.Max.Y, c.Max.Z
for i := 0; i <= c.Stripes; i++ {
p := float64(i) / float64(c.Stripes)
x := x1 + (x2-x1)*p
y := y1 + (y2-y1)*p
paths = append(paths, ln.Path{{x, y1, z1}, {x, y1, z2}})
paths = append(paths, ln.Path{{x, y2, z1}, {x, y2, z2}})
paths = append(paths, ln.Path{{x1, y, z1}, {x1, y, z2}})
paths = append(paths, ln.Path{{x2, y, z1}, {x2, y, z2}})
}
return paths
}
func main() {
// create a scene and add a single cube
scene := ln.Scene{}
cube := ln.NewCube(ln.Vector{-1, -1, -1}, ln.Vector{1, 1, 1})
scene.Add(StripedCube(cube, 8))
// define camera parameters
eye := ln.Vector{4, 3, 2} // camera position
center := ln.Vector{0, 0, 0} // camera looks at
up := ln.Vector{0, 0, 1} // up direction
// define rendering parameters
width := 1024.0 // rendered width
height := 1024.0 // rendered height
fovy := 50.0 // vertical field of view, degrees
znear := 0.1 // near z plane
zfar := 10.0 // far z plane
step := 0.01 // how finely to chop the paths for visibility testing
// compute 2D paths that depict the 3D scene
paths := scene.Render(eye, center, up, width, height, fovy, znear, zfar, step)
// render the paths in an image
paths.WriteToPNG("out.png", width, height)
// save the paths as an svg
paths.WriteToSVG("out.svg", width, height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment