From 092a7c7d08e7de5ab60b904246197bd6321b01aa Mon Sep 17 00:00:00 2001 From: tynanpurdy Date: Wed, 12 Sep 2018 10:57:09 -0400 Subject: [PATCH] separated out different order bernsteins in to their own files --- py/1stbezier.py | 42 ++++++++++++++++++++++++++ py/2ndbezier.py | 47 +++++++++++++++++++++++++++++ py/{decasteljau.py => 3rdbezeir.py} | 39 +++--------------------- 3 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 py/1stbezier.py create mode 100644 py/2ndbezier.py rename py/{decasteljau.py => 3rdbezeir.py} (55%) diff --git a/py/1stbezier.py b/py/1stbezier.py new file mode 100644 index 0000000..a9e39d0 --- /dev/null +++ b/py/1stbezier.py @@ -0,0 +1,42 @@ +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) # match with chosen functions from above + x.append(xarray) + yarray = y(t) # match with chosen functions from above + 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. comment out lines referencing cps that aren't being used +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 new file mode 100644 index 0000000..b1c1ce4 --- /dev/null +++ b/py/2ndbezier.py @@ -0,0 +1,47 @@ +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) # match with chosen functions from above + x.append(xarray) + yarray = y(t) # match with chosen functions from above + 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. comment out lines referencing cps that aren't being used +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/decasteljau.py b/py/3rdbezeir.py similarity index 55% rename from py/decasteljau.py rename to py/3rdbezeir.py index 3418ca3..fc69d68 100644 --- a/py/decasteljau.py +++ b/py/3rdbezeir.py @@ -6,45 +6,16 @@ cpy = [1.0, 9.0, 6.0, 1.0] x = [] y = [] -# uncomment the section with the order of polynomial desired. -# remember to adjust the size of the control point coordinate arrays -''' -# 1st order Bernstein polynomial -#P1 = p1 * (1-t) + p2 * t -def x1(t): - x = cpx[0] * (1-t) \ - + cpx[1] * t - return x -def y1(t): - y = cpy[0] * (1-t) \ - + cpy[1] * t - return y -''' -''' -# 2nd order Bernstein polynomial -#P2 = p1 * (1-t)**2 + 2 * p2 * t * (1-t) + p3 * t**2 -def x2(t): - x = cpx[0] * (1-t)**2 \ - + 2 * cpx[1] * t * (1-t) \ - + cpx[2] * t**2 - return x -def y2(t): - y = cpy[0] * (1-t)**2 \ - + 2 * cpy[1] * t * (1-t) \ - + cpy[2] * t**2 - return 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 x3(t): +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 y3(t): +def y(t): y = cpy[0] * (1-t)**3 \ + 3 * cpy[1] * t * (1-t)**2 \ + 3 * cpy[2] * (1-t) * t**2 \ @@ -53,9 +24,9 @@ def y3(t): for t in np.linspace(0, 1, 100): - xarray = x3(t) # match with chosen functions from above + xarray = x(t) # match with chosen functions from above x.append(xarray) - yarray = y3(t) # match with chosen functions from above + yarray = y(t) # match with chosen functions from above y.append(yarray) fig = plt.figure() @@ -75,6 +46,6 @@ 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('Bezier Curve') +plt.title('3rd Order B\'ezier Curve') plt.draw() plt.show() \ No newline at end of file -- 2.51.2