code wiki / _hdl_build / nx_viz_trig.nx
nx_viz_trig.nx source
↩ module page · 30 lines · 1231 B
1// nx_viz_trig.nx -- the TRIG layer of the sovereign Nishi viz library (hardware-rung-up: pure INTEGER sin/cos,
2// no FPU, no lookup table). Bhaskara I's sine approximation: sin(d deg) ~= 4d(180-d) / (40500 - d(180-d)),
3// accurate to ~0.16% -- exact at 0/30/90/150/180. Output scaled by 10000 (sin(90)=10000). Range-reduced to
4// [0,360). Unlocks arc/pie shapes + radial/force layouts. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6const K_MAGIC_40500: i64 = 40500
7const K_MAGIC_40000: i64 = 40000
8
9// sin(deg) * 10000, any integer degree.
10func vs_sin(deg: i64) -> i64 {
11 var d: i64 = deg % 360
12 if d < 0 { d = d + 360 }
13 var sign: i64 = 1
14 if d > 180 { d = d - 180; sign = 0 - 1 }
15 let p: i64 = d * (180 - d)
16 let den: i64 = K_MAGIC_40500 - p
17 if den == 0 { return 0 }
18 return sign * (K_MAGIC_40000 * p) / den
19}
20// cos(deg) * 10000.
21func vs_cos(deg: i64) -> i64 { return vs_sin(deg + 90) }
22// integer square root (floor), Newton's method -- the math layer's root primitive (powers distances in layouts).
23func vs_isqrt(n: i64) -> i64 {
24 if n <= 0 { return 0 }
25 var x: i64 = n
26 var y: i64 = (x + 1) / 2
27 while y < x { x = y; y = (x + n / x) / 2 }
28 return x
29}
30func main() -> i64 { return 0 }