我正在为 3D 打印机的床网格开发基于 ASCII 的热图。我有一个颜色数组(长度 107),并且床网格可以有很多种值,其中始终包含负值和正值 - 下面的示例:
{"mesh_matrix": [
[ 0.240094, 0.178237, 0.114668, 0.054469, 0.001266, -0.042774, -0.076938, -0.101971, -0.120074, -0.134906, -0.151581, -0.17667, -0.218202 ],
[ 0.126341, 0.10407, 0.054248, -0.002618, -0.05146, -0.082652, -0.092006, -0.080773, -0.055648, -0.02876, -0.017682, -0.045426, -0.140442 ],
[ 0.12967, 0.115636, 0.068095, 0.010321, -0.040461, -0.073068, -0.082363, -0.069252, -0.040688, -0.009667, 0.00477, -0.022463, -0.122497 ],
[ 0.200219, 0.175653, 0.124276, 0.064281, 0.009271, -0.031736, -0.054312, -0.058614, -0.049389, -0.035969, -0.032275, -0.056816, -0.132687 ],
[ 0.298821, 0.255262, 0.198163, 0.136783, 0.07823, 0.027458, -0.012731, -0.041688, -0.060917, -0.074073, -0.086965, -0.107555, -0.145957 ],
[ 0.396996, 0.334024, 0.272435, 0.211874, 0.152394, 0.094456, 0.038925, -0.012924, -0.059411, -0.098447, -0.127539, -0.143785, -0.143875 ],
[ 0.476957, 0.399923, 0.337079, 0.280125, 0.223227, 0.163025, 0.098625, 0.031606, -0.033983, -0.091625, -0.132331, -0.144644, -0.114635 ],
[ 0.531606, 0.449362, 0.389391, 0.338633, 0.287678, 0.23076, 0.165761, 0.09421, 0.021282, -0.044201, -0.08977, -0.099308, -0.053054 ],
[ 0.564536, 0.487167, 0.43397, 0.391022, 0.348181, 0.299085, 0.24115, 0.175572, 0.107325, 0.045164, 0.001623, -0.006987, 0.039427 ],
[ 0.590031, 0.526586, 0.482725, 0.447438, 0.412655, 0.373249, 0.327031, 0.274756, 0.220117, 0.16975, 0.13323, 0.123075, 0.154741 ],
[ 0.633064, 0.589288, 0.554874, 0.524556, 0.494505, 0.462327, 0.427067, 0.389203, 0.350654, 0.31477, 0.286343, 0.271598, 0.278198 ],
[ 0.7293, 0.705362, 0.676938, 0.645575, 0.61262, 0.579226, 0.546345, 0.514736, 0.484958, 0.457376, 0.432156, 0.409267, 0.388482 ],
[ 0.925094, 0.913319, 0.882748, 0.840219, 0.791376, 0.740674, 0.691375, 0.645551, 0.604082, 0.566656, 0.531771, 0.496733, 0.457656 ]
]}
上述数据中,最小值为-0.218202,最大值为0.925094。 因此,如果我有一个包含 107 个字符的数组,我们会将
-0.218202
映射到 colors[0]
,将 0.925094
映射到 colors[-1]
。
我尝试做的是:
107/1.143296 = 93.5890618003
0.496733
,则 colorIdx = round((0.496733+0.218202)*93.5890618003) = 67
您的输入是 JSON。由于您已标记 bash,您可能正在寻找命令行 JSON 处理器。例如,对于 jq,可以按如下方式解决:
< input.json jq --argjson n 107 '
([.[][][]] | [min, $n / (max - min)]) as [$min, $s]
| .[][][] |= ($s * (. - $min) | round)
'
{
"mesh_matrix": [
[
43,
37,
31,
26,
21,
16,
13,
11,
9,
8,
6,
4,
0
],
[
32,
30,
25,
20,
16,
13,
12,
13,
15,
18,
19,
16,
7
],
[
33,
31,
27,
21,
17,
14,
13,
14,
17,
20,
21,
18,
9
],
[
39,
37,
32,
26,
21,
17,
15,
15,
16,
17,
17,
15,
8
],
[
48,
44,
39,
33,
28,
23,
19,
17,
15,
13,
12,
10,
7
],
[
58,
52,
46,
40,
35,
29,
24,
19,
15,
11,
8,
7,
7
],
[
65,
58,
52,
47,
41,
36,
30,
23,
17,
12,
8,
7,
10
],
[
70,
62,
57,
52,
47,
42,
36,
29,
22,
16,
12,
11,
15
],
[
73,
66,
61,
57,
53,
48,
43,
37,
30,
25,
21,
20,
24
],
[
76,
70,
66,
62,
59,
55,
51,
46,
41,
36,
33,
32,
35
],
[
80,
76,
72,
70,
67,
64,
60,
57,
53,
50,
47,
46,
46
],
[
89,
86,
84,
81,
78,
75,
72,
69,
66,
63,
61,
59,
57
],
[
107,
106,
103,
99,
94,
90,
85,
81,
77,
73,
70,
67,
63
]
]
}