How to resolve the algorithm Mandelbrot set step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Mandelbrot set step by step in the jq programming language

Table of Contents

Problem Statement

Generate and draw the Mandelbrot set.

Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mandelbrot set step by step in the jq programming language

Source code in the jq programming language

# SVG STUFF
  def svg(id; width; height): 
    "<svg width='\(width // "100%")' height='\(height // "100%") '
        id='\(id)'
        xmlns='http://www.w3.org/2000/svg'>";

  def pixel(x;y;r;g;b;a):
    "<circle cx='\(x)' cy='\(y)' r='1' fill='rgb(\(r|floor),\(g|floor),\(b|floor))' />";

# "UNTIL"
  # As soon as "condition" is true, then emit . and stop:
  def do_until(condition; next):
    def u: if condition then . else (next|u) end;
    u;

def Mandeliter( cx; cy; maxiter ):
  # [i, x, y, x^2+y^2]
  [ maxiter, 0.0, 0.0, 0.0 ]
  | do_until( .[0] == 0 or .[3] > 4;
      .[1] as $x | .[2] as $y
      | ($x * $y) as $xy
      | ($x * $x) as $xx
      | ($y * $y) as $yy
      | [ (.[0] - 1),         # i
          ($xx - $yy + cx),   # x
          ($xy + $xy + cy),   # y
          ($xx+$yy)           # xx+yy
        ] )
    | maxiter - .[0];
 
# width and height should be specified as the number of pixels.
# obj == { xmin: _, xmax: _, ymin: _, ymax: _ }
def Mandelbrot( obj; width; height; iterations ):
  def pixies:
    range(0; width) as $ix
    | (obj.xmin + ((obj.xmax - obj.xmin) * $ix / (width - 1))) as $x 
    | range(0; height) as $iy
    | (obj.ymin + ((obj.ymax - obj.ymin) * $iy / (height - 1))) as $y
    | Mandeliter( $x; $y; iterations ) as $i
    | if $i == iterations then
        pixel($ix; $iy; 0; 0; 0; 255)
      else
        (3 * ($i|log)/((iterations - 1.0)|log)) as $c  # redness
        | if $c < 1 then
            pixel($ix;$iy; 255*$c; 0; 0; 255)
          elif $c < 2 then
            pixel($ix;$iy; 255; 255*($c-1); 0; 255)
          else
            pixel($ix;$iy; 255; 255; 255*($c-2); 255)
          end
      end;

  svg("mandelbrot"; "100%"; "100%"),
  pixies,
  "</svg>";

 Mandelbrot( {"xmin": -2, "xmax": 1, "ymin": -1, "ymax":1}; 900; 600; 1000 )

  

You may also check:How to resolve the algorithm Loops/Downward for step by step in the langur programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Racket programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the Prolog programming language
You may also check:How to resolve the algorithm Mastermind step by step in the BASIC programming language