How to resolve the algorithm Archimedean spiral step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Archimedean spiral step by step in the jq programming language

Table of Contents

Problem Statement

The Archimedean spiral is a spiral named after the Greek mathematician Archimedes.

An Archimedean spiral can be described by the equation: with real numbers a and b.

Draw an Archimedean spiral.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Archimedean spiral step by step in the jq programming language

Source code in the jq programming language

def spiral($zero; $turns; $step):

  def pi: 1 | atan * 4;
  def p2: (. * 100 | round) / 100;

  def svg:
    400 as $width
    | 400 as $height
    | 2 as $swidth # stroke
    | "blue" as $stroke
    | (range($zero; $turns * 2 * pi; $step) as $theta
       | (((($theta)|cos) * 2 * $theta + ($width/2)) |p2) as $x
       | (((($theta)|sin) * 2 * $theta + ($height/2))|p2) as $y
       | if $theta == $zero
         then "<path fill='transparent' style='stroke:\($stroke); stroke-width:\($swidth)' d='M \($x) \($y)"
         else " L \($x) \($y)"
         end),
      "' />";

  "<svg width='100%' height='100%' 
        xmlns='http://www.w3.org/2000/svg'>",
        svg,
  "</svg>" ;

spiral(0; 10; 0.025)

def spiral($a; $b; $step; $h):
  def min($x;$y): if $x <= $y then $x else $y end;
  def max($x;$y): if $x <= $y then $y else $x end;
  def pi: 1 | atan * 4;
  
  (6 * pi) as $m
  | ($h * 1.5) as $w
  | { x_min: 9999, y_min: 9999,
      x_max:    0, y_max:    0,
      arr: [] }
  | reduce range($step; $m+$step; $step) as $t (.;
      .r = $a + $b * $t
      | ((.r * ($t|cos) + $w) | round) as $x
      | ((.r * ($t|sin) + $h) | round) as $y
      | if   $x <= 0 or $y <= 0 then .
        elif $x >= 280          then .
        elif $y >= 192          then .
        else .arr[$x][$y] = "*"
        | .x_min = min(.x_min; $x)
        | .x_max = max(.x_max; $x)
        | .y_min = min(.y_min; $y)
        | .y_max = max(.y_max; $y)
	end )
  # ... and print it
  | .arr as $arr
  | range(.x_min; .x_max + 1) as $i
  | reduce range(.y_min; .y_max+1) as $j ( "";
      . + ($arr[$i][$j] // " ") )
  | "\(.)\n" ;

spiral(1; 1; 0.02; 96)

  

You may also check:How to resolve the algorithm Define a primitive data type step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Chernick's Carmichael numbers step by step in the J programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the XSLT programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the C programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the K programming language