about summary refs log tree commit diff
path: root/draw
diff options
context:
space:
mode:
authoremile <hanemile@protonmail.com>2018-10-08 16:45:30 +0200
committeremile <hanemile@protonmail.com>2018-10-08 16:45:30 +0200
commitf3af8e9abd0fe9b6f678a11e5c4d30770d91841a (patch)
tree0b8461bcbf4f9d41d989810ff492658581cd010a /draw
parent61a348b3fc49dd60c01ffb6e3f2a218b1327cb20 (diff)
the drawForce function draws the force from the given star
Diffstat (limited to 'draw')
-rw-r--r--draw/draw.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/draw/draw.go b/draw/draw.go
index cfde776..9af3ad9 100644
--- a/draw/draw.go
+++ b/draw/draw.go
@@ -27,6 +27,66 @@ func initializePlot() *gg.Context {
 	return dc
 }
 
+// saveImages saves the given context to a png at the given path
+func saveImage(dc *gg.Context, path string) {
+	dc.SavePNG(path)
+}
+
+// drawStar draws the given stars to the given context
+func drawStar(dc *gg.Context, star structs.Star) {
+	dc.DrawPoint(star.C.X, star.C.Y, 1)
+	dc.Fill()
+	dc.Stroke()
+}
+
+// vectorLength calculates the length of the given vector
+func vectorLength(force structs.Force) float64 {
+	return math.Sqrt(math.Pow(force.X, 2) + math.Pow(force.Y, 2))
+}
+
+func drawForce(dc *gg.Context, star structs.Star) {
+	// controll the length of the vector
+	var scalingFactor float64 = 10
+
+	// Move the "cursor" to the start position of the vector
+	dc.MoveTo(star.C.X, star.C.Y)
+
+	// calculate the length of the vector
+	vecLength := vectorLength(star.F)
+
+	// Use a sigmoid function to generate useful values for coloring the vectors according to their
+	// strength
+	var val = 1.0 / (1.0 + math.Exp(-vecLength/100000))
+
+	// Set the color to a blue / red
+	dc.SetRGB(val, 0, 1-val)
+
+	// trace the Vector
+	FxUnit := star.F.X / math.Abs(vecLength)
+	FyUnit := star.F.Y / math.Abs(vecLength)
+	dc.LineTo(star.C.X+(FxUnit*scalingFactor), star.C.Y+(FyUnit*scalingFactor))
+
+	// css
+	dc.SetLineWidth(2)
+
+	// And finally: DRAW (stroke) the vector
+	dc.Stroke()
+}
+
+// drawStars draws all the stars in the given slice to the given context
+func drawStars(dc *gg.Context, slice []structs.Star) {
+	dc.SetRGB(1, 1, 1)
+
+	for _, star := range slice {
+		drawForce(dc, star)
+	}
+
+	for _, star := range slice {
+		drawStar(dc, star)
+	}
+}
+
+// Slice draws the stars and the forces acting on them and saves the result to the given path
 func Slice(slice []structs.Star, path string) {
 	dc := initializePlot()
 	dc.SetRGB(1, 1, 1)