Compute the Bezier polynomials of a given collection of points. Note that using mpoly::as.function.mpoly() on the resulting Bezier polynomials is made numerically stable by taking advantage of de Casteljau's algorithm; it does not use the polynomial that is printed to the screen. See bezier_function() for details.

bezier(..., indeterminate = "t")

Arguments

...

either a sequence of points or a matrix/data frame of points, see examples

indeterminate

the indeterminate of the resulting polynomial

Value

a mpoly object

See also

Examples

p1 <- c(0, 0) p2 <- c(1, 1) p3 <- c(2, -1) p4 <- c(3, 0) bezier(p1, p2, p3, p4)
#> Error in FUN(X[[i]], ...): object 'p1' not found
points <- data.frame(x = 0:3, y = c(0,1,-1,0)) bezier(points)
#> Error in 0:(n - 1): argument of length 0
points <- data.frame(x = 0:2, y = c(0,1,0)) bezier(points)
#> Error in 0:(n - 1): argument of length 0
# visualize the bernstein polynomials library(ggplot2); theme_set(theme_bw()) s <- seq(0, 1, length.out = 101) ## example 1 points <- data.frame(x = 0:3, y = c(0,1,-1,0)) (bezPolys <- bezier(points))
#> Error in 0:(n - 1): argument of length 0
f <- as.function(bezPolys)
#> Error in as.function(bezPolys): object 'bezPolys' not found
df <- as.data.frame(f(s))
#> Error in f(s): could not find function "f"
ggplot(aes(x = x, y = y), data = df) + geom_point(data = points, color = "red") + geom_path(data = points, color = "red") + geom_path()
#> Error: You're passing a function as global data. #> Have you misspelled the `data` argument in `ggplot()`
## example 1 with weights f <- as.function(bezPolys, weights = c(1,5,5,1))
#> Error in as.function(bezPolys, weights = c(1, 5, 5, 1)): object 'bezPolys' not found
df <- as.data.frame(f(s))
#> Error in f(s): could not find function "f"
ggplot(aes(x = x, y = y), data = df) + geom_point(data = points, color = "red") + geom_path(data = points, color = "red") + geom_path()
#> Error: You're passing a function as global data. #> Have you misspelled the `data` argument in `ggplot()`
## example 2 points <- data.frame(x = 0:2, y = c(0,1,0)) (bezPolys <- bezier(points))
#> Error in 0:(n - 1): argument of length 0
f <- as.function(bezPolys)
#> Error in as.function(bezPolys): object 'bezPolys' not found
df <- as.data.frame(f(s))
#> Error in f(s): could not find function "f"
ggplot(aes(x = x, y = y), data = df) + geom_point(data = points, color = "red") + geom_path(data = points, color = "red") + geom_path()
#> Error: You're passing a function as global data. #> Have you misspelled the `data` argument in `ggplot()`
## example 3 points <- data.frame(x = c(-1,-2,2,1), y = c(0,1,1,0)) (bezPolys <- bezier(points))
#> Error in 0:(n - 1): argument of length 0
f <- as.function(bezPolys)
#> Error in as.function(bezPolys): object 'bezPolys' not found
df <- as.data.frame(f(s))
#> Error in f(s): could not find function "f"
ggplot(aes(x = x, y = y), data = df) + geom_point(data = points, color = "red") + geom_path(data = points, color = "red") + geom_path()
#> Error: You're passing a function as global data. #> Have you misspelled the `data` argument in `ggplot()`
## example 4 points <- data.frame(x = c(-1,2,-2,1), y = c(0,1,1,0)) (bezPolys <- bezier(points))
#> Error in 0:(n - 1): argument of length 0
f <- as.function(bezPolys)
#> Error in as.function(bezPolys): object 'bezPolys' not found
df <- as.data.frame(f(s))
#> Error in f(s): could not find function "f"
ggplot(aes(x = x, y = y), data = df) + geom_point(data = points, color = "red") + geom_path(data = points, color = "red") + geom_path()
#> Error: You're passing a function as global data. #> Have you misspelled the `data` argument in `ggplot()`
## example 5 qplot(speed, dist, data = cars)
s <- seq(0, 1, length.out = 201) p <- bezier(cars) f <- as.function(p) df <- as.data.frame(f(s)) qplot(speed, dist, data = cars) + geom_path(data = df, color = "red")
# the curve is not invariant to permutations of the points # but it always goes through the first and last points permute_rows <- function(df) df[sample(nrow(df)),] p <- bezier(permute_rows(cars))
#> Error in permute_rows(cars): could not find function "permute_rows"
f <- as.function(p) df <- as.data.frame(f(s)) qplot(speed, dist, data = cars) + geom_path(data = df, color = "red")