use super::options::Decoration;
use super::path::Sub;
use super::{Point, Segment};
pub fn apply(
sub: &Sub,
decoration: Decoration,
segment_length: f64,
amplitude: f64,
aspect: f64,
) -> Sub {
if segment_length <= 0.0 {
return sub.clone();
}
let mut out = Sub {
start: sub.start,
segments: Vec::new(),
closed: false,
};
let mut from = sub.start;
for segment in &sub.segments {
match segment {
Segment::Line(to) => {
let run = Run::new(from, *to);
match run.length > 0.0 {
true => run.draw(decoration, segment_length, amplitude, aspect, &mut out),
false => out.segments.push(Segment::Line(*to)),
}
from = *to;
}
other => {
out.segments.push(*other);
from = match other {
Segment::Curve(_, _, to) => *to,
Segment::Line(to) => *to,
};
}
}
}
match out.segments.is_empty() {
true => sub.clone(),
false => out,
}
}
struct Run {
at: Point,
to: Point,
along: Point,
across: Point,
length: f64,
}
impl Run {
fn new(from: Point, to: Point) -> Run {
let (dx, dy) = (to.0 - from.0, to.1 - from.1);
let length = dx.hypot(dy);
let along = match length == 0.0 {
true => (1.0, 0.0),
false => (dx / length, dy / length),
};
Run {
at: from,
to,
along,
across: (-along.1, along.0),
length,
}
}
fn point(&self, base: f64, x: f64, y: f64) -> Point {
(
self.at.0 + (base + x) * self.along.0 + y * self.across.0,
self.at.1 + (base + x) * self.along.1 + y * self.across.1,
)
}
fn draw(
&self,
decoration: Decoration,
length: f64,
amplitude: f64,
aspect: f64,
out: &mut Sub,
) {
match decoration {
Decoration::Snake => self.snake(length, amplitude, out),
Decoration::Zigzag => self.zigzag(length, amplitude, out),
Decoration::Saw => self.saw(length, amplitude, out),
Decoration::Brace => self.brace(amplitude, aspect, out),
}
}
fn snake(&self, length: f64, amplitude: f64, out: &mut Sub) {
let mut at = 0.0;
if self.length - at < 0.625 * length {
out.segments.push(Segment::Line(self.to));
return;
}
out.segments.push(Segment::Curve(
self.point(at, 0.125 * length, 0.0),
self.point(at, 0.1875 * length, amplitude),
self.point(at, 0.3125 * length, amplitude),
));
at += 0.3125 * length;
let mut down = true;
while self.length - at >= 0.8125 * length {
let (high, low) = match down {
true => (amplitude, -amplitude),
false => (-amplitude, amplitude),
};
let quarter = 0.25 * length;
let middle = (high + low) / 2.0;
out.segments.push(Segment::Curve(
self.point(at, 0.3620 * quarter, high),
self.point(at, 0.6740 * quarter, high + 0.4880 * (middle - high)),
self.point(at, quarter, middle),
));
out.segments.push(Segment::Curve(
self.point(
at,
quarter + 0.3260 * quarter,
middle + 0.5120 * (low - middle),
),
self.point(at, quarter + 0.6380 * quarter, low),
self.point(at, 2.0 * quarter, low),
));
at += 0.5 * length;
down = !down;
}
let side = match down {
true => amplitude,
false => -amplitude,
};
out.segments.push(Segment::Curve(
self.point(at, 0.125 * length, side),
self.point(at, 0.1875 * length, 0.0),
self.point(at, 0.3125 * length, 0.0),
));
out.segments.push(Segment::Line(self.to));
}
fn zigzag(&self, length: f64, amplitude: f64, out: &mut Sub) {
let half = 0.5 * length;
let mut at = 0.0;
if self.length < half {
out.segments.push(Segment::Line(self.to));
return;
}
out.segments
.push(Segment::Line(self.point(at, 0.25 * length, amplitude)));
at += half;
let mut up = false;
while self.length - at >= half {
let side = match up {
true => amplitude,
false => -amplitude,
};
out.segments
.push(Segment::Line(self.point(at, 0.25 * length, side)));
at += half;
up = !up;
}
let finish = self.point(at, 0.0, 0.0);
out.segments.push(Segment::Line(finish));
if (finish.0 - self.to.0).hypot(finish.1 - self.to.1) > 1e-6 {
out.segments.push(Segment::Line(self.to));
}
}
fn saw(&self, length: f64, amplitude: f64, out: &mut Sub) {
let mut at = 0.0;
while self.length - at >= length {
out.segments
.push(Segment::Line(self.point(at, length, amplitude)));
out.segments
.push(Segment::Line(self.point(at, length, 0.0)));
at += length;
}
out.segments.push(Segment::Line(self.to));
}
fn brace(&self, amplitude: f64, aspect: f64, out: &mut Sub) {
let total = self.length;
let mut yc = aspect * total;
yc = match 2.0 * amplitude > yc {
true => 0.5 * yc,
false => amplitude,
};
let mut xc = aspect * total - total;
xc = match -2.0 * amplitude < xc {
true => -0.5 * xc,
false => amplitude,
};
let middle = aspect * total;
out.segments.push(Segment::Curve(
self.point(0.0, 0.15 * yc, 0.3 * amplitude),
self.point(0.0, 0.5 * yc, 0.5 * amplitude),
self.point(0.0, yc, 0.5 * amplitude),
));
out.segments
.push(Segment::Line(self.point(middle, -yc, 0.5 * amplitude)));
out.segments.push(Segment::Curve(
self.point(middle, -0.5 * yc, 0.5 * amplitude),
self.point(middle, -0.15 * yc, 0.7 * amplitude),
self.point(middle, 0.0, amplitude),
));
out.segments.push(Segment::Curve(
self.point(middle, 0.15 * xc, 0.7 * amplitude),
self.point(middle, 0.5 * xc, 0.5 * amplitude),
self.point(middle, xc, 0.5 * amplitude),
));
out.segments
.push(Segment::Line(self.point(total, -xc, 0.5 * amplitude)));
out.segments.push(Segment::Curve(
self.point(total, -0.5 * xc, 0.5 * amplitude),
self.point(total, -0.15 * xc, 0.3 * amplitude),
self.point(total, 0.0, 0.0),
));
}
}
#[cfg(test)]
mod tests {
use super::*;
fn line(length: f64) -> Sub {
Sub {
start: (0.0, 0.0),
segments: vec![Segment::Line((length, 0.0))],
closed: false,
}
}
#[test]
fn a_snake_rises_waves_and_falls_back_to_the_line() {
let out = apply(&line(40.0), Decoration::Snake, 10.0, 2.5, 0.5);
assert!(matches!(out.segments[0], Segment::Curve(..)));
if let Segment::Curve(_, _, (x, y)) = out.segments[0] {
assert!(
(x - 3.125).abs() < 1e-9 && (y - 2.5).abs() < 1e-9,
"{x} {y}"
);
}
let last = out.segments.last().unwrap();
assert_eq!(*last, Segment::Line((40.0, 0.0)));
let lowest = out
.segments
.iter()
.map(|segment| match segment {
Segment::Line((_, y)) => *y,
Segment::Curve(_, _, (_, y)) => *y,
})
.fold(f64::MAX, f64::min);
assert!((lowest + 2.5).abs() < 1e-9, "{lowest}");
}
#[test]
fn a_line_too_short_for_a_snake_stays_a_line() {
let out = apply(&line(4.0), Decoration::Snake, 10.0, 2.5, 0.5);
assert_eq!(out.segments, vec![Segment::Line((4.0, 0.0))]);
}
#[test]
fn a_zigzag_puts_its_apexes_a_quarter_segment_in() {
let out = apply(&line(30.0), Decoration::Zigzag, 10.0, 2.5, 0.5);
assert_eq!(out.segments[0], Segment::Line((2.5, 2.5)));
assert_eq!(out.segments[1], Segment::Line((7.5, -2.5)));
assert_eq!(out.segments[2], Segment::Line((12.5, 2.5)));
assert!(out
.segments
.iter()
.all(|segment| matches!(segment, Segment::Line(_))));
}
#[test]
fn a_saw_rises_and_drops_at_the_same_place() {
let out = apply(&line(20.0), Decoration::Saw, 10.0, 2.5, 0.5);
assert_eq!(out.segments[0], Segment::Line((10.0, 2.5)));
assert_eq!(out.segments[1], Segment::Line((10.0, 0.0)));
assert_eq!(out.segments[2], Segment::Line((20.0, 2.5)));
}
#[test]
fn a_brace_spikes_at_the_aspect_and_returns_to_the_ends() {
let out = apply(&line(100.0), Decoration::Brace, 10.0, 2.5, 0.5);
let spike = match out.segments[2] {
Segment::Curve(_, _, at) => at,
other => panic!("a curve, not {other:?}"),
};
assert!((spike.0 - 50.0).abs() < 1e-9, "{spike:?}");
assert!((spike.1 - 2.5).abs() < 1e-9, "{spike:?}");
let end = match out.segments.last().unwrap() {
Segment::Curve(_, _, at) => *at,
other => panic!("a curve, not {other:?}"),
};
assert!(
(end.0 - 100.0).abs() < 1e-9 && end.1.abs() < 1e-9,
"{end:?}"
);
}
#[test]
fn a_decoration_turns_with_the_line_it_is_on() {
let up = Sub {
start: (0.0, 0.0),
segments: vec![Segment::Line((0.0, 30.0))],
closed: false,
};
let out = apply(&up, Decoration::Zigzag, 10.0, 2.5, 0.5);
assert_eq!(out.segments[0], Segment::Line((-2.5, 2.5)));
}
}