#import "@preview/cetz:0.4.2"

#let bgBlock(body, fill: color, width: 100%) = block(body, fill:fill.lighten(80%), width: width, inset: (bottom: 2mm, left: 2mm, right: 2mm,))

#let SeperatorLine = line(length: 100%, stroke: (paint: black, thickness: 0.3mm))
#let MathAlignLeft(e) = {
  align(left, block(e))
}

#let subHeading(body, fill: color) = {
  move(dx: -2mm, dy: 0mm, box(
    align(
      top+center,
      text(
        body,
        size: 8pt,
        weight: "bold",
        style: "italic",
      )
    ),
    fill: fill,
    width: 100% + 4mm,
    inset: 1mm,
    height: auto
  ))
}

#let MathAlignLeft(e) = {
  align(left, block(e))
}

#let tableFillHigh = white
#let tableFillLow = color.lighten(gray, 50%)

#let sinTable = [
  #let data = json("../sintable.json")
  #table(
    columns: data.len(),
    rows: data.keys().len(),
    stroke: none,
    table.hline(stroke: (thickness: 0.3mm)),
    fill: (x, y) => if (calc.rem(y, 2) == 0) { tableFillLow } else { tableFillHigh },

    table.vline(),
    ..for (i, label) in data.keys().enumerate() {
      ([*#eval(label, mode: "math")*], if i > 0 { table.vline() } else { table.vline(stroke: none) })
    },

    table.hline(stroke: (thickness: 0.3mm)),
  
    ..for (i, v) in data.at("x").enumerate() {
    for (col) in data.keys() {
        (eval(data.at(col).at(i), mode: "math"),)
      }
    },
    table.hline(stroke: (thickness: 0.3mm)),
  )
]

#let ComplexNumbersSection(i: $i$) = [
  $1/#i = #i^(-1) = -#i quad quad #i^2=-1 quad quad sqrt(#i) = 1/sqrt(2) + 1/sqrt(2)#i$

  $z in CC = a + b #i quad quad quad z = r dot e^(#i phi)$ \
  $z_0 + z_1 = (a_0 + a_1) + (b_0 + b_1) #i$\
  $z_0 dot z_1 = (a_1 a_2 - b_1 b_2) + #i (a_1b_2 + a_2 b_1) = r_0 r_1 e^(#i (phi_0 + phi_1))$\
  $z^x = r^x dot e^(phi #i dot x) quad x in RR$ \
  $z_0/z_1 = r_0/r_1 e^(#i (phi_0 - phi_1)) quad quad quad$ 

  $z^* = a - #i b = r e^(-#i phi)$

  Konjungiert Erweitern:\
  #grid(columns: (1fr, 1fr), 
    $(a + b #i)/(c + d #i) = ((a + b #i)(c - d #i))/(c^2 + d² )$,
    $1/(a + j b) = (a - j b)/(a^2 + b^2)$
  )

  

  $r = abs(z) = sqrt(a + #i b) = sqrt(z z^*) quad phi = cases(
    + arccos(a/r) space : space a >= 0,
    - arccos(a/r) space : space a < 0,
  )$
]

#let SineCircle() = {
  align(center+horizon,
    cetz.canvas({
      import cetz.draw : *
      line((-33mm, 0), (33mm, 0), stroke: (paint: rgb("#8e8e8e7f")))
      line((0, -33mm), (0, 33mm), stroke: (paint: rgb("#8e8e8e7f")))
      let labels = (
        (0deg, $0°$, $0$),
        (30deg, $30°$, $pi/6$),
        (45deg, $45°$, $pi/4$),
        (60deg, $60°$, $pi/3$),
        (90deg, $90°$, $pi/2$),
        (120deg, $120°$, $2pi/3$),
        (135deg, $135°$, $3pi/4$),
        (150deg, $150°$, $5pi/6$),
        (180deg, $180°$, $pi$),
        (210deg, $210°$, $7pi/6$),
        (225deg, $2250°$, $5pi/4$),
        (240deg, $240°$, $4pi/3$),
        (270deg, $270°$, $3pi/2$),
        (300deg, $300°$, $5pi/3$),
        (315deg, $315°$, $7pi/4$),
        (330deg, $330°$, $11pi/6$),
      )


      circle((0, 0), radius: 25mm)



      for (pos, label1, label2) in labels {
        line((radius: 24mm, angle: pos), (radius: 26mm, angle: pos))
        content((radius: 20.0mm, angle: pos), label1, anchor: "mid", angle: if(pos > 90deg and pos < 270deg) {pos + 180deg} else {pos})
        content((radius: 29.5mm, angle: pos), label2, anchor: "mid")    
      }

    })
  )
}

#let SIPrefixesTable = [
  #table(
    columns: (1fr, 1fr, 1fr),
    stroke: (thickness: 0.3mm),
    align: (col, row) => if row == 0 { center } else { left },
    fill: (x, y) => if (calc.rem(y, 2) == 0) { tableFillLow } else { tableFillHigh },
    
    [*Prefix*], [*Symbol*], [*Factor*],
    
    [exa], [T], [$10^18$],
    [peta], [T], [$10^15$],
    [tera], [T], [$10^12$],
    [giga], [G], [$10^9$],
    [mega], [M], [$10^6$],
    [kilo], [k], [$10^3$],
    [hecto], [h], [$10^2$],
    [deca], [da], [$10^1$],
    [(none)], [(none)], [$10^0$],
    [deci], [d], [$10^(-1)$],
    [centi], [c], [$10^(-2)$],
    [milli], [m], [$10^(-3)$],
    [micro], [μ], [$10^(-6)$],
    [nano], [n], [$10^(-9)$],
    [pico], [p], [$10^(-12)$],
    [femto], [f], [$10^(-15)$],
    [atto], [p], [$10^(-18)$],
  )
]