Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor on rect needs doc clarification #481

Open
uben0 opened this issue Jan 31, 2024 · 2 comments
Open

Anchor on rect needs doc clarification #481

uben0 opened this issue Jan 31, 2024 · 2 comments
Labels
core:draw ✏️ question ❔ Further information is requested

Comments

@uben0
Copy link

uben0 commented Jan 31, 2024

I'm just beginning to use CeTZ (awesome job BTW 🤩) and I could not find how to center rectangles. Maybe it's just a skill issue 😶‍🌫️

#rect((3, 0), (0.8, 0.8), name: "b", anchor: "center")

Setting the anchor to center doesn't change the a position in the rectangle, it remains what seems to be south-west. Which makes sens as this is what the documentation says:

a: Coordinate of the bottom left corner of the rectangle.

I wrote a wrapper to bypass this limitation:

#let rectc(center, dim, ..param) = {
    let x = center.at(0) - dim.at(0) / 2
    let y = center.at(1) - dim.at(1) / 2
    rect((x, y), (rel: dim), ..param)
}

But this scratched my head. How could such a trivial problem could remain unnoticed for so long? It has to be a problem with my comprehension. So I experimented with different anchors and I was very surprised by the behavior, that looked, at first glance, chaotic and nonsensical. But at the end, I got it:

  • a is bottom left
  • b is top right
  • the anchor applies a shift (or drag) to have the anchor point on the center (a + b) / 2

It makes sens after all. It is coherent. Knowing that, a rectangle can be centered by putting its anchor to north-east.

Request

I suggest to add a clarification in the documentation about this.

Or even maybe add an optional center: false parameter to change the meaning of a as the center instead of the bottom left corner. (Which is not equivalent to setting anchor to north-east)

@uben0 uben0 changed the title Anchor on rect makes little sens Anchor on rect needs doc clarification Jan 31, 2024
@johannes-wolf
Copy link
Member

johannes-wolf commented Jan 31, 2024

Note: the second argument of rect is an absolute position! If you want to pass a size, use (rel: (width, height)). Using relative coordinates is the correct way to add multiple coordinates in cetz.

You can get centered rects if you use relative coordinates:

rect((rel: (-1,-1), to: (...center...)), (rel: (2,2)), name: "b")

@johannes-wolf johannes-wolf added question ❔ Further information is requested core:draw ✏️ labels Jan 31, 2024
@johannes-wolf
Copy link
Member

Proposal: Change rect’s default anchor to its first parameter (a). This would allow the expected transformation to happen (anchor: "center"). The intersection code of line must be changed to use the center anchor instead of the default anchor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core:draw ✏️ question ❔ Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants