473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Any royal road to Bezier curves...?

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
Nov 22 '05 #1
9 5133
Warren Francis wrote:
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...
There's some 2D code (which could be trivially adapted to 3D) in
PIDDLE/Sping. In the latest Sping download, look in the file pid.py at
the Canvas.curvePoints() method.

http://piddle.sourceforge.net/
Are bezier curves really what I want after all?


I dunno. It depends on how much flexibility you need.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Nov 22 '05 #2
Warren Francis wrote:
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...
There's some 2D code (which could be trivially adapted to 3D) in
PIDDLE/Sping. In the latest Sping download, look in the file pid.py at
the Canvas.curvePoints() method.

http://piddle.sourceforge.net/
Are bezier curves really what I want after all?


I dunno. It depends on how much flexibility you need.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Nov 22 '05 #3
The Bezier gives control points with natural interpretations and a
nice "within the convex hull" property. I happen to like Beziers to
control curves which are aestheticly, rather than computationally
defined.
--
-Scott David Daniels
sc***********@acm.org
Nov 22 '05 #4
http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm

has a Python example implementation of qubic Bezier curves available.

Claudio

"Warren Francis" <ju*****************@yahoo.com> schrieb im Newsbeitrag
news:dl**********@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

Nov 22 '05 #5
> 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),(10 0,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" <ju*****************@yahoo.com> schrieb im Newsbeitrag
news:dl**********@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


Nov 22 '05 #6
For my purposes, I think you're right about the natural cubic splines.
Guaranteeing that an object passes through an exact point in space will be
more immediately useful than trying to create rules governing where control
points ought to be placed so that the object passes close enough to where I
intended it to go. Thanks for the insight, I never would have found that on
my own. At least not until Google labs comes out with a search engine that
gives names for what you're thinking of. ;-)

I know this is a fairly pitiful request, since it just involves parsing your
code, but I'm new enough to this that I'd benefit greatly from an couple of
lines of example code, implementing your classes... how do I go from a set
of coordinates to a Natural Cubic Spline, using your python code?

Thanks for all the help, everybody!

Warren

"Tom Anderson" <tw**@urchin.earth.li> wrote in message
news:Pi*******************************@urchin.eart h.li...
On Mon, 21 Nov 2005, Tom Anderson wrote:
On Sun, 20 Nov 2005, Warren Francis wrote:
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?


No. You want a natural cubic spline:


In a fit of code fury (a short fit - this is python, so it didn't take
long), i ported my old java code to python, and tidied it up a bit in the
process:

http://urchin.earth.li/~twic/splines.py

That gives you a natural cubic spline, plus my blended quadratic spline,
and a framework for implementing other kinds of splines.

tom

--
Gin makes a man mean; let's booze up and riot!

Nov 22 '05 #7
On Tue, 22 Nov 2005, Warren Francis wrote:
For my purposes, I think you're right about the natural cubic splines.
Guaranteeing that an object passes through an exact point in space will
be more immediately useful than trying to create rules governing where
control points ought to be placed so that the object passes close enough
to where I intended it to go.
Right so. I wrote that code the first time when i was in a similar spot
myself - trying to draw maps with nice smooth roads etc based on a fairly
sparse set of points - so i felt your pain.
Thanks for the insight, I never would have found that on my own. At
least not until Google labs comes out with a search engine that gives
names for what you're thinking of. ;-)
You're in for a wait - i think that feature's scheduled for summer 2006.
I know this is a fairly pitiful request, since it just involves parsing
your code, but I'm new enough to this that I'd benefit greatly from an
couple of lines of example code, implementing your classes... how do I
go from a set of coordinates to a Natural Cubic Spline, using your
python code?


Pitiful but legit - i haven't documented that code at all well. If you go
right to the foot of my code, you'll find a simple test routine, which
shows you the skeleton of how to drive the code. It looks a bit like this
(this is slightly simplified):

def test_spline():
knots = [(0, 0), (0, 1), (1, 0), (0, -2), (-3, 0)] # a spiral
trace = []
c = NaturalCubicSpline(tuples2points(knots))
u = 0.0
du = 0.1
lim = len(c) + du
while (u < lim):
p = c(u)
trace.append(tuple(p))
u = u + du
return trace

tuples2points is a helper function which turns your coordinates from a
list of tuples (really, an iterable of length-2 iterables) to a list of
Points. The alternative way of doing it is something like:

curve = NaturalCubicSpline()
for x, y in knot_coords:
curve.knots.append(Point(x, y))
do_something_with(curve)

tom

--
I DO IT WRONG!!!
Nov 23 '05 #8
> If you go right to the foot of my code, you'll find a simple test routine,
which shows you the skeleton of how to drive the code.


Oops... my request just got that much more pitiful. :-) Thanks for the
help.

