Files
bspviz/internal/viz/dot.go

38 lines
873 B
Go

package viz
import (
"bspviz/internal/bsp"
"bytes"
"fmt"
"os"
)
func EmitDOT(root *bsp.Node, path string) error {
var buf bytes.Buffer
buf.WriteString("digraph BSP {\n")
buf.WriteString(" node [fontname=\"Helvetica\"];\n")
id := 0
var walk func(*bsp.Node) int
walk = func(n *bsp.Node) int {
my := id
id++
if n.Leaf != nil {
fmt.Fprintf(&buf, " n%d [label=\"Leaf\\nSegs=%d\", shape=ellipse, style=filled, fillcolor=lightgray];\n",
my, len(n.Leaf.Segs))
return my
}
fmt.Fprintf(&buf, " n%d [label=\"Split\\nO=(%.0f,%.0f) D=(%.0f,%.0f)\"];\n",
my, n.O.X, n.O.Y, n.D.X, n.D.Y)
l := walk(n.Left)
r := walk(n.Right)
fmt.Fprintf(&buf, " n%d -> n%d [label=\"L\"];\n", my, l)
fmt.Fprintf(&buf, " n%d -> n%d [label=\"R\"];\n", my, r)
return my
}
walk(root)
buf.WriteString("}\n")
return os.WriteFile(path, buf.Bytes(), 0644)
}