Skip to content

Instantly share code, notes, and snippets.

@mathyourlife
Created November 20, 2017 01:38
Show Gist options
  • Save mathyourlife/f513a93443935c3a4c57ac8222a2ec75 to your computer and use it in GitHub Desktop.
Save mathyourlife/f513a93443935c3a4c57ac8222a2ec75 to your computer and use it in GitHub Desktop.
playing around with finding the length of garland needed for different sags
package main
import (
"fmt"
"image/color"
"math"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
const (
h = float64(1)
a = float64(0.54)
png = "garland.png"
)
func s(a, t float64) float64 {
return a * math.Sinh(t/a)
}
func y(a, t float64) float64 {
return a * math.Cosh(t/a)
}
// catenaryPoints generates coordinates for a catenary curve.
func catenaryPoints(n int, a float64) plotter.XYs {
pts := make(plotter.XYs, n)
dx := h / float64(n)
x := -h / 2
c := y(a, h/2)
for i := range pts {
pts[i].X = x + 0.5
pts[i].Y = y(a, x) - c
x += dx
}
return pts
}
func main() {
n := 1000
lineData := catenaryPoints(n, a)
// Create a new plot, set its title and axis labels.
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = fmt.Sprintf("Catenary Garland w/ "+
"sag: %0.0f%% extra length: %0.0f%%\n"+
"y = a cosh((x-h)/a) - c\n"+
"a=%0.3f h=%0.3f, c=%0.3f\n",
(y(a, h/2)-y(a, 0))*100,
(s(a, h/2)*2-1)*100,
a, h/2, y(a, h/2),
)
// Draw a grid behind the data
p.Add(plotter.NewGrid())
// Make a line plotter and set its style.
l, err := plotter.NewLine(lineData)
if err != nil {
panic(err)
}
l.LineStyle.Color = color.RGBA{B: 255, A: 255}
// Add series to the plot
p.Add(l)
p.X.Min = -0.1
p.X.Max = 1.1
p.Y.Min = -0.9
p.Y.Max = 0.1
// Save the plot to a PNG file.
if err := p.Save(4*vg.Inch, 4*vg.Inch, png); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment