Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
angle: Streamline behaviour
Browse files Browse the repository at this point in the history
johannes-wolf committed Oct 19, 2024
1 parent 98ac77a commit 6ba76f9
Showing 3 changed files with 39 additions and 30 deletions.
44 changes: 19 additions & 25 deletions src/lib/angle.typ
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
mark: auto,
)

/// Draw an angle between `a` and `b` through origin `origin`
/// Draw an angle counter-clock-wise between `a` and `b` through origin `origin`
///
/// ```typc example
/// line((0,0), (1,1.5), name: "a")
@@ -31,7 +31,7 @@
/// - origin (coordinate): Angle origin
/// - a (coordinate): Coordinate of side `a`, containing an angle between `origin` and `b`.
/// - b (coordinate): Coordinate of side `b`, containing an angle between `origin` and `a`.
/// - inner (bool): Draw the smaller (inner) angle if true, otherwise the outer angle gets drawn.
/// - inner (bool): Draw the angle angle a-origin-b if true, otherwise the angle from b-origin-a gets drawn.
/// - label (none,content,function): Draw a label at the angles "label" anchor. If label is a function, it gets the angle value passed as argument. The function must be of the format `angle => content`.
/// - name (none,str): Element name, used for querying anchors.
/// - ..style (style): Style key-value pairs.
@@ -65,32 +65,26 @@
assert(origin.at(2) == a.at(2) and a.at(2) == b.at(2),
message: "Angle z coordinates of all three points must be equal")

let (s, e, ss) = {
let (start, delta) = {
let s = vector.angle2(origin, a)
if s < 0deg { s += 360deg }

let e = vector.angle2(origin, b)
if e < 0deg { e += 360deg }

if s > e {
(s, e) = (e, s)
if e < s {
e += 360deg
}

if inner == true {
let d = vector.angle(a, origin, b)
if e - s > 180deg {
(s, e) = (e, e + d)
} else {
(s, e) = (s, s + d)
}
} else if inner == false {
if e - s < 180deg {
let d = 360deg - vector.angle(a, origin, b)
(s, e) = (e, e + d)
}
if inner {
(s, (e - s))
} else {
(s, -(360deg - (e - s)))
}
(s, e, (s + e) / 2)
}

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%
@@ -103,28 +97,28 @@
}
let (ra, _) = util.resolve-radius(style.label-radius).map(util.resolve-number.with(ctx))

let label-pt = vector.add(origin, (calc.cos(ss) * ra, calc.sin(ss) * ra, 0))
let start-pt = vector.add(origin, (calc.cos(s) * r, calc.sin(s) * r, 0))
let end-pt = vector.add(origin, (calc.cos(e) * r, calc.sin(e) * r, 0))
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))
draw.anchor("origin", origin)
draw.anchor("label", label-pt)
draw.anchor("start", start-pt)
draw.anchor("end", end-pt)
draw.anchor("a", a)
draw.anchor("b", b)

if s != e {
if delta != 0deg {
if style.fill != none {
draw.arc(origin, start: s, stop: e, anchor: "origin",
draw.arc(origin, start: start, delta: delta, anchor: "origin",
name: "arc", ..style, radius: r, mode: "PIE", mark: none, stroke: none)
}
if style.stroke != none {
draw.arc(origin, start: s, stop: e, anchor: "origin",
draw.arc(origin, start: start, delta: delta, anchor: "origin",
name: "arc", ..style, radius: r, fill: none)
}
}

let label = if type(label) == function { label(e - s) } else { label }
let label = if type(label) == function { label(calc.abs(delta)) } else { label }
if label != none {
draw.content(label-pt, label)
}
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.
25 changes: 20 additions & 5 deletions tests/angle/test.typ
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#box(stroke: 2pt + red, canvas(length: 1cm, {
#test-case({
import draw: *
import angle: angle

@@ -16,7 +17,7 @@
}
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
#test-case({
import draw: *
import angle: angle

@@ -31,7 +32,7 @@
}
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
#test-case({
import draw: *
import angle: angle

@@ -69,15 +70,15 @@

line(a, b, c)
set-style(angle: (stroke: red, label-radius: 1))
angle(b, a, c, mark: (start: ">", end: ">"),
angle(b, c, a, mark: (start: ">", end: ">"),
inner: true, label: $omega$)

translate((2,0,0))

line(a, b, c)
set-style(stroke: blue)
set-style(angle: (stroke: auto, radius: 1, label-radius: .5))
angle(b, c, a, mark: (start: ">", end: ">"),
angle(b, c, a, mark: (start: "|", end: ">"),
inner: false, label: $alpha$, name: "alpha")

set-style(stroke: black)
@@ -86,3 +87,17 @@
circle("alpha.start", radius: .25)
circle("alpha.end", radius: .25)
}))

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

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

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

angle((0,0), (1,0), (0,1), mark: (end: ">"), inner: false)
})

0 comments on commit 6ba76f9

Please sign in to comment.