// vim: shiftwidth=4 tabstop=4 noexpandtab package main import ( "github.com/go-gl/mathgl/mgl32" ) // Point represents a three-dimensional point in space. type Point struct { scalars [3]float32 // x = scalars[0], y = scalars[1], z = scalars[2] } // Triangle is a geometrical triangle defined by three points. type Triangle struct { points [3]*Point } // Surface is a slice of triangles. type Surface struct { triangles []*Triangle } // getHomeView returns a transformation which shows the given Surface in the // center of the window. func (s Surface) getHomeView() mgl32.Mat4 { // evaluating the smallest and biggest values for x,y,z of the surface var min,max [3]float32 for _,triangle := range(s.triangles) { for _,point := range(triangle.points) { for index,_ := range(min) { if point.scalars[index] < min[index] { min[index] = point.scalars[index] } if point.scalars[index] > max[index] { max[index] = point.scalars[index] } } } } // calculate center point var center Point for index,_ := range(min) { center.scalars[index] = (min[index] + max[index]) / 2 } // calculate zoom var zoom,zoomX,zoomY float32 const fillFactor float32 = 0.8 zoomX = (2 / (max[0] - min[0])) * fillFactor zoomY = (2 / (max[1] - min[1])) * fillFactor if zoomX > zoomY { zoom = zoomY } else { zoom = zoomX } // calculate and return transformation var trafo mgl32.Mat4 = mgl32.Ident4() trafo = mgl32.Translate3D( -center.scalars[0], -center.scalars[1], -center.scalars[2]).Mul4(trafo) trafo = mgl32.Scale3D(zoom, zoom, zoom).Mul4(trafo) return trafo }