Difference between revisions of "BGRABitmap tutorial 7"

From Free Pascal wiki
Jump to navigationJump to search
(→‎Run the program: description)
Line 60: Line 60:
  
 
[[Image:BGRATutorial7.png]]
 
[[Image:BGRATutorial7.png]]
 +
 +
Notice that the spline goes through each point. If you want the curve to stay inside or define tangeants, you need to use control points, which are available in Bézier curves.
 +
 +
=== Using Bézier curves ===
 +
 +
<delphi>
 +
    storedSpline := image.ComputeBezierSpline([BezierCurve(PointF(50,50),PointF(150,50),PointF(150,100)),
 +
                                              BezierCurve(PointF(150,100),PointF(150,150),PointF(50,150))]);
 +
    image.DrawPolylineAntialias(storedSpline,c,2);
 +
</delphi>
  
 
[[BGRABitmap tutorial 6|Previous tutorial (line style)]]
 
[[BGRABitmap tutorial 6|Previous tutorial (line style)]]
  
 
[[Category:Graphics]]
 
[[Category:Graphics]]

Revision as of 13:38, 1 April 2011

This tutorial shows how to use splines.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Draw an opened spline

With the object inspector, add an OnPaint handler and write : <delphi>procedure TForm1.FormPaint(Sender: TObject); var

 image: TBGRABitmap;
 pts: array of TPointF;
 storedSpline: array of TPointF;
 c: TBGRAPixel;

begin

   image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
   c := ColorToBGRA(ColorToRGB(clWindowText));
   //rectangular polyline
   setlength(pts,4);
   pts[0] := PointF(50,50);
   pts[1] := PointF(150,50);
   pts[2] := PointF(150,150);
   pts[3] := PointF(50,150);
   image.DrawPolylineAntialias(pts,BGRA(255,0,0,150),1);
   //compute spline points and draw as a polyline
   storedSpline := image.ComputeOpenedSpline(pts);
   image.DrawPolylineAntialias(storedSpline,c,1);
   image.Draw(Canvas,0,0,True);
   image.free;  

end;</delphi>

There are two lines that draw the spline. The first line computes the spline points, and the second draw them. Notice that it is a specific function for opened splines.

Draw a closed spline

Before image.Draw, add these lines : <delphi> for i := 0 to 3 do

     pts[i].x += 200;
   image.DrawPolylineAntialias(pts,BGRA(255,0,0,150),1);
   storedSpline := image.ComputeClosedSpline(pts);
   image.DrawPolygonAntialias(storedSpline,c,1);</delphi>

Go with the text cursor on the 'i' identifier and press Ctrl-Shift-C to add the variable declaration. The loop offsets the points to the right.

Two new lines draw a closed spline. Notice the specific function that computes closed splines and the call to DrawPolygonAntialias.

You can avoid using a variable to store spline points like this : <delphi>image.DrawPolygonAntialias(image.ComputeClosedSpline(pts),c,1);</delphi> However, if you do so, you cannot use the computed points more than once, they must be calculated each time you use them.

Run the program

This should draw two splines, one opened on the left and one closed on the right.

BGRATutorial7.png

Notice that the spline goes through each point. If you want the curve to stay inside or define tangeants, you need to use control points, which are available in Bézier curves.

Using Bézier curves

<delphi>

   storedSpline := image.ComputeBezierSpline([BezierCurve(PointF(50,50),PointF(150,50),PointF(150,100)),
                                              BezierCurve(PointF(150,100),PointF(150,150),PointF(50,150))]);
   image.DrawPolylineAntialias(storedSpline,c,2); 

</delphi>

Previous tutorial (line style)