Repository: GraphiteEditor/Graphite
File: editor/src/messages/tool/common_functionality/shape_editor.rs (Rust)

Code before:
	}

	pub fn closest_point_to_viewport(&self) -> DVec2 {
		self.bezier_point_to_viewport
	}

	/// Updates this [`ClosestSegment`] with the viewport-space location of the closest point on the segment to the given mouse position.
	pub fn update_closest_point(&mut self, document_metadata: &DocumentMetadata, mouse_position: DVec2) {
		let transform = document_metadata.transform_to_viewport(self.layer);
		let layer_mouse_pos = transform.inverse().transform_point2(mouse_position);

		let t = self.bezier.project(layer_mouse_pos).clamp(0., 1.);
		self.t = t;

		let bezier_point = self.bezier.evaluate(TValue::Parametric(t));
		let bezier_point = transform.transform_point2(bezier_point);
		self.bezier_point_to_viewport = bezier_point;
	}

	pub fn distance_squared(&self, mouse_position: DVec2) -> f64 {
		self.bezier_point_to_viewport.distance_squared(mouse_position)
	}

	pub fn too_far(&self, mouse_position: DVec2, tolerance: f64) -> bool {
		let dist_sq = self.distance_squared(mouse_position);
		(tolerance).powi(2) < dist_sq
	}

	pub fn handle_positions(&self, document_metadata: &DocumentMetadata) -> (Option<DVec2>, Option<DVec2>) {
		// Transform to viewport space
		let transform = document_metadata.transform_to_viewport(self.layer);

		// Split the Bezier at the parameter `t`
		let [first, second] = self.bezier.split(TValue::Parametric(self.t));

		// Transform the handle positions to viewport space
		let first_handle = first.handle_end().map(|handle| transform.transform_point2(handle));
		let second_handle = second.handle_start().map(|handle| transform.transform_point2(handle));

		(first_handle, second_handle)
	}

	pub fn adjusted_insert(&self, responses: &mut VecDeque<Message>) -> PointId {
		let layer = self.layer;
		let [first, second] = self.bezier.split(TValue::Parametric(self.t));

		// Point
		let midpoint = PointId::generate();
		let modification_type = VectorModificationType::InsertPoint { id: midpoint, position: first.end };
		responses.add(GraphOperationMessage::Vector { layer, modification_type });


Review comment (suggestion):
```suggestion
		tolerance.powi(2) < self.distance_squared(mouse_position)
```

Code after:
	}

	pub fn points(&self) -> [PointId; 2] {
		self.points
	}

	pub fn closest_point_to_viewport(&self) -> DVec2 {
		self.bezier_point_to_viewport
	}

	pub fn closest_point(&self, document_metadata: &DocumentMetadata) -> DVec2 {
		let transform = document_metadata.transform_to_viewport(self.layer);
		let bezier_point = self.bezier.evaluate(TValue::Parametric(self.t));
		transform.transform_point2(bezier_point)
	}

	/// Updates this [`ClosestSegment`] with the viewport-space location of the closest point on the segment to the given mouse position.
	pub fn update_closest_point(&mut self, document_metadata: &DocumentMetadata, mouse_position: DVec2) {
		let transform = document_metadata.transform_to_viewport(self.layer);
		let layer_mouse_pos = transform.inverse().transform_point2(mouse_position);

		let t = self.bezier.project(layer_mouse_pos).clamp(0., 1.);
		self.t = t;

		let bezier_point = self.bezier.evaluate(TValue::Parametric(t));
		let bezier_point = transform.transform_point2(bezier_point);
		self.bezier_point_to_viewport = bezier_point;
	}

	pub fn distance_squared(&self, mouse_position: DVec2) -> f64 {
		self.bezier_point_to_viewport.distance_squared(mouse_position)
	}

	pub fn too_far(&self, mouse_position: DVec2, tolerance: f64) -> bool {
		tolerance.powi(2) < self.distance_squared(mouse_position)
	}

	pub fn handle_positions(&self, document_metadata: &DocumentMetadata) -> (Option<DVec2>, Option<DVec2>) {
		// Transform to viewport space
		let transform = document_metadata.transform_to_viewport(self.layer);

		// Split the Bezier at the parameter `t`
		let [first, second] = self.bezier.split(TValue::Parametric(self.t));

		// Transform the handle positions to viewport space
		let first_handle = first.handle_end().map(|handle| transform.transform_point2(handle));
		let second_handle = second.handle_start().map(|handle| transform.transform_point2(handle));

		(first_handle, second_handle)
	}