code wiki / _hdl_build / nx_viz_chord.nx
nx_viz_chord.nx source
↩ module page · 44 lines · 1888 B
1// nx_viz_chord.nx -- the CHORD layer of the sovereign Nishi viz library (the d3-chord equivalent, bits-up).
2// Maps an N x N flow matrix to a circular layout: each group gets an arc proportional to its row-sum; within it,
3// sub-arcs per target; a chord (i,j) connects group i's sub-arc-for-j to group j's sub-arc-for-i. Angles in
4// centi-degrees (0..36000) for integer precision. Render with arcs (groups) + curves (chords). Closes
5// layout-chord. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7const K_MAGIC_36000: i64 = 36000
8
9func vch_rowsum(matrix: *i64, n: i64, i: i64) -> i64 {
10 var s: i64 = 0; var j: i64 = 0
11 while j < n { s = s + matrix[i * n + j]; j = j + 1 }
12 return s
13}
14func vch_total(matrix: *i64, n: i64) -> i64 {
15 var s: i64 = 0; var i: i64 = 0
16 while i < n * n { s = s + matrix[i]; i = i + 1 }
17 return s
18}
19// group arcs in centi-degrees: garc_out[i*2]=start, [i*2+1]=end. returns total.
20func vch_groups(matrix: *i64, n: i64, garc_out: *i64) -> i64 {
21 let total: i64 = vch_total(matrix, n)
22 if total <= 0 { return 0 }
23 var cum: i64 = 0; var i: i64 = 0
24 while i < n {
25 let gs: i64 = vch_rowsum(matrix, n, i)
26 garc_out[i * 2] = (cum * K_MAGIC_36000) / total
27 cum = cum + gs
28 garc_out[i * 2 + 1] = (cum * K_MAGIC_36000) / total
29 i = i + 1
30 }
31 return total
32}
33// midpoint angle (centi-deg) of sub-arc (i,j) within group i's arc.
34func vch_sub_mid(matrix: *i64, n: i64, i: i64, j: i64, garc_out: *i64) -> i64 {
35 let gstart: i64 = garc_out[i * 2]
36 let gspan: i64 = garc_out[i * 2 + 1] - gstart
37 let gs: i64 = vch_rowsum(matrix, n, i)
38 if gs <= 0 { return gstart }
39 var before: i64 = 0; var k: i64 = 0
40 while k < j { before = before + matrix[i * n + k]; k = k + 1 }
41 let mid: i64 = before + matrix[i * n + j] / 2
42 return gstart + (mid * gspan) / gs
43}
44func main() -> i64 { return 0 }