QuadraticBezier
This commit is contained in:
parent
104a343597
commit
a68d5b73e5
27
bezier.go
27
bezier.go
@ -2,6 +2,33 @@ package gg
|
|||||||
|
|
||||||
import "math"
|
import "math"
|
||||||
|
|
||||||
|
func quadratic(x0, y0, x1, y1, x2, y2, t float64) (x, y float64) {
|
||||||
|
u := 1 - t
|
||||||
|
a := u * u
|
||||||
|
b := 2 * u * t
|
||||||
|
c := t * t
|
||||||
|
x = a*x0 + b*x1 + c*x2
|
||||||
|
y = a*y0 + b*y1 + c*y2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func QuadraticBezier(x0, y0, x1, y1, x2, y2 float64) []Point {
|
||||||
|
l := (math.Hypot(x1-x0, y1-y0) +
|
||||||
|
math.Hypot(x2-x1, y2-y1))
|
||||||
|
n := int(l + 0.5)
|
||||||
|
if n < 4 {
|
||||||
|
n = 4
|
||||||
|
}
|
||||||
|
d := float64(n) - 1
|
||||||
|
result := make([]Point, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
t := float64(i) / d
|
||||||
|
x, y := quadratic(x0, y0, x1, y1, x2, y2, t)
|
||||||
|
result[i] = Point{x, y}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func cubic(x0, y0, x1, y1, x2, y2, x3, y3, t float64) (x, y float64) {
|
func cubic(x0, y0, x1, y1, x2, y2, x3, y3, t float64) (x, y float64) {
|
||||||
u := 1 - t
|
u := 1 - t
|
||||||
a := u * u * u
|
a := u * u * u
|
||||||
|
Loading…
Reference in New Issue
Block a user