egui: Plot Arrows add method to specify tip_size (#3138)

Normally the tip_size will be dependent on the arrow length.
This patch adds a new method to explicitly set the arrow tip size.
This commit is contained in:
Nicolas Riebesel 2023-08-09 16:47:46 +02:00 committed by GitHub
parent e82ec74c5c
commit cbdf748a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -997,6 +997,7 @@ impl PlotItem for Points {
pub struct Arrows {
pub(super) origins: PlotPoints,
pub(super) tips: PlotPoints,
pub(super) tip_length: Option<f32>,
pub(super) color: Color32,
pub(super) name: String,
pub(super) highlight: bool,
@ -1007,6 +1008,7 @@ impl Arrows {
Self {
origins: origins.into(),
tips: tips.into(),
tip_length: None,
color: Color32::TRANSPARENT,
name: Default::default(),
highlight: false,
@ -1019,6 +1021,12 @@ impl Arrows {
self
}
/// Set the length of the arrow tips
pub fn tip_length(mut self, tip_length: f32) -> Self {
self.tip_length = Some(tip_length);
self
}
/// Set the arrows' color.
pub fn color(mut self, color: impl Into<Color32>) -> Self {
self.color = color.into();
@ -1044,6 +1052,7 @@ impl PlotItem for Arrows {
let Self {
origins,
tips,
tip_length,
color,
highlight,
..
@ -1062,7 +1071,11 @@ impl PlotItem for Arrows {
.for_each(|(origin, tip)| {
let vector = tip - origin;
let rot = Rot2::from_angle(std::f32::consts::TAU / 10.0);
let tip_length = vector.length() / 4.0;
let tip_length = if let Some(tip_length) = tip_length {
*tip_length
} else {
vector.length() / 4.0
};
let tip = origin + vector;
let dir = vector.normalized();
shapes.push(Shape::line_segment([origin, tip], stroke));