Recent

Author Topic: Hypocycloid Program (included)  (Read 1176 times)

speter

  • Full Member
  • ***
  • Posts: 187
Hypocycloid Program (included)
« on: December 20, 2020, 07:09:05 am »
G'Day Folks,

I was looking through some old code and came across a (Turbo Pascal) program I wrote to draw Hypocycloid patterns. :)

I have ported it to Laz - which was a very simple task; it uses Pixels[] so it is fairly slow; but that is part of its charm. The pattern builds up over time; changing as it goes.

Anyway, the project it attached to this post.

I find the patterns amazing and hypnotic. Enjoy.  8-)

Note that it will NOT work in macOS. I couldn't get it to work using the OnPaint method to draw the pattern.

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

wp

  • Hero Member
  • *****
  • Posts: 8322
Re: Hypocycloid Program (included)
« Reply #1 on: December 20, 2020, 12:51:24 pm »
Nice.

When you draw into a bitmap, paint that bitmap on the Paintbox canvas in the Paintbox OnPaint handler and call Paintbox.Invalidate after setting a (or several) pixel then the program works also on Mac. This way the "art pattern" is persistent regarding repaint requests by the OS: While your program is calculating drag the form halfway out of the screen and then back - see what happens...

I am attaching a modified version according to this suggestion. I also allows to close the form by clicking the "x" button while the calculation is running.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jwdietrich

  • Hero Member
  • *****
  • Posts: 1172
    • formatio reticularis
Re: Hypocycloid Program (included)
« Reply #2 on: December 20, 2020, 08:26:38 pm »
Thanks. With @wp's modification, it works charmingly on the Mac, too.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.12 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

speter

  • Full Member
  • ***
  • Posts: 187
Re: Hypocycloid Program (included)
« Reply #3 on: December 20, 2020, 11:55:08 pm »
Thanks wp.

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

speter

  • Full Member
  • ***
  • Posts: 187
Re: Hypocycloid Program (included)
« Reply #4 on: December 21, 2020, 02:24:24 am »
G'Day Folks,

Here is a new version of my hypocycloid pattern drawing program; based on wp's tweak of my first version.

  v1.2
    + the program now draws on a bitmap, this will hopefully work with macOS.
    + handles resize
    + key / mouse click now "toggles" drawing
    + allows user to set the background colour.

Enjoy. :P

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1393
  • Former Delphi 1-7, 10.2 User
Re: Hypocycloid Program (included)
« Reply #5 on: December 21, 2020, 09:28:36 am »
Here is a new version of my hypocycloid pattern drawing program; based on wp's tweak of my first version.

  v1.2

Works perfectly, albeit slowly, on macOS :)
Lazarus 2.1 r65019 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65019 FPC 3.3.1 r49223 macOS 11.2.3 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

wp

  • Hero Member
  • *****
  • Posts: 8322
Re: Hypocycloid Program (included)
« Reply #6 on: December 21, 2020, 11:25:38 am »
Regarding the speed you could pick up your old idea again of doing several cycles of calculation before a repaint is performed:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Process;
  2. //--------------------------------------------------------------------
  3. const
  4.   inc_a : single = pi/50;
  5.   GROUP_SIZE = 10;
  6. var
  7.   start_colour : tcolor;
  8.   ratio, a, b, h, penpos, ang_a, ang_b, inc_b : single;
  9.   mid : tpoint;
  10.   i, x, y : integer;
  11. begin
  12.   // Draw background
  13.   bmp.Canvas.Brush.Color := Shape_bg.brush.color;
  14.   bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);
  15.  
  16.   // Start parameters
  17.   mid := point(bmp.width div 2, bmp.height div 2);
  18.   ratio := 1 + random*10;
  19.   penpos := 1 + random*10;
  20.   start_colour := random(1 shl 25);
  21.   a := 0.5 * ratio * bmp.height / (ratio + penpos -1);
  22.   b := a / ratio;
  23.   inc_b := (a-b)/b;
  24.   h := penpos * b;
  25.   ang_a := 0;
  26.  
  27.   // Create hypocycloid pixel by pixel
  28.   repeat
  29.     for i := 1 to GROUP_SIZE do
  30.     begin
  31.       ang_a += inc_a;
  32.       ang_b := ang_a * inc_b;
  33.       x := trunc (mid.x + (a-b)*cos(ang_a) + h*cos(ang_b));
  34.       y := trunc (mid.y - (a-b)*sin(ang_a) + h*sin(ang_b));
  35.  
  36.       if bmp.Canvas.Pixels[x, y] <> Shape_bg.brush.color then
  37.         bmp.Canvas.Pixels[x, y] := random(1 shl 25)
  38.       else
  39.         bmp.Canvas.Pixels[x, y] := start_colour;
  40.     end;
  41.  
  42.     Paintbox1.Invalidate;
  43.     Application.ProcessMessages;
  44.   until finished;
  45. end;

Additionally, making the constant GROUP_SIZE a user-defined variable would allow the user to adjust the drawing speed himself. Of course it must be guaranteed that the variable is not less than 1 (this would not calculate anything) or too large (this would not update the display at all or at too large intervals; find out the limit yourself.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

speter

  • Full Member
  • ***
  • Posts: 187
Re: Hypocycloid Program (included)
« Reply #7 on: December 24, 2020, 01:31:52 am »
Thanks wp, good idea.

I've changed the program, re-introducing the inner loop. I've added an edit-box to allow the user to set the loop size (constrained to 1..200).

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

 

TinyPortal © 2005-2018