Hypocycloid Program (included) lazarus / 39-Graphics ---------------------------------------------------------------------- speter · 2020-12-20 06:09 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.  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. ---------------------------------------------------------------------- wp · 2020-12-20 11:51 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 · 2020-12-20 19:26 Thanks. With @wp's modification, it works charmingly on the Mac, too. ---------------------------------------------------------------------- speter · 2020-12-20 22:55 Thanks wp. cheers S. ---------------------------------------------------------------------- speter · 2020-12-21 01:24 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. cheers S. ---------------------------------------------------------------------- trev · 2020-12-21 08:28 Quote from: speter on December 21, 2020, 02:24:24 amHere 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 · 2020-12-21 10:25 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure T Form1.Process;//------------------------------------------------------ --------------const  inc_a : single = pi/50;  GROUP_SIZE = 10;var  start_colour : tcolor;  ratio, a, b, h, penpos, ang_a, ang_b, inc_b : single;  mid : tpoint;  i, x, y : integer;begin  // Draw background  bmp.Canvas.Brush.Color := Shape_bg.brush.color;  bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);   // Start parameters  mid := point(bmp.width div 2, bmp.height div 2);  ratio := 1 + random*10;  penpos := 1 + random*10;  start_colour := random(1 shl 25);  a := 0.5 * ratio * bmp.height / (ratio + penpos -1);  b := a / ratio;  inc_b := (a-b)/b;  h := penpos * b;  ang_a := 0;   // Create hypocycloid pixel by pixel  repeat    for i := 1 to GROUP_SIZE do    begin      ang_a += inc_a;      ang_b := ang_a * inc_b;      x := trunc (mid.x + (a-b)*cos(ang_a) + h*cos(ang_b));      y := trunc (mid.y - (a-b)*sin(ang_a) + h*sin(ang_b));       if bmp.Canvas.Pixels[x, y] <> Shape_bg.brush.color then        bmp.Canvas.Pixels[x, y] := random(1 shl 25)      else        bmp.Canvas.Pixels[x, y] := start_colour;    end;     Paintbox1.Invalidate;    Application.ProcessMessages;  until finished;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 · 2020-12-24 00:31 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. ---------------------------------------------------------------------- speter · 2024-03-19 04:33 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. ---------------------------------------------------------------------- Dzandaa · 2024-03-19 14:09 Hi, I adapted speter code to TBGRABitmap. Have fun, great forum. B-> ---------------------------------------------------------------------- Thaddy · 2024-03-19 18:02 too much donuts... have fun you both....