Sign in Join
Wiki copytemporary

BGRABitmap tutorial 7: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11This tutorial shows how to use splines. A sample project can be found in [[BGRABitmap AggPas]].
22
33### Create a new project
44
55Create a new project and add a reference to [[BGRABitmap]], the same way as in [[BGRABitmap tutorial|the first tutorial]].
66
77### Draw an opened spline
88
99With the object inspector, add an OnPaint handler and write:
1010
1111``` pascal
1212procedure TForm1.FormPaint(Sender: TObject);
1313var
1414 image: TBGRABitmap;
1515 pts: array of TPointF;
1616 storedSpline: array of TPointF;
1717 c: TBGRAPixel;
1818
1919begin
2020 image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
2121 c := ColorToBGRA(ColorToRGB(clWindowText));
2222
2323 //rectangular polyline
2424 setlength(pts,4);
2525 pts[0] := PointF(50,50);
2626 pts[1] := PointF(150,50);
2727 pts[2] := PointF(150,150);
2828 pts[3] := PointF(50,150);
2929 image.DrawPolylineAntialias(pts,BGRA(255,0,0,150),1);
3030
3131 //compute spline points and draw as a polyline
3232 storedSpline := image.ComputeOpenedSpline(pts,ssVertexToSide);
3333 image.DrawPolylineAntialias(storedSpline,c,1);
3434
3535 image.Draw(Canvas,0,0,True);
3636 image.free;
3737end;
3838```
3939
4040There 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.
4141
4242### Draw a closed spline
4343
4444Before image.Draw, add these lines:
4545
4646``` pascal
4747 for i := 0 to 3 do
4848 pts[i].x += 200;
4949 image.DrawPolylineAntialias(pts,BGRA(255,0,0,150),1);
5050
5151 storedSpline := image.ComputeClosedSpline(pts,ssVertexToSide);
5252 image.DrawPolygonAntialias(storedSpline,c,1);
5353```
5454
5555Go 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.
5656
5757Two new lines draw a closed spline. Notice the specific function that computes closed splines and the call to DrawPolygonAntialias.
5858
5959You can avoid using a variable to store spline points like this:
6060
6161``` pascal
6262image.DrawPolygonAntialias(image.ComputeClosedSpline(pts),c,1);
6363```
6464
6565However, if you do so, you cannot use the computed points more than once, they must be calculated each time you use them.
6666
6767### Run the program
6868
6969This should draw two splines, one opened on the left and one closed on the right.
7070
71-<figure>
72-<img src="BGRATutorial7.png" title="BGRATutorial7.png" />
73-<figcaption>BGRATutorial7.png</figcaption>
74-</figure>
71+![](/wiki-images/8/84/BGRATutorial7.png)
7572
7673Notice 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.
7774
7875### Using Bézier curves
7976
8077Before image.Draw, add these lines:
8178
8279``` pascal
8380 storedSpline := image.ComputeBezierSpline([BezierCurve(PointF(50,50),PointF(150,50),PointF(150,100)),
8481 BezierCurve(PointF(150,100),PointF(150,150),PointF(50,150))]);
8582 image.DrawPolylineAntialias(storedSpline,c,2);
8683```
8784
8885The function BezierCurve defines a curve with an origin and a destination, and one or two control points. Here there is only one control point. Here the control points are defined so that the curve be tangeant to the rectangle defined before.
8986
9087A Bézier spline is simple a series of Bézier curve. Thus, the function ComputeBezierSpline concatenates an array of Bézier curves. Here, we construct a nice U-turn with two curves.
9188
9289### Run the program
9390
9491You should see a bold Bézier curve inside the left rectangle.
9592
96-<figure>
97-<img src="BGRATutorial7b.png" title="BGRATutorial7b.png" />
98-<figcaption>BGRATutorial7b.png</figcaption>
99-</figure>
93+![](/wiki-images/a/aa/BGRATutorial7b.png)
10094
10195[[BGRABitmap tutorial 6|Previous tutorial (line style)]] [[BGRABitmap tutorial 8|Next tutorial (textures)]]
10296
10397---
10498*From the Free Pascal wiki, page "BGRABitmap tutorial 7", as it stood on 2023-05-20 after a change by Circular, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
10599Category: BGRABitmap
106100Category: Graphics

Back to history