PDA

View Full Version : quadratic bezier curve



peterbone
31-07-2004, 03:32 PM
I need to draw a quadratic bezier curve (start point, end point and 1 control point) quickly. I could use the Canvas.PolyBezier function, which calls the windows graphics dll, but that's for a cubic bezier (2 control points). I can't duplicate one of my 3 points to make 4 points because that increases the weight of that point making the curve destorted from the quadratic curve.
Is there a windows dll function I could call for drawing a quadratic bezier or is there a clever way I can draw a quadratic bezier using the PolyBezier procedure?

Thanks

Peter

p.s I don't want to write my own procedure because it won't be as optimized as PolyBezier.

Crisp_N_Dry
31-07-2004, 08:36 PM
Having tried out the PolyBezier code for the first time and I see what you mean with the 4 points thing. Haven't been able to try this out but was toying with the idea of using the 2nd control point but having an adjustment algorithm that compensates for the fact that there is 2 control points and puts the 2nd one in an appropriate place. Resulting in a cubic Bezier curve that is the same shape as a quadratic Bezier curve. I am no mathematician so I don't know if this would even work and to work out the algorithm you would probably have to code a function that created a quadratic curve for reference anyway which kinda defeats the object. I will have a proper go at it tomorrow night hopefully. Will keep you posted. Let me know if anyone has any thoughts on this or if you figure it out.

peterbone
31-07-2004, 09:19 PM
Thanks for putting so much time into it just for me! I was just expecting someone to reply if they'd done it before or something.
I had the same idea of moving the controls points position to compensate. Some cubic bezier formulas have a weighting variable for each point - if the PolyBezier function had that it would be easy because you could just give the 2 control points half the weight (but it doesn't). I'm surprised there isn't a funtion for bezier curves of any order in the graphics dll. Maybe they can optimize it a lot more if it's just cubic.

Peter

peterbone
31-07-2004, 09:34 PM
Just found this

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q243/2/85.asp&NoWebContent=1

The MakeBezierFromQBSpline function in the code converts a quadratic to a cubic! It looks like they position the 2 control points 2/3's away from the quadratic control point or something.

Peter

Crisp_N_Dry
01-08-2004, 04:24 PM
Nice one, will have to try that one out.

peterbone
01-08-2004, 09:30 PM
Here's my code
type
TPointFloat = record
X : Single;
Y : Single;
end;

TPointArray = array of TPoint;

// draw a quadratic bezier using the cubic PolyBezier procedure
procedure QuadraticBezier(ACanvas : TCanvas ; P0, P1, P2 : TPointFloat);
Var
LBez : TPointArray;
begin
SetLength(LBez, 4);

// calculate cubic control points so that curve is eqivalent to the quadratic

// cubic P0 is on curve start point
LBez[0].X := Round(P0.X);
LBez[0].Y := Round(P0.Y);

// Cubic P1 in terms of Quadratic P0 and P1
LBez[1].X := Round(P0.X + 2*(P1.X - P0.X)/3);
LBez[1].Y := Round(P0.Y + 2*(P1.Y - P0.Y)/3);

// Cubic P2 in terms of Quadratic P1 and P2
LBez[2].X := Round(P1.X + (P2.X - P1.X)/3);
LBez[2].Y := Round(P1.Y + (P2.Y - P1.Y)/3);

// Cubic P3 is the on curve end point
LBez[3].X := Round(P2.X);
LBez[3].Y := Round(P2.Y);

ACanvas.PolyBezier(LBez);
end;