forked from cetz-package/cetz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.typ
64 lines (59 loc) · 1.57 KB
/
util.typ
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
62
63
64
#import "matrix.typ"
#import "vector.typ"
// Apply all transformation matrices `queue` in order on `vec`.
#let apply-transform(transform, vec) = {
// vec = vector.as-vec(vec, init: (0, 0, 0, 1))
// for m in queue.do {
// if m != none {
// vec = matrix.mul-vec(m, vec)
// }
// }
// return vec.slice(0, 3)
matrix.mul-vec(transform, vector.as-vec(vec, init: (0, 0, 0, 1))).slice(0, 3)
}
// Revert all transformation matrices `queue` in
// reverse order on `vec`.
#let revert-transform(transform, vec) = {
// vec = vector.as-vec(vec, init: (0, 0, 0, 1))
// for m in queue.undo.rev() {
// if m != none {
// vec = matrix.mul-vec(m, vec)
// }
// }
// return vec.slice(0, 3)
apply-transform(matrix.inverse(transform), vec)
}
#let bezier-quadratic-pt(a, b, c, t) = {
// (1-t)^2 * a + 2 * (1-t) * t * c + t^2 b
return vector.add(
vector.add(
vector.scale(a, calc.pow(1-t, 2)),
vector.scale(c, 2 * (1-t) * t)
),
vector.scale(b, calc.pow(t, 2))
)
}
#let bezier-cubic-pt(a, b, c1, c2, t) = {
// (1-t)^3*a + 3*(1-t)^2*t*c1 + 3*(1-t)*t^2*c2 + t^3*b
vector.add(
vector.add(
vector.scale(a, calc.pow(1-t, 3)),
vector.scale(c1, 3 * calc.pow(1-t, 2) * t)
),
vector.add(
vector.scale(c2, 3*(1-t)*calc.pow(t,2)),
vector.scale(b, calc.pow(t, 3))
)
)
}
#let resolve-number(ctx, num) = {
if type(num) == "length" {
if repr(num).ends-with("em") {
float(repr(num).slice(0, -2)) * ctx.em-size.width / ctx.length
} else {
float(num / ctx.length)
}
} else {
float(num)
}
}