Skip to content

Commit

Permalink
QuadraticBezier
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed Feb 21, 2016
1 parent 104a343 commit a68d5b7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bezier.go
Expand Up @@ -2,6 +2,33 @@ package gg

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) {
u := 1 - t
a := u * u * u
Expand Down

0 comments on commit a68d5b7

Please sign in to comment.