use gpui_kit::{PathBuilder, Pixels, StrokeOptions, point, px};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PathPoint {
pub x: f32,
pub y: f32,
}
impl PathPoint {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn lerp(self, other: Self, t: f32) -> Self {
Self {
x: self.x + (other.x - self.x) * t,
y: self.y + (other.y - self.y) * t,
}
}
pub fn distance(self, other: Self) -> f32 {
(other.x - self.x).hypot(other.y - self.y)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PolylinePath {
pub points: Vec<PathPoint>,
}
impl PolylinePath {
pub fn new(points: Vec<PathPoint>) -> Self {
Self { points }
}
pub fn total_length(&self) -> f32 {
self.points.windows(2).map(|w| w[0].distance(w[1])).sum()
}
pub fn point_at(&self, u: f32) -> Option<PathPoint> {
if self.points.is_empty() {
return None;
}
if self.points.len() == 1 {
return Some(self.points[0]);
}
let total = self.total_length();
if total <= f32::EPSILON {
return Some(self.points[0]);
}
let target = u.clamp(0.0, 1.0) * total;
let mut acc = 0.0_f32;
for w in self.points.windows(2) {
let seg = w[0].distance(w[1]);
if acc + seg >= target && seg > f32::EPSILON {
let local = ((target - acc) / seg).clamp(0.0, 1.0);
return Some(w[0].lerp(w[1], local));
}
acc += seg;
}
self.points.last().copied()
}
pub fn slice(&self, start: f32, end: f32) -> Self {
let start = start.clamp(0.0, 1.0);
let end = end.clamp(0.0, 1.0);
if self.points.len() < 2 || end <= start {
return Self { points: Vec::new() };
}
let total = self.total_length();
if total <= f32::EPSILON {
return Self {
points: self.points.clone(),
};
}
let start_dist = start * total;
let end_dist = end * total;
let mut out = Vec::new();
let mut acc = 0.0_f32;
for w in self.points.windows(2) {
let seg = w[0].distance(w[1]);
let next_acc = acc + seg;
if next_acc >= start_dist && acc <= end_dist && seg > f32::EPSILON {
let t0 = ((start_dist - acc) / seg).clamp(0.0, 1.0);
let t1 = ((end_dist - acc) / seg).clamp(0.0, 1.0);
let p0 = w[0].lerp(w[1], t0);
let p1 = w[0].lerp(w[1], t1);
if out.is_empty() {
out.push(p0);
}
out.push(p1);
}
acc = next_acc;
}
Self { points: out }
}
pub fn resample(&self, count: usize) -> Self {
if count == 0 || self.points.is_empty() {
return Self { points: Vec::new() };
}
if count == 1 {
return Self {
points: vec![self.points[0]],
};
}
let points = (0..count)
.filter_map(|i| self.point_at(i as f32 / (count - 1) as f32))
.collect();
Self { points }
}
pub fn morph(&self, other: &Self, t: f32, sample_count: usize) -> Self {
let n = sample_count
.max(self.points.len())
.max(other.points.len())
.max(2);
let a = self.resample(n);
let b = other.resample(n);
let t = t.clamp(0.0, 1.0);
let points = a
.points
.iter()
.zip(b.points.iter())
.map(|(&pa, &pb)| pa.lerp(pb, t))
.collect();
Self { points }
}
pub fn to_stroke_builder(
&self,
offset_x: Pixels,
offset_y: Pixels,
stroke_width: Pixels,
) -> Option<PathBuilder> {
if self.points.len() < 2 {
return None;
}
let mut builder = PathBuilder::stroke(stroke_width);
let ox = f32::from(offset_x);
let oy = f32::from(offset_y);
builder.move_to(point(px(ox + self.points[0].x), px(oy + self.points[0].y)));
for pt in &self.points[1..] {
builder.line_to(point(px(ox + pt.x), px(oy + pt.y)));
}
let _ = StrokeOptions::default();
Some(builder)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn path_slice_truncates_proportionally_to_length() {
let check = PolylinePath::new(vec![
PathPoint::new(0.0, 0.0),
PathPoint::new(10.0, 0.0),
PathPoint::new(10.0, 10.0),
]);
assert!((check.total_length() - 20.0).abs() < 1e-4);
let half = check.slice(0.0, 0.5);
assert!((half.total_length() - 10.0).abs() < 1e-4);
assert_eq!(half.points.last().copied(), Some(PathPoint::new(10.0, 0.0)));
let quarter = check.slice(0.25, 0.75);
assert!((quarter.total_length() - 10.0).abs() < 1e-4);
}
#[test]
fn path_morph_aligns_mismatched_vertex_counts() {
let line = PolylinePath::new(vec![PathPoint::new(0.0, 0.0), PathPoint::new(20.0, 0.0)]);
let v_shape = PolylinePath::new(vec![
PathPoint::new(0.0, 10.0),
PathPoint::new(10.0, 20.0),
PathPoint::new(20.0, 10.0),
]);
let mid = line.morph(&v_shape, 0.5, 16);
assert_eq!(mid.points.len(), 16);
assert!((mid.points[0].y - 5.0).abs() < 1e-3);
assert!((mid.points[15].y - 5.0).abs() < 1e-3);
}
}