about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2019-01-25 13:51:28 +0100
committerhanemile <hanemile@protonmail.com>2019-01-25 13:51:28 +0100
commit4e3d9c53580b45c2dd4cd32b3bfe905dfc36ddf4 (patch)
tree57f251899d737e6db8c65f3ea1e9af5cedd10548
parent8ec4d560c79760746d1ca991938f2bf7f81011fc (diff)
push
-rw-r--r--main.go161
1 files changed, 161 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..e0f08d6
--- /dev/null
+++ b/main.go
@@ -0,0 +1,161 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+
+	"github.com/fogleman/gg"
+
+	"git.darknebu.la/GalaxySimulator/structs"
+)
+
+var (
+	treeArray []*structs.Node
+)
+
+func readfile(filename string) {
+	// read the json
+	file, err := ioutil.ReadFile(filename)
+	if err != nil {
+		panic(err)
+	}
+
+	// initialize the rootnode
+	var rootnode structs.Node
+
+	// unmarshal the json into the rootnode
+	err = json.Unmarshal(file, &rootnode)
+	if err != nil {
+		panic(err)
+	}
+
+	treeArray = append(treeArray, &rootnode)
+}
+
+// draw the requested tree
+func drawtree(treeindex int64, savepath string) {
+
+	// generate a list of all stars
+	var starlist []structs.Star2D
+	starlist = treeArray[treeindex].GetAllStars()
+
+	log.Println("[   ] Initializing the Plot")
+	dc := initializePlot()
+	log.Println("[   ] Done Initializing the Plot")
+
+	log.Println("[   ] Drawing the Starlist")
+	drawStarlist(dc, starlist)
+	log.Println("[   ] Done Drawing the Starlist")
+
+	log.Println("[   ] Drawing the Boxes")
+	drawBoxes(dc, treeindex)
+	log.Println("[   ] Done Drawing the Boxes")
+
+	log.Println("[   ] Saving the image")
+	saveImage(dc, savepath)
+	log.Println("[   ] Done Saving the image")
+}
+
+func saveImage(dc *gg.Context, path string) {
+	err := dc.SavePNG(path)
+	if err != nil {
+		panic(err)
+	}
+}
+
+func drawBox(dc *gg.Context, box structs.BoundingBox) {
+	x := (box.Center.X / 5e3) - (box.Width/5e3)/2
+	y := (box.Center.Y / 5e3) - (box.Width/5e3)/2
+	w := box.Width / 5e3
+
+	log.Println("[   ] Drawing the Box")
+	dc.DrawRectangle(x, y, w, w)
+	log.Println("[   ] 0")
+	dc.Stroke()
+	log.Println("[   ] Done Drawing the Box")
+}
+
+func genBoxes(dc *gg.Context, node structs.Node) {
+
+	// if the BoundingBox is not empty, draw it
+	if node.Boundry != (structs.BoundingBox{}) {
+		drawBox(dc, node.Boundry)
+	}
+
+	for i := 0; i < len(node.Subtrees); i++ {
+		if node.Subtrees[i] != nil {
+			genBoxes(dc, *node.Subtrees[i])
+		}
+	}
+}
+
+func drawBoxes(dc *gg.Context, treeindex int64) {
+	log.Println("[   ] before genBoxes")
+	root := treeArray[treeindex]
+	genBoxes(dc, *root)
+	log.Println("[   ] after genBoxes")
+}
+
+func drawStar(dc *gg.Context, star structs.Star2D) {
+	// scalingFactor := 50
+	defaultStarSize := 2.0
+
+	x := star.C.X / 5e3
+	y := star.C.Y / 5e3
+
+	fmt.Printf("(%20.3f, %20.3f)\n", x, y)
+
+	dc.SetRGB(1, 1, 1)
+	dc.DrawCircle(x, y, defaultStarSize)
+	dc.Fill()
+	dc.Stroke()
+}
+
+func drawStarlist(dc *gg.Context, starlist []structs.Star2D) {
+	for _, star := range starlist {
+		drawStar(dc, star)
+	}
+}
+
+// initializePlot generates a new plot and returns the plot context
+func initializePlot() *gg.Context {
+	// Define the image size
+	const imageWidth = 8192
+	const imageHeight = 8192
+
+	// Initialize the new context
+	dc := gg.NewContext(imageWidth, imageHeight)
+
+	// Set the background black
+	dc.SetRGB(0, 0, 0)
+	dc.Clear()
+
+	// Invert the Y axis (positive values are on the top and right)
+	dc.InvertY()
+
+	// Set the coordinate midpoint to the middle of the image
+	dc.Translate(imageWidth/2, imageHeight/2)
+
+	return dc
+}
+
+func drawallboxes(amount int) {
+	for i := 0; i < amount; i++ {
+		index := fmt.Sprintf("%d", i)
+		readfile(fmt.Sprintf("%s.json", index))
+		drawtree(0, fmt.Sprintf("%s.png", index))
+	}
+}
+
+func main() {
+	var amount int
+	_, err := fmt.Scanf("%d", &amount)
+	if err != nil {
+		panic(err)
+	}
+
+	drawallboxes(amount)
+
+}