// Approximate a circular arc of fewer than π/2 // radians with cubic Bézier curve. func partialArc(p *pdf.Path, x, y, r vg.Length, a1, a2 float64) { a := (a2 - a1) / 2 x4 := r * vg.Length(math.Cos(a)) y4 := r * vg.Length(math.Sin(a)) x1 := x4 y1 := -y4 const k = 0.5522847498 // some magic constant f := k * vg.Length(math.Tan(a)) x2 := x1 + f*y4 y2 := y1 + f*x4 x3 := x2 y3 := -y2 // Rotate and translate points into position. ar := a + a1 sinar := vg.Length(math.Sin(ar)) cosar := vg.Length(math.Cos(ar)) x2r := x2*cosar - y2*sinar + x y2r := x2*sinar + y2*cosar + y x3r := x3*cosar - y3*sinar + x y3r := x3*sinar + y3*cosar + y x4 = r*vg.Length(math.Cos(a2)) + x y4 = r*vg.Length(math.Sin(a2)) + y p.Curve(pdfPoint(x2r, y2r), pdfPoint(x3r, y3r), pdfPoint(x4, y4)) }
func traceCurve(a, b, c, d pdf.Point, canvas *pdf.Canvas) { path := new(pdf.Path) path.Move(a) path.Curve(b, c, d) canvas.Stroke(path) }
func main() { doc := pdf.New() canvas := doc.NewPage(pdf.A4Width, pdf.A4Height) path := new(pdf.Path) bottomLeft := pdf.Point{borderWidth, borderWidth} topRight := pdf.Point{pdf.A4Width - borderWidth, pdf.A4Height - borderWidth} path.Rectangle(pdf.Rectangle{bottomLeft, topRight}) canvas.Stroke(path) left := 5 * pdf.Cm right := 15 * pdf.Cm top := 15 * pdf.Cm bottom := 10 * pdf.Cm curve := new(pdf.Path) a := pdf.Point{left, bottom} b := pdf.Point{left, top} c := pdf.Point{right, top} d := pdf.Point{right, bottom} curve.Move(a) curve.Curve(b, c, d) beneath := 5 * pdf.Cm e := pdf.Point{left, beneath} f := pdf.Point{right, beneath} curve.Curve(f, e, a) left = 6.5 * pdf.Cm right = 13.5 * pdf.Cm top = 13.5 * pdf.Cm bottom = 10 * pdf.Cm beneath = 6.5 * pdf.Cm a = pdf.Point{left, 9 * pdf.Cm} b = pdf.Point{left, top} c = pdf.Point{right, top} d = pdf.Point{right, 11 * pdf.Cm} curve.Move(d) curve.Curve(c, b, a) e = pdf.Point{left, beneath} f = pdf.Point{right, beneath} curve.Curve(e, f, d) canvas.FillStroke(curve) canvas.Close() file, err := os.Create("curves.pdf") if err != nil { log.Fatal(err) } err = doc.Encode(file) if err != nil { log.Fatal(err) os.Exit(1) } file.Close() }