From 8a9704a5ad109baf66deb597a0e0f8f2dbd604bb Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 12 Jan 2019 16:51:23 +0100 Subject: added some comments for godoc --- boundingBox.go | 1 + draw.go | 156 --------------------------------------------------------- star.go | 89 +++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 185 deletions(-) delete mode 100644 draw.go diff --git a/boundingBox.go b/boundingBox.go index 6cf60c4..ff8bfb1 100644 --- a/boundingBox.go +++ b/boundingBox.go @@ -6,6 +6,7 @@ type BoundingBox struct { Width float64 // Width of the box } +// NewBoundingBox returns a new Bounding Box using the centerpoint and the width given by the function parameters func NewBoundingBox(center Vec2, width float64) BoundingBox { return BoundingBox{Center: center, Width: width} } diff --git a/draw.go b/draw.go deleted file mode 100644 index 277a8d2..0000000 --- a/draw.go +++ /dev/null @@ -1,156 +0,0 @@ -package structs - -import ( - "fmt" - "log" - - "github.com/fogleman/gg" -) - -func initializePlot(imageWidth int, imageHeight int) *gg.Context { - - // define an new context using the given image width and height - context := gg.NewContext(imageWidth, imageHeight) - - // set the background color to black - context.SetRGB(0, 0, 0) - context.Clear() - context.SetRGB(1, 1, 1) - - // translate the coordinate origin to the midpoint of the image - context.Translate(float64(imageWidth/2), float64(imageHeight/2)) - - return context -} - -// drawQuadtree draws a given quadtree and the stars in it recursively to the given canvas -func drawQuadtree(context *gg.Context, q Quadtree) { - - // find out if the node is a leaf find out if the node is a leaf find out if the node is a leaf find out if the node is a leaf - var draw bool = false - for i := 0; i < 4; i++ { - if q.Quadrants[i] == nil { - draw = true - } - } - - // draw the bounding box and the star if the node is a leaf - if draw == true { - - // Don't draw nonexistent stars - if q.Star != (Star2D{}) { - - // define the current star - x := q.Star.C.X - y := q.Star.C.Y - starsize := 2 - - // set the color of the stars to green - context.SetRGB(0, 1, 1) - context.DrawPoint(x, y, float64(starsize)) - context.Fill() - // context.DrawString(fmt.Sprintf("(%f, %f)", x, y), x, y) - context.Stroke() - log.Printf("[***] Drawing Star (%f, %f)", x, y) - } - - // define the bounding box - boundingx := q.Boundary.Center.X - boundingy := q.Boundary.Center.Y - boundingw := q.Boundary.Width - - // bottom left corner - contextx := boundingx - (boundingw / 2) - contexty := boundingy - (boundingw / 2) - - // draw the rectangle - context.SetRGB(1, 1, 1) - context.DrawRectangle(contextx, contexty, boundingw, boundingw) - context.Stroke() - - log.Printf("[***] Drawing Box ((%f, %f), %f)", contextx, contexty, boundingw) - } - - // draw all the other trees recursively... - for i := 0; i < 4; i++ { - // ... but only if they exist - if q.Quadrants[i] != nil { - drawQuadtree(context, *q.Quadrants[i]) - } - } -} - -// saveImage saves the given context to the given path as a png -func saveImage(context *gg.Context, outpath string) { - savePngError := context.SavePNG(outpath) - if savePngError != nil { - panic(savePngError) - } -} - -// DrawGalaxy draws the given quadtree to the given output path -func (q Quadtree) DrawGalaxy(outpath string) { - log.Printf("Drawing the quadtree to %s", outpath) - - // define the image dimensions - imageWidth := 1024 * 8 - imageHeight := 1024 * 8 - - // define a new context to draw on - context := initializePlot(imageWidth, imageHeight) - - // first recursive call of drawQuadtree - drawQuadtree(context, q) - - // save the context to the given output path - saveImage(context, outpath) -} - -// GeneratePrintTree generates forest code for drawing a tree -func (q Quadtree) GeneratePrintTree(depth int) string { - returnString := "" - if q.Star != (Star2D{}) { - returnString += "[a" - fmt.Printf("[a") - } else { - returnString += "[" - fmt.Printf("[") - } - - for i := 0; i < 4; i++ { - if q.Quadrants[i] != nil { - returnString += fmt.Sprintf("[%d]", depth) - returnString += q.Quadrants[i].GeneratePrintTree(depth + 1) - } - } - - // ok, the reason the final image will only show the nodes in the leaf is, that in the latex - // forest package that I use, trees must be drawn like this: [a[b]] and not like this: [[b]a]. - // [[b]a] == [[b]]. So there might be a lot of zeros, but that's ok! - if q.Star != (Star2D{}) { - returnString += "a]" - fmt.Printf("a]") - } else { - returnString += "]" - fmt.Printf("]") - } - - return returnString -} - -// DrawTree returns a valid LaTeX Document as a string drawing the quadtree it is called on using the forest package -func (q Quadtree) DrawTree() string { - s1 := `\documentclass{article} -\usepackage{tikz} -\usepackage{forest} -\begin{document} -\begin{forest} -for tree={circle,draw, s sep+=0.25em}` - - s2 := q.GeneratePrintTree(0) - - s3 := `\end{forest} -\end{document}` - - return fmt.Sprintf("%s\n%s\n%s\n", s1, s2, s3) -} diff --git a/star.go b/star.go index 3a7f66e..1486d5f 100644 --- a/star.go +++ b/star.go @@ -36,35 +36,6 @@ func (s Star2D) InsideOf(boundary BoundingBox) bool { } } -// Quadrant returns a string indicating in which quadrant of the given quadtree the point the method -// is applied on is. -// This methods presumes that the point is inside of the boundingBox -func (s Star2D) Quadrant(starsQuadtree *Quadtree) string { - centerX := starsQuadtree.Boundary.Center.X - centerY := starsQuadtree.Boundary.Center.Y - - // test if the point is left the the Center or not - if s.C.X < centerX { - - // Test if the point is above or below of the Center - if s.C.Y > centerY { - return "northwest" - } else { - return "southwest" - } - - // The point is right of the Center - } else { - - // Test if the point is above or below of the Center - if s.C.Y > centerY { - return "northeast" - } else { - return "southeast" - } - } -} - // Return a copy of the star by returning a star struct with the same values. func (s *Star2D) Copy() Star2D { return Star2D{s.C.Copy(), s.V.Copy(), s.M} @@ -88,3 +59,63 @@ func (s *Star2D) Accelerate(a Vec2, t float64) { s.AccelerateVelocity(a, t) s.Move(t) } + +// posX determines if the star is the positive x region of the given boundary. If it is, +// the method returns true, if not, it returns false +func (star Star2D) posX(boundary BoundingBox) bool { + + // define shortcuts + bx := boundary.Center.X + bw := boundary.Width / 2 + + if star.C.X > bx && star.C.X < bx+bw { + return true + } else { + return false + } +} + +// posY determines if the star is the positive y region of the given boundary. If it is, +// the method returns true, if not, it returns false +func (star Star2D) posY(boundary BoundingBox) bool { + + // define shortcuts + by := boundary.Center.Y + bw := boundary.Width / 2 + + if star.C.Y > by && star.C.Y < by+bw { + return true + } else { + return false + } +} + +// getRelativePosition returns the relative position of a star relative to the bounding +// bounding box it is in. It returns the integer that is mapped to a cell in the Node +// definition +func (star Star2D) getRelativePosition(boundary BoundingBox) string { + if star.posX(boundary) == true { + if star.posY(boundary) == true { + return "NE" + } else { + return "SE" + } + } else { + if star.posY(boundary) == true { + return "NW" + } else { + return "SW" + } + } +} + +func (star Star2D) getRelativePositionInt(boundary BoundingBox) int { + quadrantMap := make(map[string]int) + quadrantMap["NW"] = 0 + quadrantMap["NE"] = 1 + quadrantMap["SW"] = 2 + quadrantMap["SE"] = 3 + + QuadrantMapString := star.getRelativePosition(boundary) + return quadrantMap[QuadrantMapString] +} -- cgit 1.4.1