#[CeTZ] How to rotate of a calculated angle?

5 messages · Page 1 of 1 (latest)

alpine ember
#

In Cetz is very easy to rotate by a fixed angle. rotate(45deg) for example. But if I want to calculate the angle by two points that are indicated as anchors (string type)?
Ho can I convert the anchor type to a vector, calculate atan for example and then rotate of that amount?
Thanks a lot to everybody!!

serene fable
#

This is not that easy right now, but doable:

#import "@preview/cetz:0.1.1"
#cetz.canvas({
  import cetz.draw: *

  line((0,0), (2,2), name: "l")
  
  get-ctx(ctx => {
    let (x,y,..) = cetz.coordinate.resolve(ctx, "l.start")
    let (x2,y2,..) = cetz.coordinate.resolve(ctx, "l.end")
    let a = calc.atan2(y2 - y, x2 - x)
    rotate(a)

    rect((1,0), (2,1), fill: blue)
  })
})

You can use cetz.coordinate.resolve with get-ctx (or group(ctx => {...}) to resolve an anchor to a vector.

alpine ember
#

Wonderful! Thanks a lot, it was very difficult to get it without your help.

eternal falcon
#

the resolve function and such dont seem to be part of the manual so it is hard to find

serene fable
#

Yes, that is true. I'll add them.