Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Nov 26, 2024
1 parent c09abaa commit e9e430d
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions crates/gpui/examples/painting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ impl PaintingViewer {
// draw a line
let mut path = Path::new(point(px(20.), px(100.)));
path.line_to(point(px(50.), px(160.)));
path.line_to(point(px(80.), px(100.)));
path.line_to(point(px(60.), px(100.)));
path.line_to(point(px(80.), px(140.)));
// go back to close the path
path.line_to(point(px(80.), px(101.)));
path.line_to(point(px(81.), px(141.)));
path.line_to(point(px(60.), px(101.)));
path.line_to(point(px(50.), px(161.)));
path.line_to(point(px(20.), px(101.)));
lines.push((path, gpui::black()));
Expand All @@ -39,7 +41,6 @@ impl PaintingViewer {
path.line_to(point(px(0.), px(25.)));
path.line_to(point(px(0.), px(5.)));
path.translate(point(px(220.), px(150.)));

lines.push((path, gpui::blue()));

// draw a ⭐
Expand Down Expand Up @@ -73,8 +74,7 @@ impl PaintingViewer {
square_bounds.lower_right(),
square_bounds.upper_right() + point(px(0.0), vertical_offset),
);
path.line_to(square_bounds.lower_left());
lines.push((path, gpui::black()));
lines.push((path, gpui::green()));

Self {
default_lines: lines.clone(),
Expand Down Expand Up @@ -137,18 +137,16 @@ impl Render for PaintingViewer {
let mut path = Path::new(points[0]);
for p in points.iter().skip(1) {
path.line_to(*p);
path.move_to(*p);
}

let mut last = points.last().unwrap();
for p in points.iter().rev() {
let mut offset_x = px(0.);
if last.x == p.x {
offset_x = STROKE_WIDTH;
}
let p1 = point(p.x + offset_x, p.y + STROKE_WIDTH);
path.line_to(p1);
path.move_to(p1);
let dx = p.x - last.x;
let dy = p.y - last.y;
let distance = (dx * dx + dy * dy).0.sqrt();
let offset_x = (STROKE_WIDTH * dy / distance).clamp(px(0.0), STROKE_WIDTH);
let offset_y = (STROKE_WIDTH * dx / distance).clamp(px(0.0), STROKE_WIDTH);
path.line_to(point(p.x + offset_x, p.y - offset_y));
last = p;
}

Expand Down

0 comments on commit e9e430d

Please sign in to comment.