Recent

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

speter

  • Sr. Member
  • ****
  • Posts: 356
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. :)

wp

  • Hero Member
  • *****
  • Posts: 12523
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.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1239
    • 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 3.7.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

speter

  • Sr. Member
  • ****
  • Posts: 356
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. :)

speter

  • Sr. Member
  • ****
  • Posts: 356
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. :)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • 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 :)

wp

  • Hero Member
  • *****
  • Posts: 12523
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.

speter

  • Sr. Member
  • ****
  • Posts: 356
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. :)

speter

  • Sr. Member
  • ****
  • Posts: 356
Re: Hypocycloid Program (included)
« Reply #8 on: March 19, 2024, 05:33:19 am »
I've recently been relooking at pixel drawing code. ;)
In another thread WP included an example using LazIntfImage.

So, here (finally) is my hypocycloid rewritten using (much) faster graphics. :)

If you'd like some nice (sugar free) donuts, checkout the attached project.

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

Dzandaa

  • Sr. Member
  • ****
  • Posts: 404
  • From C# to Lazarus
Re: Hypocycloid Program (included)
« Reply #9 on: March 19, 2024, 03:09:00 pm »
Hi,

I adapted speter code to TBGRABitmap.

Have fun, great forum.

B->
« Last Edit: March 19, 2024, 04:57:44 pm by Dzandaa »
Regards,
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 16384
  • Censorship about opinions does not belong here.
Re: Hypocycloid Program (included)
« Reply #10 on: March 19, 2024, 07:02:37 pm »
too much donuts... have fun you both....
There is nothing wrong with being blunt. At a minimum it is also honest.

 

TinyPortal © 2005-2018