Any royal road to Bezier curves...?

Claudio Grondi claudio.grondi at freenet.de
Tue Nov 22 09:30:04 CET 2005


> http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
> has a Python example implementation of qubic Bezier curves available.

Here my port to Tkinter (doesn't need PIL)

from Tkinter import *
master = Tk()
objTkCanvas = Canvas(master, width=110, height=180)
objTkCanvas.pack()

def midpoint((x1, y1), (x2, y2)):
  return ((x1+x2)/2, (y1+y2)/2)

MAX_LEVEL = 5
def drawCubicBezierCurveToCanvas(P1, P2, P3, P4, level=1):
  # global MAX_LEVEL
  # global objTkCanvas
  if level == MAX_LEVEL:
    objTkCanvas.create_line(P1[0],P1[1],P4[0],P4[1], fill='red', width=1.5)
  else:
    L1 = P1
    L2 = midpoint(P1, P2)
    H  = midpoint(P2, P3)
    R3 = midpoint(P3, P4)
    R4 = P4
    L3 = midpoint(L2, H)
    R2 = midpoint(R3, H)
    L4 = midpoint(L3, R2)
    R1 = L4
    drawCubicBezierCurveToCanvas(L1, L2, L3, L4, level+1)
    drawCubicBezierCurveToCanvas(R1, R2, R3, R4, level+1)
  #:if/else level == MAX_LEVEL
#:def draw_curve(P1, P2, P3, P4, level=1)

objTkCanvas.create_rectangle(10, 10, 100, 100, fill="yellow")
objTkCanvas.create_line(     10, 20, 100,  20, fill="green")
objTkCanvas.create_line(     10, 30, 100,  30, fill="green")
objTkCanvas.create_line(     10, 40, 100,  40, fill="green")
objTkCanvas.create_line(     10, 50, 100,  50, fill="green")
objTkCanvas.create_line(     10, 60, 100,  60, fill="green")
objTkCanvas.create_line(     10, 70, 100,  70, fill="green")
objTkCanvas.create_line(     10, 80, 100,  80, fill="green")
objTkCanvas.create_line(     10, 90, 100,  90, fill="green")
objTkCanvas.create_line(     20, 10,  20, 100, fill="green")
objTkCanvas.create_line(     30, 10,  30, 100, fill="green")
objTkCanvas.create_line(     40, 10,  40, 100, fill="green")
objTkCanvas.create_line(     50, 10,  50, 100, fill="green")
objTkCanvas.create_line(     60, 10,  60, 100, fill="green")
objTkCanvas.create_line(     70, 10,  70, 100, fill="green")
objTkCanvas.create_line(     80, 10,  80, 100, fill="green")
objTkCanvas.create_line(     90, 10,  90, 100, fill="green")

drawCubicBezierCurveToCanvas((10,10),(100,100),(100,10),(100,100))

objTkCanvas.create_text( 10, 130, anchor='sw', text='Bezier curve:',
font='Arial   10')
objTkCanvas.create_text( 10, 140, anchor='sw', text=' P1=( 10, 10)',
font='Courier  8')
objTkCanvas.create_text( 10, 150, anchor='sw', text=' P2=(100,100)',
font='Courier  8')
objTkCanvas.create_text( 10, 160, anchor='sw', text=' P3=(100, 10)',
font='Courier  8')
objTkCanvas.create_text( 10, 170, anchor='sw', text=' P4=(100,100)',
font='Courier  8')

mainloop() # show the Tkinter window with the diagram of the cubic Bezier
curve


> http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
>
> has a Python example implementation of qubic Bezier curves available.
>
> Claudio
>
> "Warren Francis" <just_another_guy287 at yahoo.com> schrieb im Newsbeitrag
> news:dlrin7$mtg$1 at charm.magnus.acs.ohio-state.edu...
> > I'm fairly new to Python (2-3 months) and I'm trying to figure out a
> simple
> > way to implement Bezier curves...  So far I've tried the following:
> >
> > http://runten.tripod.com/NURBS/
> > ...which won't work because the only compiled binaries are for Windows
> 2000,
> > python 2.1.  I'm on Windows XP (for now), using Python 2.4.  I
downloaded
> > the source distribution, but the header files aren't included, so I'm
not
> > sure how to compile it.
> >
> > It appears there's some bezier functionality in the python that comes
> > Blender... but I'm not savvy enough yet to try and extract whatever
makes
> > beziers work there, to make it work in my general-purpose Python
programs.
> >
> > Basically, I'd like to specify a curved path of an object through space.
> 3D
> > space would be wonderful, but I could jimmy-rig something if I could
just
> > get 2D...  Are bezier curves really what I want after all?
> >
> > Any thoughts would be much appreciated.  I've got some ideas I want to
> test,
> > but if I can't find a simple implementation of curves, I'm going to get
so
> > bogged down in trying to do that part, I'll never get to what I'm
excited
> > about. :-P
> >
> > Warren
> >
> >
>
>





More information about the Python-list mailing list