github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/web/render/bezier.go (about) 1 package render 2 3 import "math" 4 5 func quadratic(x0, y0, x1, y1, x2, y2, t float64) (x, y float64) { 6 u := 1 - t 7 a := u * u 8 b := 2 * u * t 9 c := t * t 10 x = a*x0 + b*x1 + c*x2 11 y = a*y0 + b*y1 + c*y2 12 return 13 } 14 15 func QuadraticBezier(x0, y0, x1, y1, x2, y2 float64) []Point { 16 l := (math.Hypot(x1-x0, y1-y0) + 17 math.Hypot(x2-x1, y2-y1)) 18 n := int(l + 0.5) 19 if n < 4 { 20 n = 4 21 } 22 d := float64(n) - 1 23 result := make([]Point, n) 24 for i := 0; i < n; i++ { 25 t := float64(i) / d 26 x, y := quadratic(x0, y0, x1, y1, x2, y2, t) 27 result[i] = Point{x, y} 28 } 29 return result 30 } 31 32 func cubic(x0, y0, x1, y1, x2, y2, x3, y3, t float64) (x, y float64) { 33 u := 1 - t 34 a := u * u * u 35 b := 3 * u * u * t 36 c := 3 * u * t * t 37 d := t * t * t 38 x = a*x0 + b*x1 + c*x2 + d*x3 39 y = a*y0 + b*y1 + c*y2 + d*y3 40 return 41 } 42 43 func CubicBezier(x0, y0, x1, y1, x2, y2, x3, y3 float64) []Point { 44 l := (math.Hypot(x1-x0, y1-y0) + 45 math.Hypot(x2-x1, y2-y1) + 46 math.Hypot(x3-x2, y3-y2)) 47 n := int(l + 0.5) 48 if n < 4 { 49 n = 4 50 } 51 d := float64(n) - 1 52 result := make([]Point, n) 53 for i := 0; i < n; i++ { 54 t := float64(i) / d 55 x, y := cubic(x0, y0, x1, y1, x2, y2, x3, y3, t) 56 result[i] = Point{x, y} 57 } 58 return result 59 }