-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath-extra.nix
61 lines (54 loc) · 1.41 KB
/
math-extra.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/nix-instantiate --eval
let lib = import <nixpkgs/lib>;
inherit (lib.trivial) mod;
sum = lib.lists.foldr (a: b: a + b) 0;
abs = n: if n < 0 then -n else n;
odd = n: mod n 2 == 1;
even = n: mod n 2 == 0;
slope = (fst: snd:
let
rise = snd.y - fst.y;
run = snd.x - fst.x;
in
if run != 0 then rise / run
else null
);
manhattanDistance = (fst: snd: abs (snd.x - fst.x) + abs (snd.y - fst.y));
quadrant = (vec:
if vec.x > 0 && vec.y > 0 then
1
else if vec.x < 0 && vec.y > 0 then
2
else if vec.x < 0 && vec.y < 0 then
3
else if vec.x > 0 && vec.y < 0 then
4
# not really quadrant anymore
else if vec.x == 0 && vec.y == 0 then
0
else if vec.y == 0 && vec.x > 0 then
-1
else if vec.x == 0 && vec.y > 0 then
-2
else if vec.y == 0 && vec.x < 0 then
-3
else if vec.x == 0 && vec.y < 0 then
-4
else
abort "Aaaaaahhhh"
);
semiaxis = (vec:
if vec.x == 0 && vec.y == 0 then
0
else if vec.y == 0 && vec.x > 0 then
1
else if vec.x == 0 && vec.y > 0 then
2
else if vec.y == 0 && vec.x < 0 then
3
else if vec.x == 0 && vec.y < 0 then
4
else
null
);
in { inherit abs even mod odd sum slope manhattanDistance; }