Author Topic: Tlabel in a circle  (Read 151 times)

SteenJorgensen

  • Jr. Member
  • **
  • Posts: 73
Tlabel in a circle
« on: September 02, 2026, 01:09:26 pm »

There is something from my school days that I have forgotten. Hope someone can help me.

I need to position a number of Tlabels on a Tpanel so that they form a circle with equal distances, like a clock with numbers from 1-12. Here, however, I need many more with letters.

Hope someone can help
----------------------------------------
Lazarus version 4.0 64-bit
FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 3532
Re: Tlabel in a circle
« Reply #1 on: September 02, 2026, 01:50:42 pm »
Use a TShape with ShapeType stCircle.
If you don't need to see the circle, just make it invisible

The rest is classic math to calculate the "anchor"-points on the circle.

You need the radius of the circle (height or width of the TShape / 2)
You need the point of origin of the circle as a TPoint
TShape.Top +TShape.Height/2 = y-coordinate origin
TShape.Left+TShape.Width/2 = x-coordinate origin
You need the angle in between two "markers" --> e.g. 12h clock needs 12 "markers" --> angle = 360° / 12 = 30°

Then you just calculate the anchorpoint on the circle for your TLabels.
Up to you, if you want to position the TopLeft corner of the Label, or whatever else point
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1704
Re: Tlabel in a circle
« Reply #2 on: September 03, 2026, 12:38:09 pm »
I would do it like this: put panel on the form and on form's OnCreate event put this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. const NumberOfLabels = 26;
  3. var i, CenterX, CenterY, Radius: Integer;
  4.     AngleStep, AngleLabelRad: Double;
  5. begin
  6.   SetLength(MyLabels, NumberOfLabels);
  7.   AngleStep := 360 / NumberOfLabels;
  8.  
  9.   CenterX := Panel1.Width div 2; // calculate center
  10.   CenterY := Panel1.Height div 2;
  11.   Radius := Min(Panel1.Width div 2 - 50, Panel1.Height div 2 - 50); // adjust radius as you wish
  12.  
  13.   For i := 0 to NumberOfLabels - 1 do
  14.   begin
  15.     // create label
  16.     MyLabels[i] := TLabel.Create(Form1);
  17.     MyLabels[i].Parent := Panel1;
  18.     MyLabels[i].Name := 'MyLabel' + IntToStr(i + 1);
  19.  
  20.     // set caption and calculate position
  21.     MyLabels[i].Caption := Chr(i + 65);
  22.     AngleLabelRad := DegToRad((i * AngleStep) - 180); // -180 start left, -90 start top, -0 start right, +90 start bottom
  23.     MyLabels[i].Left := CenterX + Round(Radius * Cos(AngleLabelRad));
  24.     MyLabels[i].Top := CenterY + Round(Radius * Sin(AngleLabelRad));
  25.   end;
  26. end;

Zvoni

  • Hero Member
  • *****
  • Posts: 3532
Re: Tlabel in a circle
« Reply #3 on: September 03, 2026, 12:46:57 pm »
dseligo,

this is for when the TPanel fills out the whole of the Client-Area of the Form? (haven't looked at your code).

I can never remember if Coordinates on a Form are relative to its container or to the client-area of the form
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1704
Re: Tlabel in a circle
« Reply #4 on: September 03, 2026, 01:41:44 pm »
dseligo,

this is for when the TPanel fills out the whole of the Client-Area of the Form? (haven't looked at your code).

Panel doesn't have to fill whole form, you can change it's size and position and the code will work. If TS wants to change position/size of the panel in the runtime then coordinates of labels have to be calculated again. But that's easy to change.

Quote
I can never remember if Coordinates on a Form are relative to its container or to the client-area of the form

Same here.

 

TinyPortal © 2005-2018