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

angle: Resolve radius #743

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
Loading