[comp.lang.postscript] Bezier Curve source required

jos@idca.tds.PHILIPS.nl (Jos Vos) (01/23/89)

In article <30936@vax1.tcd.ie> ambarry@vax1.tcd.ie writes:

>	Has anyone got pascal source code, for Bezier curves? I recently
>recieved the database for a goblet, but the writing of a program to plot the
>points is beyond me.

A related question (I think it's just the inverted algorithm...):

I'm looking for a program that calculates the so called
Bezier cubic control points when giving a number of points 
that should be placed in the resulting Bezier cubic section.

I prefer C code, but any other code is welcome too.

[ I want to use it for generating PostScript code for a certain figure,
  without using packages like Adobe Illustrator. ]

-- 
-- ######   Jos Vos   ######   Internet   jos@idca.tds.philips.nl   ######
-- ######             ######   UUCP         ...!mcvax!philapd!jos   ######

rcd@ico.ISC.COM (Dick Dunn) (01/26/89)

> >	Has anyone got pascal source code, for Bezier curves? I recently
> >recieved the database for a goblet, but the writing of a program to plot the
> >points is beyond me.

I don't have the code written out and tested (well, I do, but it's in C and
assembly language for some obscure hardware:-), but it turns out to be
quite simple.  Call the four control points of the curve A, B, C, and D in
order (i.e., A and D are the endpoints).  The algorithm requires bisecting
segments--if we denote coordinates of points as A=(Ax,Ay), B=(Bx,By) then
define		Bisect(A,B) = ((Ax+Bx)/2, (Ay+By)/2)
To get the points you need to plot, use a recursive algorithm which splits
the curve until the segments are small enough or the angle between them is
small enough.  One step goes like this:
	First bisect each of the three segments from taking control points
	in order:
		P1 = Bisect(A,B)
		P2 = Bisect(B,C)
		P3 = Bisect(C,D)
	Now bisect each of the new segments formed by these:
		Q1 = Bisect(P1,P2)
		Q2 = Bisect(P2,P3)
	Finally, bisect that segment:
		R = Bisect(Q1,Q2)
You now have two Bezier curves, each of which is shorter and flatter than
the original.  The control points for these curves are A-P1-Q1-R and
R-Q2-P3-D.  R lies on the curve.  You can repeat the algorithm with each of
the subcurves, or if you've gotten things down far enough, just draw
segments AR and RD.

> A related question (I think it's just the inverted algorithm...):
> 
> I'm looking for a program that calculates the so called
> Bezier cubic control points when giving a number of points 
> that should be placed in the resulting Bezier cubic section.

This is a rather harder problem.  Now, at first blush you might think that
if you had the endpoints plus two points on the curve, you should be able
to define the curve uniquely--after all, there are four control points, so
it seems like the same number of degrees of freedom, but not so.  Anyway,
if you have a bunch of points, you're probably going to need to do a fit of
the curve which minimizes some error criterion.  I fiddled around with this
for a little while, stumbling over the problem of trying to get a useful
error criterion given the parametric representation...and eventually
decided that I was too lazy to work it through, but I *would* like to see
how it is done.
-- 
Dick Dunn      UUCP: {ncar,nbires}!ico!rcd           (303)449-2870
   ...A friend of the devil is a friend of mine.

ath@helios.prosys.se (Anders Thulin) (01/31/89)

In article <13960@ico.ISC.COM> rcd@ico.ISC.COM (Dick Dunn) writes:
>
>> A related question (I think it's just the inverted algorithm...):
>> 
>> I'm looking for a program that calculates the so called
>> Bezier cubic control points when giving a number of points 
>> that should be placed in the resulting Bezier cubic section.
>
> [...] I
>decided that I was too lazy to work it through, but I *would* like to see
>how it is done.

I think that something along these lines is done in MetaFont. Check
out Computers and Typesetting, vol IV by Knuth.
-- 
Anders Thulin			INET : ath@prosys.se
ProgramSystem AB		UUCP : ...!{uunet,mcvax}!enea!prosys!ath
Teknikringen 2A			PHONE: +46 (0)13 21 40 40
S-583 30 Linkoping, Sweden	FAX  : +46 (0)13 21 36 35