Warren
Nov 23 '05 #9
Warren Francis wrote:
For my purposes, I think you're right about the natural cubic splines.
Guaranteeing that an object passes through an exact point in space will be
more immediately useful than trying to create rules governing where control
points ought to be placed so that the object passes close enough to where I
intended it to go. Thanks for the insight, I never would have found that on
my own. At least not until Google labs comes out with a search engine that
gives names for what you're thinking of. ;-)

I know this is a fairly pitiful request, since it just involves parsing your
code, but I'm new enough to this that I'd benefit greatly from an couple of
lines of example code, implementing your classes... how do I go from a set
of coordinates to a Natural Cubic Spline, using your python code?

Thanks for all the help, everybody!

Warren

"Tom Anderson" <tw**@urchin.earth.li> wrote in message
news:Pi*******************************@urchin.eart h.li...
On Mon, 21 Nov 2005, Tom Anderson wrote:

On Sun, 20 Nov 2005, Warren Francis wrote:
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?

No. You want a natural cubic spline:


In a fit of code fury (a short fit - this is python, so it didn't take
long), i ported my old java code to python, and tidied it up a bit in the
process:

http://urchin.earth.li/~twic/splines.py

That gives you a natural cubic spline, plus my blended quadratic spline,
and a framework for implementing other kinds of splines.

tom

--
Gin makes a man mean; let's booze up and riot!


Half the points on a cubic Bezier curve are knots -- points through
which the curve must pass. If you have four points for a bezier:
a
d
b c

Then the curve must pass through a and d, and it should be tangent
to a-b at a, and tangent to c-d at d. All parts of the curve must lie
within the convex hull of a-b-c-d (that is, the largest quadrilateral
with those four corners). It is simple to subdivide a bezier curve into
a pair that produce the same curve (so you can cut them nicely). If you
connect two curves together, the tangent requirement is enough to make the
joint look smooth:

a e f
d
b c g

In this diagram, there are two curves: a-b-c-d and d-e-f-g. If c-d-e is
a line, then the connection will look smooth.

This is why it is so easy to work with beziers, lots of properties make
visual sense. In fact I've even used the bezier algorithm to control
not only x, y, and z, but also r, g, and b. Controlling all six, I can
enforce smooth controllable color transitions that I can even tweak to
satisfy my visual aesthetic. The convex hull property guarantees me
that all colors will exit. Knots-only systems go outside the hulls,
so colors don't make as much sense.

--Scott David Daniels
sc***********@acm.org
Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bissatch | last post by:
I am trying to compare two variable but using regular expression: $access = "glasgow" $areaid = "glasgow-westend-byers_rd" using the two variables, I would like the areaid variable to be...
0
by: diablo | last post by:
Hi Anyone know where I can get hold of a script to work out royal mail postage depending on weight? Thanks Kal
2
by: Dennis Myrén | last post by:
Hi. Sorry if this post might be a little off the topic, but just in case anyone knows... Given a size, a position, a start angle and a sweep angle, i want to draw a bezier curve. I will...
0
by: Warren Francis | last post by:
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...
1
by: sunny | last post by:
I am looking for C source code that can find the area of maximum square that can fit in butterfly curves.I am struck with project because of this. I would greatly appreciate if some one can help...
2
by: pavanholla | last post by:
Well ive just written a program that plots algaebraic curves of any degree .Please download the code and modify it 4 da better.Im just an amateur programmer in class12 so bear with me for any bugs. ...
1
by: ibnsahl_mail | last post by:
Do you want really to know the happiness road ? Visit : www.sultan.org
2
by: Peter Morris | last post by:
Hi all I want to draw a line as if it were being drawn in real-time by a person. To save lots of data I think that storing 4 points in memory and drawing a bezier curve would be efficient. What...
3
by: gnawz | last post by:
Hi Any body knows how to create curves and graphs from PHP/MySQL reports? Is there a tool/script that I could use?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.