### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Truchet Tiling Curve: Some Extra Settings  (Read 1113 times)

#### Boleeman

• Hero Member
• Posts: 524
##### Truchet Tiling Curve: Some Extra Settings
« on: May 25, 2024, 03:50:55 pm »
Thanks Dzandaa for getting the tiling working. Much Appreciated.

Hi All.

I was trying to create a Truchet Tiling Curve from some VB6 code, but I can't seem to get the four 1/4 of a circle sized arcs to work correctly in Lazarus. Any help from someone with a sound knowledge of Lazarus Arc creation would be greatly appreciated.

Attached is a png picture of how the arcs in the tiles should be orientated, as well as the expected Truchet Tiling Curve.

I have used this code to create the arcs in Lazarus but obviously it is incorrect. Cant seem to those ARCS right.

Code: Pascal  [Select][+][-]
1.       begin
2.         // Draw quarter circles in opposing corners
3.         ACanvas.Arc(Round(X), Round(Y), Round(X + WID), Round(Y + WID),
5.         ACanvas.Arc(Round(X + WID), Round(Y + WID), Round(X), Round(Y),
6.           Round(X + WID), Round(Y + RADIUS), Round(X + RADIUS), Round(Y + WID));
7.       end
8.       else
9.       begin
10.         ACanvas.Arc(Round(X), Round(Y), Round(X + WID), Round(Y + WID),
12.         ACanvas.Arc(Round(X), Round(Y + WID), Round(X + WID), Round(Y),
14.       end;
« Last Edit: June 04, 2024, 01:28:47 am by Boleeman »

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Problem Orientating the four 1/4 ARCS
« Reply #1 on: May 26, 2024, 12:10:08 pm »
Now I am getting the pattern shown in the attached png.

Still not correct, but an interesting pattern.

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Problem Orientating the four 1/4 ARCS
« Reply #2 on: May 26, 2024, 12:35:26 pm »
Expected curve still not happening. Ahhhhh, the bug is where ?

Worked out how to do the arcs, by lots of experimenting.

Now I have the Arcs paired up correctly, but the final curve is not what is expected.
What could be wrong now?

Code: Pascal  [Select][+][-]
1.  procedure TForm1.DrawPicture(ACanvas: TCanvas);
2. var
3.   r, c, max_r, max_c: Integer;
4.   X, Y: Single;
5. begin
6.   ACanvas.Brush.Color := clWhite;
7.   ACanvas.FillRect(0, 0, PaintBox1.Width, PaintBox1.Height);
8.
9.   max_c := PaintBox1.Width div WID + 1;
10.   max_r := PaintBox1.Height div WID + 1;
11.   Y := 0;
12.   for r := 0 to max_r do
13.   begin
14.     X := 0;
15.     for c := 0 to max_c do
16.     begin
17.       if Random > 0.5 then
18.
19.       begin
20.            ACanvas.Pen.Width:=2;
21.                 // Draw quarter circles in opposing corners
22.
23.           // Draw Top Left quarter circle
26.
27.         // Draw bottom-right quarter circle
30.
31.       end
32.       else
33.       begin
34.             ACanvas.Pen.Width:=2;
35.                     // Draw bottom-left quarter circle
36.
39.
40.
41.      // Draw top-right quarter circle
44.
45.       end;
46.       X := X + WID;
47.     end;
48.     Y := Y + WID;
49.   end;
50.
51.   if chkFillIt.Checked then
52.   begin
53.     // Flood areas
55.     for r := 0 to max_r do
56.     begin
58.       for c := 0 to max_c do
59.       begin
60.         if ACanvas.Pixels[Round(X), Round(Y)] = clWhite then
61.           FloodAtPoint(ACanvas, X, Y);
62.         X := X + WID;
63.       end;
64.       Y := Y + WID;
65.     end;
66.   end;
67. end;

Final curve should be smooth and round, as shown in the last attached png.
« Last Edit: May 26, 2024, 12:43:14 pm by Boleeman »

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Problem Orientating the four 1/4 ARCS
« Reply #3 on: May 26, 2024, 02:15:52 pm »
I was analyzing the pattern and it seems sometimes there are 4 arcs drawn in the one tile position and other times there are no arcs drawn in a tile position.

2 arcs should always be drawn in a tile position, selecting from 2 possibilities.

Somehow the tiling of the pairs of arcs is not done consistently, so that there are 2 arcs in every tiling position.

See attached png to see what I mean.

I did a manual copy and paste test of the pairs of arcs in MsPaint and was getting the required curve.
So that means somewhere the program is messing up when tiling the 2 pairs of arcs. Sometimes 4 arcs are placed in one tiling position and other times there are no arcs, and at other times there are 2 arcs in one tiling position (correct placement).

Could     if Random > 0.5 then     be the problem?
Is there a better way of randomly choosing from 2 arc pair options?
« Last Edit: May 26, 2024, 03:02:16 pm by Boleeman »

#### Dzandaa

• Sr. Member
• Posts: 305
• From C# to Lazarus
##### Re: Truchet Tiling Curve: Problem Orientating the four 1/4 ARCS
« Reply #4 on: June 03, 2024, 03:11:45 pm »
Hi Boleeman,

Here is a solution:

Have a nice day.
B->
Dzandaa

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Problem Orientating the four 1/4 ARCS
« Reply #5 on: June 03, 2024, 09:55:28 pm »
Thanks Dzandaa for that solution.

As always, Very Much Appreciated.

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Thanks Dzandaa for the Solution
« Reply #6 on: June 03, 2024, 11:02:36 pm »
I added a Get rid of any dark fill colours, so that there are only light fill colours.

I was also trying to go a step further by minimizing any diagonally adjacent circles.

See diagram to see what I mean.

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Some Extra Settings
« Reply #7 on: June 04, 2024, 01:30:28 am »
Added some extra settings to DZandaa's version. Limited the colors to lighter ones.

Resize the from and watch the magic.

Wondered how to antialiase the Arcs using BgraBMP ? (I tried but failed)
« Last Edit: June 04, 2024, 04:07:24 am by Boleeman »

#### Dzandaa

• Sr. Member
• Posts: 305
• From C# to Lazarus
##### Re: Truchet Tiling Curve: Some Extra Settings
« Reply #8 on: June 04, 2024, 12:12:34 pm »
Hi,

@Boleeman,

This version use BGRAVirtualScreen with Antialiasing.

It is slower for large Pictures.

B->
Dzandaa

#### Boleeman

• Hero Member
• Posts: 524
##### Re: Truchet Tiling Curve: Some Extra Settings
« Reply #9 on: June 04, 2024, 01:31:46 pm »
Thanks DZandaa for the BGRAVirtualScreen with Antialiasing.

I tried BgraBmp control but got a bit lost by it all.

Your solution is great. I see how it is a bit slower but the Arc graphics are better.
I did not know about Pict.CanvasBGRA.Arc

I attached my crappy failed attempt (looks like I was doing it incorrectly with BgraBmp)
Curve is moved by width of Panel1 and seems not anti-aliased. Also fill was gone.

A great result with your BGRAVirtualScreen version. Thanks once again DZandaa.
« Last Edit: June 04, 2024, 01:40:55 pm by Boleeman »