Skip to content

Commit

Permalink
angle: Resolve radius (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf authored Nov 28, 2024
1 parent 24f2c21 commit 4f6e196
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/lib/angle.typ
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
..style
) = draw.group(name: name, ctx => {
let style = styles.resolve(ctx.style, merge: style.named(), base: default-style, root: "angle")
let radius = util.resolve-number(ctx, style.radius)
let label-radius = util.resolve-number(ctx, style.label-radius)

let (ctx, origin) = coordinate.resolve(ctx, origin)
let (ctx, a, b) = coordinate.resolve(ctx, a, b, update: false)

Expand Down Expand Up @@ -91,20 +94,18 @@
let mid = start + delta / 2

// Radius can be relative to the min-distance between origin-a and origin-b
if type(style.radius) == ratio {
style.radius = style.radius * calc.min(vector.dist(origin, a), vector.dist(origin, b)) / 100%
if type(radius) == ratio {
radius = radius * calc.min(vector.dist(origin, a), vector.dist(origin, b)) / 100%
}
let (r, _) = util.resolve-radius(style.radius).map(util.resolve-number.with(ctx))

// Label radius can be relative to radius
if type(style.label-radius) == ratio {
style.label-radius = style.label-radius * style.radius / 100%
if type(label-radius) == ratio {
label-radius = label-radius * radius / 100%
}
let (ra, _) = util.resolve-radius(style.label-radius).map(util.resolve-number.with(ctx))

let label-pt = vector.add(origin, (calc.cos(mid) * ra, calc.sin(mid) * ra, 0))
let start-pt = vector.add(origin, (calc.cos(start) * r, calc.sin(start) * r, 0))
let end-pt = vector.add(origin, (calc.cos(start + delta) * r, calc.sin(start + delta) * r, 0))
let label-pt = vector.add(origin, (calc.cos(mid) * label-radius, calc.sin(mid) * label-radius, 0))
let start-pt = vector.add(origin, (calc.cos(start) * radius, calc.sin(start) * radius, 0))
let end-pt = vector.add(origin, (calc.cos(start + delta) * radius, calc.sin(start + delta) * radius, 0))
draw.anchor("origin", origin)
draw.anchor("label", label-pt)
draw.anchor("start", start-pt)
Expand All @@ -115,11 +116,11 @@
if delta != 0deg {
if style.fill != none {
draw.arc(origin, start: start, delta: delta, anchor: "origin",
name: "arc", ..style, radius: r, mode: "PIE", mark: none, stroke: none)
name: "arc", ..style, radius: radius, mode: "PIE", mark: none, stroke: none)
}
if style.stroke != none {
draw.arc(origin, start: start, delta: delta, anchor: "origin",
name: "arc", ..style, radius: r, fill: none)
name: "arc", ..style, radius: radius, fill: none)
}
}

Expand Down
Binary file modified tests/angle/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/angle/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@

angle((0,0), (1,0), (0,1), mark: (end: ">"), direction: "cw")
})

#test-case({
import draw: *
import angle: *

angle((0,0), (1,0), (0,1), radius: .5cm)
angle((0,0), (1,0), (0,1), radius: 75%, stroke: blue)
angle((0,0), (1,0), (0,1), radius: 1, stroke: green)
})

0 comments on commit 4f6e196

Please sign in to comment.