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

decoration: Allow dynamic amplitudes #756

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
decoration: Add square decoration
johannes-wolf committed Dec 6, 2024
commit 8aa2d6a016cb1d2491a31897482b32c2aefb8607
2 changes: 1 addition & 1 deletion src/lib/decorations.typ
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#import "decorations/brace.typ": brace, brace-default-style, flat-brace, flat-brace-default-style
#import "decorations/path.typ": zigzag, wave, coil
#import "decorations/path.typ": zigzag, wave, coil, square
91 changes: 83 additions & 8 deletions src/lib/decorations/path.typ
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
/// The following types are supported:
/// - float
/// - function ratio -> float (the segment ratio is given as argument)
/// - array of floats (the rounded segment ratio is used as index)
/// - array of floats (the rounded down segment number is used as index modulo the array length)
amplitude: 1,
/// Decoration start
start: 0%,
@@ -63,11 +63,19 @@
factor: 150%,
)

// Square default style
#let square-default-style = (
..default-style,
/// Midpoint factor
factor: 50%,
)

#let resolve-amplitude(amplitude, segment, num-segments) = {
segment = calc.max(0, calc.min(segment, num-segments))
return if type(amplitude) == function {
(amplitude)(segment / (num-segments - 1) * 100%)
(amplitude)(segment / num-segments * 100%)
} else if type(amplitude) == array {
amplitude.at(int(calc.max(0, calc.round(segment / (num-segments - 1) * (amplitude.len() - 1)))), default: 0)
amplitude.at(calc.rem(int(segment), amplitude.len()), default: 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered amplitude.at(calc.rem(int(2*segment), amplitude.len()), default: 0)? That gives the user control over individual peaks and troughs. With int(segment), you can only control them in full-wavelength pairs.

} else {
amplitude
}
@@ -183,12 +191,12 @@

(p0, p1) = util.revert-transform(ctx.transform, p0, p1)

let dir = vector.sub(p1, p0)
let norm = vector.norm(vector.cross(dir, if p0.at(2) != p1.at(2) {
let dir = vector.norm(vector.sub(p1, p0))
let norm = vector.cross(dir, if p0.at(2) != p1.at(2) {
style.z-up
} else {
style.xy-up
}))
})

pts += fn(i, p0, p1, norm)
}
@@ -238,7 +246,7 @@
let f = .25 - (50% - style.factor) / 50% * .25
let q-dir = vector.scale(ab, f)
let up = vector.scale(norm, resolve-amplitude(style.amplitude, i + .25, num-segments) / 2)
let down = vector.scale(up, -resolve-amplitude(style.amplitude, i + .75, num-segments) / 2)
let down = vector.scale(norm, -resolve-amplitude(style.amplitude, i + .75, num-segments) / 2)

let m1 = vector.add(vector.add(a, q-dir), up)
let m2 = vector.add(vector.sub(b, q-dir), down)
@@ -395,7 +403,7 @@
//
let fn(i, a, b, norm) = {
let ab = vector.sub(b, a)
let up = vector.scale(norm, resolve-amplitude(style.amplitude, i + .25, num-segments) / 2)
let up = vector.scale(norm, +resolve-amplitude(style.amplitude, i + .25, num-segments) / 2)
let down = vector.scale(norm, -resolve-amplitude(style.amplitude, i + .75, num-segments) / 2)

let ma = vector.add(vector.add(a, vector.scale(ab, .25)), up)
@@ -421,3 +429,70 @@
close: close,
..style)
})

/// Draw a square-wave along a path using a line-strip
///
/// The number of phases can be controlled via the `segments` or `segment-length` style key, and the width via `amplitude`.
///
/// ```typc example
/// line((0,0), (2,1), stroke: gray)
/// cetz.decorations.square(line((0,0), (2,1)), amplitude: .25, start: 10%, stop: 90%)
/// ```
///
/// - target (drawable): Target path
/// - close (auto,bool): Close the path
/// - name (none,string): Element name
/// - ..style (style): Style
///
/// ## Styling
/// *Root*: `squre`
///
/// - factor (ratio) = 50% Square-Wave midpoint
#let square(target, close: auto, name: none, ..style) = draw.get-ctx(ctx => {
let style = styles.resolve(ctx, merge: style.named(),
base: square-default-style, root: "square")

