From 59b638e4e45bcdde051d130e5e64fe5e83acc374 Mon Sep 17 00:00:00 2001 From: 0HyperCube <78500760+0HyperCube@users.noreply.github.com> Date: Sun, 27 Nov 2022 18:57:15 +0000 Subject: [PATCH] Fix Crashing on Smoothing Vertex (#868) Fix occasional crashing on smoothing a vertex in a path by double clicking. --- .../tool/common_functionality/shape_editor.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/editor/src/messages/tool/common_functionality/shape_editor.rs b/editor/src/messages/tool/common_functionality/shape_editor.rs index 73d3ac9a..c98e7a15 100644 --- a/editor/src/messages/tool/common_functionality/shape_editor.rs +++ b/editor/src/messages/tool/common_functionality/shape_editor.rs @@ -375,13 +375,13 @@ impl ShapeEditor { let length_next = next_position.map(|point| (point.position - anchor_position).length() / 3.); // Use the position relative to the anchor - let relative_previous_normalised = previous_position.map(|point| (point.position - anchor_position).normalize()); - let relative_next_normalised = next_position.map(|point| (point.position - anchor_position).normalize()); + let previous_angle = previous_position.map(|point| (point.position - anchor_position)).map(|pos| pos.y.atan2(pos.x)); + let next_angle = next_position.map(|point| (point.position - anchor_position)).map(|pos| pos.y.atan2(pos.x)); // The direction of the handles is either the perpendicular vector to the sum of the anchors' positions or just the anchor's position (if only one) - let handle_direction = match (relative_previous_normalised, relative_next_normalised) { - (Some(previous), Some(next)) => DVec2::new(previous.y + next.y, -(previous.x + next.x)), - (None, Some(val)) => -val, + let handle_direction = match (previous_angle, next_angle) { + (Some(previous), Some(next)) => (previous + next) / 2. + core::f64::consts::FRAC_PI_2, + (None, Some(val)) => core::f64::consts::PI + val, (Some(val), None) => val, (None, None) => return None, }; @@ -397,10 +397,13 @@ impl ShapeEditor { .into(), ); - let mut handle_vector = handle_direction.normalize(); + let (sin, cos) = handle_direction.sin_cos(); + let mut handle_vector = DVec2::new(cos, sin); // Flip the vector if it is not facing towards the same direction as the anchor - if relative_previous_normalised.filter(|pos| pos.dot(handle_vector) < 0.).is_some() || relative_next_normalised.filter(|pos| pos.dot(handle_vector) > 0.).is_some() { + if previous_position.filter(|pos| (pos.position - anchor_position).normalize().dot(handle_vector) < 0.).is_some() + || next_position.filter(|pos| (pos.position - anchor_position).normalize().dot(handle_vector) > 0.).is_some() + { handle_vector = -handle_vector; }