How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the R programming language

Table of Contents

Problem Statement

Using the data storage type defined on this page for raster images, and the draw_line function defined in this other one, draw a cubic bezier curve (definition on Wikipedia).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the R programming language

Source code in the r programming language

# x, y: the x and y coordinates of the hull points
# n: the number of points in the curve.
bezierCurve <- function(x, y, n=10)
	{
	outx <- NULL
	outy <- NULL

	i <- 1
	for (t in seq(0, 1, length.out=n))
		{
		b <- bez(x, y, t)
		outx[i] <- b$x
		outy[i] <- b$y

		i <- i+1
		}

	return (list(x=outx, y=outy))
	}

bez <- function(x, y, t)
	{
	outx <- 0
	outy <- 0
	n <- length(x)-1
	for (i in 0:n)
		{
		outx <- outx + choose(n, i)*((1-t)^(n-i))*t^i*x[i+1]
		outy <- outy + choose(n, i)*((1-t)^(n-i))*t^i*y[i+1]
		}

	return (list(x=outx, y=outy))
	}

# Example usage
x <- c(4,6,4,5,6,7)
y <- 1:6
plot(x, y, "o", pch=20)
points(bezierCurve(x,y,20), type="l", col="red")


  

You may also check:How to resolve the algorithm Digital root step by step in the Clojure programming language
You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the Lua programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the Maple programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Include a file step by step in the GAP programming language