let (segments, close) = get-segments(ctx, target)
let style = resolve-style(ctx, segments, style)
let num-segments = style.segments
let factor = calc.max(0, calc.min(style.factor / 100%, 1))

// Return a list of points for the line-strip
//
// +----+ ▲
// | | │ Up
// ..a....m....b.. '
// | |
// +----+
//
let fn(i, a, b, norm) = {
let ab = vector.sub(b, a)
let up = vector.scale(norm, +resolve-amplitude(style.amplitude, i + .25, num-segments) / 2)
let down = vector.scale(norm, -resolve-amplitude(style.amplitude, i + .75, num-segments) / 2)
let m = vector.add(a, vector.scale(ab, factor))

if not close {
if i == 0 {
return (a, vector.add(a, up),
vector.add(m, up), vector.add(m, down),
vector.add(b, down))
} else if i == num-segments - 1 {
return (vector.add(a, up),
vector.add(m, up), vector.add(m, down),
vector.add(b, down), b)
}
}

return (vector.add(a, up),
vector.add(m, up), vector.add(m, down),
vector.add(b, down))
}

return draw.merge-path(
finalize-path(ctx, segments, style, draw.line(
.._path-effect(ctx, segments, fn, close: close, style),
close: close), close: close) ,
name: name,
close: close,
..style)
})
91 changes: 25 additions & 66 deletions tests/decorations/path/test.typ
Original file line number Diff line number Diff line change
@@ -2,61 +2,23 @@
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#import decorations: zigzag, coil, wave
#import decorations: zigzag, coil, wave, square

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

zigzag(line((0,0), (4,0)))
zigzag(line((0,1), (4,1)), amplitude: .5)
zigzag(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
zigzag(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))

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

wave(line((0,0), (4,0)))
wave(line((0,1), (4,1)), amplitude: .5)
wave(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
wave(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))

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

coil(line((0,0), (4,0)))
coil(line((0,1), (4,1)), amplitude: .5)
coil(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
coil(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))
#let all-fns = (zigzag, coil, wave, square)

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

zigzag(hobby((0,0), (4,0), (6,2)))
}))
fn(line((0,0), (4,0)))
fn(line((0,1), (4,1)), amplitude: .5)
fn(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
fn(line((0,3), (4,3)), amplitude: (0, .5, 1))
}, args: all-fns)

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

coil(hobby((0,0), (4,0), (6,2)))
}))

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

wave(hobby((0,0), (4,0), (6,2)))
}))

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

set-style(radius: .9)
zigzag(circle((0,0)), amplitude: .2, segments: 20, factor: 0%)
zigzag(circle((0,2)), amplitude: .2, segments: 20, factor: 50%, stroke: blue)
zigzag(circle((0,4)), amplitude: .2, segments: 20, factor: 100%, stroke: red)
}))
fn(hobby((0,0), (4,0), (6,2)))
}, args: all-fns)

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
@@ -76,33 +38,30 @@
wave(circle((0,4)), amplitude: .2, segments: 20, tension: 1, stroke: red)
}))


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

zigzag(line((0,0), (3,0)), start: 10%, stop: 90%)
zigzag(line((0,2), (3,2)), start: 1, stop: 2)
})
set-style(radius: .9)
square(circle((0,2)), amplitude: .2, segments: 20)
}))

#test-case({
#test-case(fn => {
import draw: *

coil(line((0,0), (3,0)), start: 10%, stop: 90%)
coil(line((0,2), (3,2)), start: 1, stop: 2)
})
fn(line((0,0), (3,0)), start: 10%, stop: 90%, amplitude: .5)
fn(line((0,1), (3,1)), start: 1, stop: 2, amplitude: .5)
}, args: all-fns)

#test-case({
#test-case(fn => {
import draw: *

wave(line((0,0), (3,0)), start: 10%, stop: 90%)
wave(line((0,2), (3,2)), start: 1, stop: 2)
})
fn(line((0,0,-1), (0,0,1)), start: 10%, stop: 90%)
}, args: all-fns)

#test-case({
#test-case(factor => {
import draw: *

wave(line((0,0,-1), (0,0,1)), start: 10%, stop: 90%)
})
square(line((0,0), (3,0)), factor: factor)
}, args: (25%, 50%, 75%))

#test-case({
import draw: *