implemented graphviz builderin dot.go to build dot files for graphviz

This commit is contained in:
Doc
2025-09-28 12:31:41 +02:00
parent 6129c31e97
commit db54f63c31
2 changed files with 45 additions and 1 deletions

View File

@@ -1,3 +1,37 @@
package viz
//init
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)
}