diff --git a/py/1stbezier.py b/py/1stbezier.py deleted file mode 100644 index 59bebc2..0000000 --- a/py/1stbezier.py +++ /dev/null @@ -1,42 +0,0 @@ -import matplotlib.pyplot as plt -import numpy as np - -cpx = [1.0, 3.0] -cpy = [1.0, 9.0] -x = [] -y = [] - -# 1st order Bernstein polynomial -#P1 = p1 * (1-t) + p2 * t -def x(t): - x = cpx[0] * (1-t) \ - + cpx[1] * t - return x -def y(t): - y = cpy[0] * (1-t) \ - + cpy[1] * t - return y - -for t in np.linspace(0, 1, 100): - xarray = x(t) - x.append(xarray) - yarray = y(t) - y.append(yarray) - -fig = plt.figure() -ax = fig.add_subplot(111) -ax.plot(x, y) -ax.plot(cpx, cpy, 'ro') - -plt.grid() -plt.xlim(0, 10) -plt.ylim(0, 10) - -# label control points -ax.text( cpx[0]+0.1, cpy[0]+0.1, 'P0') -ax.text( cpx[1]-0.1, cpy[1]+0.2, 'P1') - -ax.set_aspect("equal") -plt.title('1st Order B\'ezier Curve') -plt.draw() -plt.show() \ No newline at end of file diff --git a/py/2ndbezier.py b/py/2ndbezier.py deleted file mode 100644 index c5b0507..0000000 --- a/py/2ndbezier.py +++ /dev/null @@ -1,47 +0,0 @@ -import matplotlib.pyplot as plt -import numpy as np - -cpx = [1.0, 3.0, 6.0] -cpy = [1.0, 9.0, 6.0] -x = [] -y = [] - - -# 2nd order Bernstein polynomial -#P2 = p1 * (1-t)**2 + 2 * p2 * t * (1-t) + p3 * t**2 -def x(t): - x = cpx[0] * (1-t)**2 \ - + 2 * cpx[1] * t * (1-t) \ - + cpx[2] * t**2 - return x -def y(t): - y = cpy[0] * (1-t)**2 \ - + 2 * cpy[1] * t * (1-t) \ - + cpy[2] * t**2 - return y - -for t in np.linspace(0, 1, 100): - xarray = x(t) - x.append(xarray) - yarray = y(t) - y.append(yarray) - -fig = plt.figure() -ax = fig.add_subplot(111) -ax.plot(x, y) -ax.plot(cpx, cpy, 'r--') -ax.plot(cpx, cpy, 'ro') - -plt.grid() -plt.xlim(0, 10) -plt.ylim(0, 10) - -# label control points -ax.text( cpx[0]+0.1, cpy[0]+0.1, 'P0') -ax.text( cpx[1]-0.1, cpy[1]+0.2, 'P1') -ax.text( cpx[2]+0.1, cpy[2]+.01, 'P2') - -ax.set_aspect("equal") -plt.title('2nd Order B\'ezier Curve') -plt.draw() -plt.show() \ No newline at end of file diff --git a/py/3rdbezier.py b/py/3rdbezier.py deleted file mode 100644 index d1bc38e..0000000 --- a/py/3rdbezier.py +++ /dev/null @@ -1,51 +0,0 @@ -import matplotlib.pyplot as plt -import numpy as np - -cpx = [1.0, 3.0, 6.0, 8.0] -cpy = [1.0, 9.0, 6.0, 1.0] -x = [] -y = [] - -# 3rd order Bernstein polynomial -#P3 = p1 * (1-t)**3 + 3 * p2 * t * (1-t)**2 + 3 * p3 * (1-t) * t**2 + p4 * t**3 -def x(t): - x = cpx[0] * (1-t)**3 \ - + 3 * cpx[1] * t * (1-t)**2 \ - + 3 * cpx[2] * (1-t) * t**2 \ - + cpx[3] * t**3 - return x - -def y(t): - y = cpy[0] * (1-t)**3 \ - + 3 * cpy[1] * t * (1-t)**2 \ - + 3 * cpy[2] * (1-t) * t**2 \ - + cpy[3] * t**3 - return y - - -for t in np.linspace(0, 1, 100): - xarray = x(t) - x.append(xarray) - yarray = y(t) - y.append(yarray) - -fig = plt.figure() -ax = fig.add_subplot(111) -ax.plot(x, y) -ax.plot(cpx, cpy, 'r--') -ax.plot(cpx, cpy, 'ro') - -plt.grid() -plt.xlim(0, 10) -plt.ylim(0, 10) - -# label control points -ax.text( cpx[0]+0.1, cpy[0]+0.1, 'P0') -ax.text( cpx[1]-0.1, cpy[1]+0.2, 'P1') -ax.text( cpx[2]+0.1, cpy[2]+.01, 'P2') -ax.text( cpx[3]-0.1, cpy[3]+0.2, 'P3') -ax.set_aspect("equal") - -plt.title('3rd Order Bezier Curve') -plt.draw() -plt.show() \ No newline at end of file diff --git a/py/bern.py b/py/bern.py new file mode 100644 index 0000000..e76546b --- /dev/null +++ b/py/bern.py @@ -0,0 +1,18 @@ +from numpy import linspace as lin # linespace +from scipy.special import comb # binomial coefficient +import matplotlib.pyplot as plt # plotting + +x = lin(0, 1, 100) # Input as 100 floats between 0 and 1 +num: int = 10 # The degree + +# returns the value from the Bernstein basis function at the point x +def bern(i: int, n: int, x: float) -> float: + return comb(n, i) * x ** i * (1 - x) ** (n-i) + +# Iterates through all of the functions for the degree +for c in range(num + 1): + # Plots all of the functions + plt.plot(x, [bern(c, num, i) for i in x]) + +plt.title("Bernstein Basis Functions") # Title +plt.show() # displays graphs \ No newline at end of file diff --git a/py/bezier.py b/py/bezier.py new file mode 100644 index 0000000..3f63d37 --- /dev/null +++ b/py/bezier.py @@ -0,0 +1,28 @@ +from numpy import linspace as lin # linespace +from scipy.special import comb # binomial coefficient +import matplotlib.pyplot as plt # plotting + +cpx = [0.0, 0.5, 2.0] +cpy = [0.0, 3.0, 0.0] + +t = lin(0, 1, 100) # Input as 100 floats between 0 and 1 +num = 2 # The degree 0 indexed + +# returns the value from the Bernstein basis function at the point x +def bern(i, n, x): + return comb(n, i) * x ** i * (1 - x) ** (n-i) + +# Generalized De Casteljau's Explicit formula +def f(a, t): + ret = 0 + for i in range(num + 1): + ret += a[i] * bern(i, num, t) + return ret + +def x(t: float) -> float: return f(cpx, t) # implemented on X's control points +def y(t: float) -> float: return f(cpy, t) # implemented on Y's control points + +plt.plot([x(i) for i in t], [y(i) for i in t]) # plots X and Y +plt.plot(cpx, cpy, 'ro') # plots the control points +plt.plot(cpx, cpy, 'r:') # plots lines between control points +plt.show() \ No newline at end of file