Recent

Author Topic: Bezier curves  (Read 2357 times)

user5

  • Sr. Member
  • ****
  • Posts: 419
Bezier curves
« on: June 12, 2023, 03:47:38 pm »
    The code below works fine but is there a way to do the same thing without the antialiasing? I could use something other than BGRABitmap since speed in this case is not important.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   image: TBGRABitmap;
  4.   pts: array of TPointF;
  5.   storedSpline: array of TPointF;
  6.   c: TBGRAPixel;
  7. begin
  8.  
  9.  image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
  10.  c := ColorToBGRA(ColorToRGB(clWindowText));
  11.  
  12.  setlength(pts,4);
  13.  pts[0] := PointF(50,50);
  14.  pts[1] := PointF(150,50);
  15.  pts[2] := PointF(150,150);
  16.  pts[3] := PointF(50,150);
  17.  image.DrawPolylineAntialias(pts,BGRA(255,0,0,150),1);
  18.  storedSpline := image.ComputeOpenedSpline(pts,ssVertexToSide);
  19.  //image.DrawPolylineAntialias(storedSpline,c,1);
  20.  storedSpline := image.ComputeBezierSpline([BezierCurve(PointF(50,50),PointF(150,50),PointF(150,100)),
  21.                                             BezierCurve(PointF(150,100),PointF(150,150),PointF(50,150))]);
  22.  image.DrawPolylineAntialias(storedSpline,c,2);
  23.  image.Draw(Canvas,0,0,True);
  24.  image.free;
  25.  
  26. end;


wp

  • Hero Member
  • *****
  • Posts: 13578
Re: Bezier curves
« Reply #1 on: June 12, 2023, 10:46:00 pm »
I could use something other than BGRABitmap since speed in this case is not important.
Use the PolyBezier of the standard LCL canvas: https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.polybezier.html. Simple demo attached.

Code: Pascal  [Select][+][-]
  1.   Points[0] := Point(10, 10);
  2.   Points[1] := Point(70, 10);
  3.   Points[2] := Point(100, 100-70);
  4.   Points[3] := Point(100, 100);
  5.   Points[4] := Point(100, 100+70);
  6.   Points[5] := Point(200-70, 200);
  7.   Points[6] := Point(200, 200);
  8.  
  9.   Paintbox1.Canvas.Pen.Color := clRed;
  10.   Paintbox1.Canvas.Pen.Style := psSolid;
  11.   Paintbox1.Canvas.PolyBezier(Points);  
« Last Edit: June 12, 2023, 10:48:40 pm by wp »

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: Bezier curves
« Reply #2 on: June 13, 2023, 03:55:31 am »
    Thank you wp for the example you posted.
    It's exactly what I need.

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: Bezier curves
« Reply #3 on: June 13, 2023, 06:00:56 am »
    If you happen to see this note wp, can you tell what or where PaintBox1Paint is called or invalidated.
    There is no Form1.show so in your example I can't figure out how the already painted paintbox appears
automatically when the form is opened. Thanks again.

wp

  • Hero Member
  • *****
  • Posts: 13578
Re: Bezier curves
« Reply #4 on: June 13, 2023, 10:39:20 am »
PaintBox1Paint is the handler for the OnPaint event of the paintbox. TPaintbox is very "stupid", it has no built-in way how to react when the OS requests it to repaint itself. Therefore the user must provide a handler for the OnPaint event which is executed in this case. When the form ist shown for the first time, the paintbox is requested to draw itself - and thus executes the user's OnPaint code.

When you put your drawing code in the OnClick event of a button it could be that the drawing appears in the paintbox, at least on Windows, but it will not be persistent: Drag the form partly out of the screen and then back into the screen again, and you'll see that part of the drawing is erased. This is because the paintbox does not know how to redraw itself if OnPaint is empty - the button's OnClick code is unreachable for the paintbox.

The button's OnClick code is correct only when it draws into a bitmap (similar to what you do with BGRABitmap, but a standard TBitmap will work as well) which is displayed in a TImage. A TImage by default "knows" how to redraw itself - namely to draw the bitmap assigned to it. Unlike a TPaintbox where you would need an OnPaint handler again to draw the bitmap on the control.

In order to assign code to the OnPaint event (or any other event) double-click on the event in the object inspector. The IDE creates an empty procedure skeleton for you in the code editor which you must complete with your own code.

user5

  • Sr. Member
  • ****
  • Posts: 419
Re: Bezier curves
« Reply #5 on: June 14, 2023, 07:40:01 am »
        Thank you for taking the time and effort to help me wp. You really know your stuff. Wow.

 

TinyPortal © 2005-2018