Recent

Author Topic: Can you do art with random walk?  (Read 366 times)

Petri

  • New member
  • *
  • Posts: 9
Can you do art with random walk?
« on: July 10, 2022, 03:39:12 pm »
Maybe little, I think.
Code: Pascal  [Select][+][-]
  1. program image;
  2. uses graph, wincrt,sysutils;
  3. var gd,gm : integer;
  4.    PathToDriver : string;
  5.    x,y,
  6.    dx,dy,
  7.    width,color,half     :       word;
  8. PROCEDURE PutBar(a,b:word);
  9. BEGIN
  10.         Bar(a-half,b-half,a+half,b+half);
  11. END;
  12. begin
  13.    Randomize;
  14.    gd:=detect;  
  15.    gm:=0;
  16.    PathToDriver:=' ';
  17.    InitGraph(gd,gm,PathToDriver);
  18.    if GraphResult<>grok then
  19.      halt;
  20.         x:= trunc(GetMAxX/2);
  21.         y:= trunc(GetMAxY/2);
  22.         dx:=0;
  23.         dy:=0;
  24.         width:=5;
  25.         half:=trunc(width/2);
  26.         color:=0;
  27.         REPEAT
  28.                 BEGIN
  29.                         dx:=dx-width-1+(random(2*width+1)+1);
  30.                         dy:=dy-width-1+(random(2*width+1)+1);
  31.                         color:=Random(65535);
  32.                         SetFillStyle(SolidFill,color);
  33.                         PutBar(x+dx,y+dy); {let's draw}
  34.                         PutBar(x-dx,y+dy); {the bar sym-}
  35.                         PutBar(x-dx,y-dy); {metrically in}
  36.                         PutBar(x+dx,y-dy); {eight places}
  37.                         PutBar(x+dy,y+dx);
  38.                         PutBar(x-dy,y+dx);
  39.                         PutBar(x-dy,y-dx);
  40.                         PutBar(x+dy,y-dx);             
  41.                         IF KeyPressed THEN halt(2);
  42.                         if (dx>x) or (dx<-dx) THEN dx:=0;
  43.                         if (dy>y) or (dy<-dy) THEN dy:=0;
  44.                 END;
  45.         UNTIL FALSE;
  46.    CloseGraph;
  47. end.
  48.  

Thaddy

  • Hero Member
  • *****
  • Posts: 11929
Re: Can you do art with random walk?
« Reply #1 on: July 10, 2022, 04:56:18 pm »
Really old code and it has some issues, like a memory leak.
I corrected most of it:
Code: Pascal  [Select][+][-]
  1. program image_withoutleaks;
  2. {$optimization fastmath}
  3. uses graph, wincrt; //sysutils is not used
  4. var
  5.    gd,gm : integer;
  6.    PathToDriver : string = '';
  7.    x,y,
  8.    dx,dy,
  9.    width,color,half : word;
  10.  
  11. procedure PutBar(a,b:word);inline;
  12. begin
  13.   Bar(a-half,b-half,a+half,b+half);
  14. end;
  15.  
  16. begin
  17.    Randomize;
  18.    gd:=detect;  
  19.    gm:=0;
  20.    InitGraph(gd,gm,PathToDriver);
  21.    if GraphResult<>grok then
  22.    begin
  23.      closegraph; // <-- graph can still be open and the error may be elsewhere.
  24.      halt;
  25.    end;
  26.    x:= trunc(GetMAxX/2);
  27.    y:= trunc(GetMAxY/2);
  28.    dx:=0;
  29.    dy:=0;
  30.    width:=5;
  31.    half:=trunc(width/2);
  32.    color:=0;
  33.    repeat
  34.      dx:=dx-width-1+(random(2*width+1)+1);
  35.      dy:=dy-width-1+(random(2*width+1)+1);
  36.      color:=Random(65535);
  37.      SetFillStyle(SolidFill,color);
  38.      PutBar(x+dx,y+dy); {let's draw}
  39.      PutBar(x-dx,y+dy); {the bar sym-}
  40.      PutBar(x-dx,y-dy); {metrically in}
  41.      PutBar(x+dx,y-dy); {eight places}
  42.      PutBar(x+dy,y+dx);
  43.      PutBar(x-dy,y+dx);
  44.      PutBar(x-dy,y-dx);
  45.      PutBar(x+dy,y-dx);            
  46.      if KeyPressed then
  47.      begin
  48.        closegraph; // <------- should be called before you call halt()
  49.        halt(2);
  50.      end;
  51.      if (dx>x) or (dx<-dx) then dx:=0;
  52.      if (dy>y) or (dy<-dy) then dy:=0;                
  53.    until false;
  54.    CloseGraph; // You can remove this, actually
  55. end.

Now it compiles without leaks. It looks easier on the eye too...
« Last Edit: July 10, 2022, 06:44:01 pm by Thaddy »
Black themes should be banned.

Petri

  • New member
  • *
  • Posts: 9
Re: Can you do art with random walk?
« Reply #2 on: July 10, 2022, 05:13:29 pm »
Really old code and it has some issues, like a memory leak.
Yes. I used it in 1983 with my 8-bit Spectrum Sinclair computer. So it's been 39 years :) It was in a little book. Nice code because I still remember it.

Thanks for correcting those 'memory leak' things.

Handoko

  • Hero Member
  • *****
  • Posts: 4712
  • My goal: build my own game engine using Lazarus
Re: Can you do art with random walk?
« Reply #3 on: July 11, 2022, 05:39:29 pm »
That random walk picture looks great.

I think you should upgrade your skill to use OpenGL. If you use Lazarus, you can do OpenGL programming easily. Most, maybe all computers produced since 2000 support OpenGL basic features. Newer computers support more modern OpenGL features.

OpenGL is many times faster than the classic drawing methods. You can do realtime 3D animations and if you prefer 2D only, OpenGL has shader programming, which you can use it to do things like this:
https://www.youtube.com/watch?v=bkAQWANJrQU
https://www.youtube.com/watch?v=Boxlb0_a-HA
« Last Edit: July 11, 2022, 05:45:24 pm by Handoko »

Petri

  • New member
  • *
  • Posts: 9
Re: Can you do art with random walk?
« Reply #4 on: July 11, 2022, 07:10:10 pm »
I think you should upgrade your skill to use OpenGL.
Maybe I should, but as an hobby artist I want to do things my way. http://www.rebol.com/ is my main programming language and I have lot of self-made programs on my hard drive. I don't want to compete in an area where there is oversupply and some have studied the field for years. I'm quite happy with my work so far:

https://youtu.be/FkNYtzQUJLk

I have an algorithmic art homepage. There you can find, among other things, these thousand pictures https://petke.info/albumi.php which I think are quite nice, and much more.
« Last Edit: July 11, 2022, 07:12:10 pm by Petri »

 

TinyPortal © 2005-2018