Source File
bezier.go
Belonging Package
gonum.org/v1/plot/tools/bezier
// Copyright ©2013 The Gonum Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Package bezier implements 2D Bézier curve calculation.package bezier // import "gonum.org/v1/plot/tools/bezier"importtype point struct {Point, Control vg.Point}// Curve implements Bezier curve calculation according to the algorithm of Robert D. Miller.//// Graphics Gems 5, 'Quick and Simple Bézier Curve Drawing', pages 206-209.type Curve []point// NewCurve returns a Curve initialized with the control points in cp.func ( ...vg.Point) Curve {if len() == 0 {return nil}:= make(Curve, len())for , := range {[].Point =}var vg.Lengthfor , := range {switch {case 0:= 1case 1:= vg.Length(len()) - 1default:*= vg.Length(len()-) / vg.Length()}[].Control.X = .Point.X *[].Control.Y = .Point.Y *}return}// Point returns the point at t along the curve, where 0 ≤ t ≤ 1.func ( Curve) ( float64) vg.Point {[0].Point = [0].Control:=for , := range [1:] {[+1].Point = vg.Point{X: .Control.X * vg.Length(),Y: .Control.Y * vg.Length(),}*=}var (= 1 -=):= [len()-1].Pointfor := len() - 2; >= 0; -- {.X += [].Point.X * vg.Length().Y += [].Point.Y * vg.Length()*=}return}// Curve returns a slice of vg.Point, p, filled with points along the Bézier curve described by c.// If the length of p is less than 2, the curve points are undefined. The length of p is not// altered by the call.func ( Curve) ( []vg.Point) []vg.Point {for , := 0, float64(len()-1); < len(); ++ {[] = .Point(float64() / )}return}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |