Recent

Author Topic: Colored Sierpinski triangle: Another How To Problem to Solve  (Read 622 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 767
Colored Sierpinski triangle: Another How To Problem to Solve
« on: November 16, 2024, 01:17:19 pm »
Created a Colored Sierpinski triangle using a starter code from the Delphi version at
https://rosettacode.org/wiki/Sierpinski_triangle/Graphical

Worked out how to scale it but now I wanted to Mirror it and add it to the lower right corner to make a complete square from 2 Sierpinski Triangles.

Not sure how to do that efficiently.
Perhaps TBgrabmp Canvas has some sort of transformation to mirror or rotate 180 degrees ?

Currently using a TImage called Image1.
« Last Edit: November 17, 2024, 01:07:42 pm by Boleeman »

Dzandaa

  • Sr. Member
  • ****
  • Posts: 404
  • From C# to Lazarus
Re: Colored Sierpinski triangle: How to mirror it to make a complete square ?
« Reply #1 on: November 16, 2024, 07:11:16 pm »
Hi,

Very Very Quick response:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DrawSerpTriangle(Image: TImage; StartX, StartY, Depth: integer);
  2. var
  3.   X, Y, Size, Inx, PosX, PosY: integer;
  4.   ScaleFactor: Double;
  5.   CanvasWidth, CanvasHeight: integer;
  6. begin
  7.   // Determine the size of the Sierpinski triangle (2^Depth x 2^Depth)
  8.   Size := 1 shl Depth;
  9.  
  10.   CanvasWidth := Image.Width;
  11.   CanvasHeight := Image.Height;
  12.  
  13.   ScaleFactor := Min(CanvasWidth / Size, CanvasHeight / Size);
  14.  
  15.   for Y := 0 to Size - 1 do
  16.     for X := 0 to Size - 1 do
  17.     begin
  18.       // Calculate new color index based on X and Y positions
  19.       Inx := (Length(DepthColors24) * (X + Y)) div (Size + Size);
  20.  
  21.       if (X and Y) = 0 then
  22.       begin
  23.         // Scale and draw the pixel on the canvas
  24.         PosX := StartX + Round(X * ScaleFactor);
  25.         PosY := StartY + Round(Y * ScaleFactor);
  26.         Image.Canvas.Pixels[PosX, PosY] := DepthColors24[Inx];
  27.         // Scale and draw the pixel on the canvas
  28.         Image.Canvas.Pixels[
  29.           Image.Canvas.Width - PosX,
  30.           Image.Canvas.Height - PosY
  31.         ] := DepthColors24[Inx];
  32.       end
  33.       else
  34.       begin
  35. {
  36.         Image.Canvas.Pixels[
  37.           StartX + Round(X * ScaleFactor),
  38.           StartY + Round(Y * ScaleFactor)
  39.         ] := clBlack;
  40.         Image.Canvas.Pixels[
  41.           Image.Canvas.Width - (StartX + Round(X * ScaleFactor)),
  42.           Image.Canvas.Height -(StartY + Round(Y * ScaleFactor))
  43.         ] := clBlack;                                                                   // Fill non-triangle parts with a background color
  44.         }
  45.       end;
  46.     end;
  47. end;  
  48.  
Regards,
Dzandaa

ackarwow

  • Full Member
  • ***
  • Posts: 103
    • Andrzej Karwowski's Homepage
Re: Colored Sierpinski triangle: How to mirror it to make a complete square ?
« Reply #2 on: November 16, 2024, 07:34:18 pm »
Hi @Boleeman
The Dzandaa's code seems to work perfectly.

Boleeman

  • Hero Member
  • *****
  • Posts: 767
Re: Colored Sierpinski triangle: How to mirror it to make a complete square ?
« Reply #3 on: November 17, 2024, 04:44:17 am »
Wow ! Thankyou very much Dzandaa for coming to the rescue.

I went on to make a few variations.

There is probably a better "more efficient" way to do the variations in code, instead of copying portions of the image.
« Last Edit: November 17, 2024, 04:46:35 am by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 767
Re: Colored Sierpinski triangle: More how to do it ?
« Reply #4 on: November 17, 2024, 01:00:36 pm »
One more "Problem-O" that I was having problems solving.

I wanted to be able to fill in the largest 2 Black filled triangles with the Multicolored triangles of the same size
(as shown in the attached png).

I wondered if one could save the pixel locations and colors to an array or a collection of the top Multicolored triangle (outlined in red)
and
apply 2 transforms (move it to the bottom right quarter of Image1 and also mirror image the red outlined triangle below the original)

Thinking that matrices might be needed (the morph program at https://forum.lazarus.freepascal.org/index.php/topic,69040.0.html  had a great pascal unit with lots of matix operations).

.
Perhaps even using Dzandaa's method (for half of the image width and height) but just doing it for the outlined smaller multicolored red outlined triangle)

Anyhow I just wondered how it could possibly be done ?
« Last Edit: November 17, 2024, 01:10:40 pm by Boleeman »

 

TinyPortal © 2005-2018