Is there a way to rotate a modpattern?

I would like to rotate a pattern created by modpattern; is there a way to do this?

#import "@preview/modpattern:0.1.0": modpattern

#let hatch(angle) = modpattern((20pt, 20pt))[
  // how to incorporate angle? This particular pattern is effectively 45 degrees
  #move(dx: 50%, line(start: (0%, 100%), end: (100%, 0%)))
]

#rect(fill: hatch(10deg), width: 8cm, height: 5cm, stroke: 1pt)

I am not familiar with modpattern, but it seems to rely on the native Typst line which can be created without start and end and instead angle.

Here’s your same output created with angle:

#import "@preview/modpattern:0.1.0": modpattern

#let hatch(angle) = modpattern((20pt, 20pt))[
  #move(dx: 50%, line(angle: -45deg))
]

#rect(fill: hatch(10deg), width: 8cm, height: 5cm, stroke: 1pt)
1 Like

Because the patterns have to tile properly, adjusting the angle of the line directly doesn’t work.

Adjusting the width or height of the piece that gets tiled will affect the perceived angle of the lines.

Code that creates the grid
#import "@preview/modpattern:0.1.0": modpattern

#let hatch(angle, width, height) = modpattern((width, height))[
  #move(dx: 50%, line(start: (0%, 100%), end: (100%, 0%)))
]

//Range of lengths to compare
#let rng = range(5, 40, step: 5)

//Function to display the pattern
#let demo(w, h) = rect(fill: hatch(10deg, w * 1pt, h * 1pt), width: 2cm, height: 2cm, stroke: 1pt)

//Create width, height pairs
#let combinations = for w in rng { for h in rng{ (w, h) } }.chunks(2)

//Create rows of all patterns and zip the height text into it
#let rows = rng.map(v => str(v) + [pt]).zip(combinations.map(combo => demo(..combo)).chunks(rng.len()))

#grid(
  align: center + horizon,
  columns: rng.len() + 1,
  
  [height#sym.arrow.r#linebreak()width#sym.arrow.b], ..rng.map(v => str(v) + [pt]),
  ..rows.flatten()
)

The obvious downside to this method is you don’t control the angle directly. It could be calculated, but I have not done that here. This might not actually be a downside for you, though.

2 Likes