Skip to content

Commit

Permalink
Make rotations/angles consistent (#275)
Browse files Browse the repository at this point in the history
Closes #274.
  • Loading branch information
matthew-e-brown authored Nov 23, 2023
1 parent ab02c66 commit fd9c332
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@
} else if args.len() == 3 {
args
} else {
panic("Expected 2 or 3 positional arguments, got " + str(args.len))
panic("Expected 2 or 3 positional arguments, got " + str(args.len()))
}

coordinate.resolve-system(a)
Expand Down Expand Up @@ -530,6 +530,8 @@
angle
}

// Typst's `rotate` function is clockwise relative to x-axis, which is backwards from us
angle = angle * -1

let (width, height, ..) = if auto-size {
util.measure(ctx, body)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/angle.typ
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
message: "Angle z coordinates of all three points must be equal")

let (s, e, ss) = {
let s = vector.angle2(origin, a) * -1
let s = vector.angle2(origin, a)
if s < 0deg { s += 360deg }
let e = vector.angle2(origin, b) * -1
let e = vector.angle2(origin, b)
if e < 0deg { e += 360deg }

if s > e {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/axes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
if not axis.at("is-mirror", default: false) {
let is-left-right = side in ("west", "east")
let angle = if is-left-right {
-90deg
90deg
} else {
0deg
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/decorations.typ
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
// pointing upwards, so we set the origin and rotate the entire group accordingly
let (_, start, end) = coordinate.resolve(ctx, start, end)
set-origin(start)
rotate(vector.angle2(start, end))
rotate(vector.angle2(start, end) * -1)

let length = vector.dist(start, end)
let middle = aspect * length
Expand Down
3 changes: 2 additions & 1 deletion src/vector.typ
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@

/// Calculate angle between two points and the x-axis in 2d space
#let angle2(a, b) = {
return calc.atan2(a.at(1) - b.at(1), a.at(0) - b.at(0)) + 90deg
// Typst's atan2 is (x, y) order, not (y, x)
return calc.atan2(b.at(0) - a.at(0), b.at(1) - a.at(1))
}

/// Calculate angle between three points
Expand Down
4 changes: 2 additions & 2 deletions tests/axes/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
set-style(axes: (stroke: blue))
set-style(axes: (left: (tick: (stroke: green + 2pt))))
set-style(axes: (bottom: (tick: (stroke: red, length: .5,
label: (angle: -90deg,
label: (angle: 90deg,
anchor: "east")))))
set-style(axes: (right: (tick: (label: (offset: .2,
angle: 45deg,
angle: -45deg,
anchor: "north-west"), length: -.1))))
axes.scientific(size: (6, 6),
frame: "set",
Expand Down
Binary file modified tests/content/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/content/rotate/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fd9c332

Please sign in to comment.