Recent

Author Topic: Weaving/Tangled Rings: Help needed for CSharp Conversion (Partially Working)  (Read 915 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 1001
Three Weaving/Tangled Rings

Now how to do more rings dynamically?

Also resize?
« Last Edit: September 10, 2025, 02:59:23 pm by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 1001
Re: Three Weaving/Tangled Rings (How to do more? and resize?)
« Reply #1 on: September 05, 2025, 05:34:36 pm »
Found a CSharp program that does it brilliantly.


Now how to port it to Lazarus?

gidesa

  • Full Member
  • ***
  • Posts: 196
Re: Three Weaving/Tangled Rings (How to do more? and resize?)
« Reply #2 on: September 07, 2025, 05:56:27 pm »
Use an online converter as this:
https://www.codeconvert.ai/free-converter
Maybe divide C# program in single function/procedures, and convert one by one.
For example, this fragment from file Circle.cs (note that the service has a limit of 4000 characters):
Code: C#  [Select][+][-]
  1. class Circle
  2.     {
  3.         public PointF Center;
  4.         public float Radius, CircleThickness, OutlineThickness;
  5.         public Color FillColor, OutlineColor;
  6.         public List<Poi> Pois = new List<Poi>();
  7.         public List<float> Angles = new List<float>();
  8.         public int NumAssigned = 0;
  9.  
  10.         public Circle(PointF center, float radius,
  11.             float circle_thickness, float outline_thickness,
  12.             Color fill_color, Color outline_color)
  13.         {
  14.             Center = center;
  15.             Radius = radius;
  16.             CircleThickness = circle_thickness;
  17.             OutlineThickness = outline_thickness;
  18.             FillColor = fill_color;
  19.             OutlineColor = outline_color;
  20.  
  21.         }
  22.  
  23.         // Return the circle's center.
  24.         public override string ToString()
  25.         {
  26.             return string.Format("{0}, {1}", Center.X, Center.Y);
  27.         }
  28.  
  29.         // Draw the circle.
  30.         public void Draw(Graphics gr)
  31.         {
  32.             using (Pen fill_pen = new Pen(FillColor, CircleThickness))
  33.             {
  34.                 using (Pen outline_pen = new Pen(OutlineColor, OutlineThickness))
  35.                 {
  36.                     gr.DrawThickArc(Center, Radius, 0, 360, fill_pen, outline_pen);
  37.                 }
  38.             }
  39.         }

is converted to Delphi:

Code: Pascal  [Select][+][-]
  1. unit CircleUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Generics.Collections, Vcl.Graphics;
  7.  
  8. type
  9.   TCircle = class
  10.   private
  11.     FCenter: TPointF;
  12.     FRadius: Single;
  13.     FCircleThickness: Single;
  14.     FOutlineThickness: Single;
  15.     FFillColor: TColor;
  16.     FOutlineColor: TColor;
  17.     FPois: TList<Poi>;
  18.     FAngles: TList<Single>;
  19.     FNumAssigned: Integer;
  20.   public
  21.     constructor Create(center: TPointF; radius, circle_thickness, outline_thickness: Single;
  22.       fill_color, outline_color: TColor);
  23.     destructor Destroy; override;
  24.  
  25.     function ToString: string; override;
  26.     procedure Draw(gr: TCanvas);
  27.   end;
  28.  
  29. implementation
  30.  
  31. constructor TCircle.Create(center: TPointF; radius, circle_thickness, outline_thickness: Single;
  32.   fill_color, outline_color: TColor);
  33. begin
  34.   FCenter := center;
  35.   FRadius := radius;
  36.   FCircleThickness := circle_thickness;
  37.   FOutlineThickness := outline_thickness;
  38.   FFillColor := fill_color;
  39.   FOutlineColor := outline_color;
  40.   FPois := TList<Poi>.Create;
  41.   FAngles := TList<Single>.Create;
  42.   FNumAssigned := 0;
  43. end;
  44.  
  45. destructor TCircle.Destroy;
  46. begin
  47.   FPois.Free;
  48.   FAngles.Free;
  49.   inherited;
  50. end;
  51.  
  52. function TCircle.ToString: string;
  53. begin
  54.   Result := Format('%f, %f', [FCenter.X, FCenter.Y]);
  55. end;
  56.  
  57. procedure TCircle.Draw(gr: TCanvas);
  58. var
  59.   fill_pen: TPen;
  60.   outline_pen: TPen;
  61. begin
  62.   fill_pen := TPen.Create;
  63.   outline_pen := TPen.Create;
  64.   try
  65.     fill_pen.Color := FFillColor;
  66.     fill_pen.Width := Round(FCircleThickness);
  67.     outline_pen.Color := FOutlineColor;
  68.     outline_pen.Width := Round(FOutlineThickness);
  69.     gr.Pen := outline_pen;
  70.     gr.Brush.Color := FFillColor;
  71.     gr.Ellipse(FCenter.X - FRadius, FCenter.Y - FRadius,
  72.                FCenter.X + FRadius, FCenter.Y + FRadius);
  73.   finally
  74.     fill_pen.Free;
  75.     outline_pen.Free;
  76.   end;
  77. end;
  78.  
  79. end.
  80.  

Not too bad.
« Last Edit: September 07, 2025, 06:11:09 pm by gidesa »

Boleeman

  • Hero Member
  • *****
  • Posts: 1001
Re: Three Weaving/Tangled Rings (How to do more? and resize?)
« Reply #3 on: September 10, 2025, 02:38:17 pm »
Thanks for that idea gidesa.

I gave it a go but its partially working.

Some rings are missing or incomplete.

The intersecting bits look correct, and the first drawn ring is always complete.

The problem seems to be at:
procedure TCircle.Assign(unfinished, partially_assigned: specialize TList<TCircle>);
and at
class procedure TCircle.FindPois(circles: array of TCircle);


I tried replacing procedure TCircle.Assign with the CSharp conversion but it always seizes up the form (don't know why)
I am going round in circles trying to get the whole conversion working correctly.

Any help is greatly appreciated.  DZandaa buddy, Pal ?

Code: Pascal  [Select][+][-]
  1. procedure TCircle.Assign(unfinished, partially_assigned: TList<Circle>);
  2. var
  3.   first_assigned, i, index: Integer;
  4.   other: Circle;
  5.   last_was_this: Boolean;
  6. begin
  7.   if (FPois.Count = 0) or AllPoisAreAssigned then
  8.   begin
  9.     partially_assigned.Remove(Self);
  10.     Exit;
  11.   end;
  12.  
  13.   first_assigned := -1;
  14.   for i := 0 to FPois.Count - 1 do
  15.   begin
  16.     if FPois[i].CircleOnTop <> nil then
  17.     begin
  18.       first_assigned := i;
  19.       Break;
  20.     end;
  21.   end;
  22.  
  23.   last_was_this := (Pois[first_assigned].CircleOnTop = Self);
  24.  
  25.   for i := 1 to FPois.Count - 1 do
  26.   begin
  27.     index := (first_assigned + i) mod FPois.Count;
  28.     if FPois[index].CircleOnTop = nil then
  29.     begin
  30.       other := FPois[index].OtherCircle(Self);
  31.       if last_was_this then
  32.         FPois[index].CircleOnTop := other
  33.       else
  34.         FPois[index].CircleOnTop := Self;
  35.  
  36.       if not other.AllPoisAreAssigned and
  37.          (partially_assigned.IndexOf(other) = -1) then
  38.         partially_assigned.Add(other);
  39.  
  40.       if unfinished.IndexOf(other) <> -1 then
  41.         unfinished.Remove(other);
  42.  
  43.       Inc(NumAssigned);
  44.     end;
  45.  
  46.     last_was_this := not last_was_this;
  47.   end;
  48. end;
  49.  
  50. function TCircle.AllPoisAreAssigned: Boolean;
  51. begin
  52.   Result := NumAssigned = Pois.Count;
  53. end;

« Last Edit: September 10, 2025, 03:02:26 pm by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 1001
Partially Working

Boleeman

  • Hero Member
  • *****
  • Posts: 1001
Got some working exactly like csharp version and others not.

Joanna

  • Hero Member
  • *****
  • Posts: 1400
Glad you got it working they look neat  :)

 

TinyPortal © 2005-2018