Recent

Author Topic: [solved] Trouble with TLazCanvas.Arc  (Read 614 times)

Valdas

  • New Member
  • *
  • Posts: 49
[solved] Trouble with TLazCanvas.Arc
« on: March 16, 2023, 07:23:42 pm »
Hello,

I' trying to adopt an example from the this post to produce an Arc.
 Instead of
Code: Pascal  [Select][+][-]
  1. cnv.Ellipse(0, 0, BITMAP_SIZE, BITMAP_SIZE);
I'm using
Code: Pascal  [Select][+][-]
  1. cnv.Arc(0, 0, 20, 20, 0, 180 * 16);
But I'm not getting the Arc, only a green field.
 Am I missing something?
« Last Edit: March 17, 2023, 07:00:31 am by Valdas »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Trouble with TLazCanvas.Arc
« Reply #1 on: March 16, 2023, 08:12:52 pm »
Can you show your code so that I can reproduce it?

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Trouble with TLazCanvas.Arc
« Reply #2 on: March 16, 2023, 08:53:23 pm »
I see: TLazCanvas does not implement its Arc method, it inherites the one from TFpCustomCanvas, and this does - nothing...

Here is a quick-and-dirty procedure which you can call instead. It draws the arc as short line segments; their angular length is 5 degree - this is not optimized: for very small arcs you can increase the value because many points will be overlapping; and for large arcs you must decrease the value if you can distinguish the individual straight-line segments in the curve. (Ideally the procedure should be tuned to determine the correct value by itself).
Code: Pascal  [Select][+][-]
  1. uses
  2.   Math;
  3. procedure Arc(ACanvas: TLazCanvas; ALeft, ATop, ARight, ABottom: Integer;
  4.   AStartDeg, ASweepDeg: Double);
  5. var
  6.   a, b: Double;
  7.   phi, phi_end, dphi: Double;
  8.   sinphi, cosphi: Double;
  9.   x, y: Integer;
  10.   Center: TPoint;
  11.   done: Boolean = false;
  12. begin
  13.   a := (ARight - ALeft) / 2;  // major ellipse axis
  14.   b := (ABottom - ATop) / 2;  // minor ellipse axis
  15.   Center := Point((ALeft + ARight) div 2, (ATop + ABottom) div 2);
  16.   dphi := DegToRad(5);  // 5-degree steps, not optimized
  17.   if ASweepDeg < 0 then dphi := -dphi;
  18.   phi := DegToRad(AStartDeg);
  19.   phi_end := DegToRad(AStartDeg + ASweepDeg);
  20.   sincos(phi, sinphi, cosphi);
  21.   x := Center.X + round(cosphi * a);
  22.   y := Center.Y - round(sinphi * b);
  23.   ACanvas.MoveTo(x, y);
  24.   repeat
  25.     phi := phi + dphi;
  26.     if ((ASweepDeg > 0) and (phi > phi_end)) or ((ASweepDeg < 0) and (phi < phi_end)) then
  27.     begin
  28.       phi := phi_end;
  29.       done := true;
  30.     end;
  31.     sincos(phi, sinphi, cosphi);
  32.     x := Center.X + round(cosphi * a);
  33.     y := Center.Y - round(sinphi * b);
  34.     ACanvas.LineTo(x, y);
  35.   until done;
  36. end;
  37.  

ALeft, ATop, ARight, ABottom define the rectangle enclosing the *full* ellipse from which the arc is cut. AStartDeg is the start angle of the arc, in degrees, measured from the x axis in counter-clockwise direction. ASweepDeg is the "length" of the arg given in degrees also. If positive the arc runs in counter-clockwise direction, otherwise in clockwise direction. The following sample draws a rounded rectangle:

Code: Pascal  [Select][+][-]
  1.   Arc(cnv, 0, 0, 30, 30, 180, -90);
  2.   cnv.Line(15, 0, img.Width-15, 0);
  3.   Arc(cnv, img.Width-30, 0, img.Width-1, 30, 0, 90);
  4.   cnv.Line(img.Width-1, 15, img.Width-1, img.Height-15);
  5.   Arc(cnv, img.Width-30, img.Height-30, img.Width-1, img.Height-1, 0, -90);
  6.   cnv.Line(img.Width-15, img.Height-1, 15, img.Height-1);
  7.   Arc(cnv, 0, img.Height-30, 30, img.Height-1, -90, -90);
  8.   cnv.Line(0, img.Height-15, 0, 15);
« Last Edit: March 16, 2023, 10:12:33 pm by wp »

Valdas

  • New Member
  • *
  • Posts: 49
Re: Trouble with TLazCanvas.Arc
« Reply #3 on: March 17, 2023, 06:59:57 am »
Thank you!

 

TinyPortal © 2005-2018