\ 4tH graphics - The Batman equation - Copyright 2022 J.L. Bezemer
\ You can redistribute this file and/or modify it under
\ the terms of the GNU General Public License

include lib/fp3.4th                    \ for floating point
include lib/anscore.4th                \ for 2OVER
include lib/graphics.4th               \ for graphics support
include 4pp/lib/float.4pp              \ since we're using the preprocessor..

[float] fclear                         \ setup for floating point

fvariable x                            \ holds x-coordinate value
fvariable y                            \ holds y coordinate value
                                       \ get coordinates from x, y values
: x! dup abs s>f 100 s>f f/ x f! 801 + 2/ ;
: y@ y f@ 1000 s>f f* f>s 5010 + 20 / 600 - abs swap ;
: paint 2swap 2over line ;             ( n1 n2 n3 n4 -- n3 n4)

: batman1                              \ first batman equation
  x!
  let y = 2*sqrt(-abs(abs(x)-1)*abs(3-abs(x))/((abs(x)-1)*(3-abs(x))))*(1+abs(abs(x)-3)/(abs(x)-3))*sqrt(1-(x/7)*(x/7));
  let y = y+(5+0.97*(abs(x-0.5)+abs(x+0.5))-3*(abs(x-0.75)+abs(x+0.75)))*(1+abs(1-abs(x))/(1-abs(x)));
  y@
;

: batman2                              \ second batman equation
  x!
  let y = (-3)*sqrt(1-(x/7)*(x/7))*sqrt(abs(abs(x)-4)/(abs(x)-4));
  y@
;

: batman3                              \ third batman equation
  x!
  let y = abs(x/2)-(0.0913722*(x*x))-3+(sqrt(1-((abs(abs(x)-2)-1)*(abs(abs(x)-2)-1))));
  y@
;

: batman4                              \ fourth batman equation
  x!
  let y = (2.71052+(1.5-0.5*abs(x))-1.35526*sqrt(4-(abs(x)-1)*(abs(x)-1)))*sqrt(abs(abs(x)-1)/(abs(x)-1))+0.9;
  y@
;

\ we're on an interval -700 < x < 700, which translates to -7.00 < x < 7.00
\ we're not calculating the values where they don't apply, so we're actually
\ restricting ourselves to certain domains. It's not quite good enough to
\ do a fill.

800 pic_width ! 600 pic_height !       \ set canvas size
color_image 255 whiteout black         \ paint black on white

." WAIT! - it will get there.." cr
                                       \ do first batman equation
-699 batman1 -300 -699 do i batman1 paint loop 2drop
 -99 batman1  100  -99 do i batman1 paint loop 2drop
 301 batman1  700  301 do i batman1 paint loop 2drop
                                       \ do second batman equation
-699 batman2 -402 -699 do i batman2 paint loop 2drop
 401 batman2  700  401 do i batman2 paint loop 2drop
                                       \ do third batman equation
-402 batman3  403 -402 do i batman3 paint loop 2drop
                                       \ do fourth batman equation
-300 batman4 -100 -300 do i batman4 paint loop 2drop
 101 batman4  301  101 do i batman4 paint loop 2drop

                                       \ fixup some cosmetic issues
-699 dup batman1  rot batman2 paint 2drop
 699 dup batman1  rot batman2 paint 2drop
 -99     batman1 -101 batman4 paint 2drop
  99     batman1  101 batman4 paint 2drop

s" batman.ppm" save_image               \ save the image

