Skip to content

Commit 1564294

Browse files
committed
transform: Add transform Function
1 parent cb2c4c6 commit 1564294

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Added a new element `svg-path` that accepts a subset of SVG
1818
commands to construct paths
1919
- Fixed a bug with `brace` and `flip: true` (#1017)
20+
- Added a new `transform` function that multiplies a given transformation matrix with the current one (#1019)
2021

2122
# 0.4.2
2223
- The `tree` element now has a `anchor:` argument to position the tree (#929)

docs/api/draw-functions/transformations/transformations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Transformations
22

3-
All transformation functions push a transformation matrix onto the current transform stack. To apply transformations scoped use the [`group`](../grouping/group.mdx) draw function.
3+
All transformation functions but `set-transform` push a transformation matrix onto the current transform stack. To apply transformations scoped use the [`group`](../grouping/group.mdx) or [`scope`](../grouping/scope.mdx') draw function.
44

55
Transformation matrices get multiplied in the following order:
66

manual.pdf

7.2 KB
Binary file not shown.

src/draw.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import "draw/grouping.typ": intersections, group, scope, anchor, copy-anchors, set-ctx, get-ctx, for-each-anchor, on-layer, hide, floating
2-
#import "draw/transformations.typ": set-transform, rotate, translate, scale, set-origin, move-to, set-viewport
2+
#import "draw/transformations.typ": set-transform, transform, rotate, translate, scale, set-origin, move-to, set-viewport
33
#import "draw/styling.typ": set-style, fill, stroke, register-mark
44
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path, polygon, compound-path, n-star, rect-around, svg-path
55
#import "draw/projection.typ": ortho, on-xy, on-xz, on-yz

src/draw/transformations.typ

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
}
2222
}
2323

24-
/// Sets the transformation matrix.
24+
/// Overwrites the transformation matrix.
2525
///
26-
/// - mat (none, matrix): The 4x4 transformation matrix to set. If `none` is passed, the transformation matrix is set to the identity matrix (`matrix.ident()`).
26+
/// - mat (none, matrix): The 4x4 transformation matrix to set. If `none` is passed, the transformation matrix is set to the identity matrix (`matrix.ident(4)`).
2727
#let set-transform(mat) = {
2828
let mat = if mat == none {
2929
matrix.ident(4)
@@ -45,6 +45,33 @@
4545
},)
4646
}
4747

48+
/// Applies a $4 times 4$ transformation matrix to the current transformation.
49+
///
50+
/// Given the current transformation $C$ and the new transformation $T$,
51+
/// the function sets the new canvas' transformation $C'$ to $C' = C T$.
52+
///
53+
/// - mat (none, matrix): The 4x4 transformation matrix to set. If `none` is passed, the transformation matrix is set to the identity matrix (`matrix.ident(4)`).
54+
#let transform(mat) = {
55+
let mat = if mat == none {
56+
matrix.ident(4)
57+
} else {
58+
matrix.round(mat)
59+
}
60+
61+
assert(
62+
type(mat) == array,
63+
message: "Transformtion matrix must be of type array, got: " + repr(mat))
64+
assert.eq(
65+
mat.len(),
66+
4,
67+
message: "Transformation matrix must be of size 4x4, got: " + repr(mat))
68+
69+
(ctx => {
70+
ctx.transform = matrix.mul-mat(ctx.transform, mat)
71+
return (ctx: ctx)
72+
},)
73+
}
74+
4875
/// Rotates the transformation matrix on the z-axis by a given angle or other axes when specified.
4976
///
5077
/// ```example
2.09 KB
Loading

tests/transform/transform/test.typ

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#set page(width: auto, height: auto)
2+
3+
#import "/src/lib.typ": *
4+
#import "/tests/helper.typ": *
5+
6+
#let arrow = draw.polygon((0, 0), 3)
7+
8+
#test-case({
9+
import draw: *
10+
11+
transform(none)
12+
arrow
13+
})
14+
15+
#test-case({
16+
import draw: *
17+
18+
transform(matrix.transform-rotate-z(45deg))
19+
transform(matrix.transform-scale((1, 0.5, 1)))
20+
arrow
21+
})

0 commit comments

Comments
 (0)