Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widget::canvas::path::Builder::arc does not correctly sweep clockwise in all cases as documented #2669

Open
4 tasks done
letssolvesolvedproblems opened this issue Nov 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@letssolvesolvedproblems

Is your issue REALLY a bug?

  • My issue is indeed a bug!
  • I am not crazy! I will not fill out this form just to ask a question or request a feature. Pinky promise.

Is there an existing issue for this?

  • I have searched the existing issues.

Is this issue related to iced?

  • My hardware is compatible and my graphics drivers are up-to-date.

What happened?

When calling the arc method on iced_graphics::geometry::path::builder::Builder and supplying a iced_graphics::geometry::path::arc::Arc constructed like:

Arc {
    center: Point::new(0.0, 0.0),
    radius: 0.5,
    start_angle: Radians(0.0),
    end_angle: Radians(std::f32::consts::PI)
}

and another constructed like:

Arc {
    center: Point::new(0.0, 0.0),
    radius: 0.5,
    start_angle: Radians(std::f32::consts::PI),
    end_angle: Radians(0.0)
}

the same arc is drawn on the canvas. See below images, note that one center point has been moved so you can see it.
second_arc
first_arc

What is the expected behavior?

I would expect the sweep of the arc to go clockwise from start_angle to end_angle in both cases.

Version

master

Operating System

Windows

Do you have any log output?

No response

@letssolvesolvedproblems letssolvesolvedproblems added the bug Something isn't working label Nov 13, 2024
@letssolvesolvedproblems
Copy link
Author

I realized that my first post has too much baggage relating to the program I was showcasing the bug in. Here is a MRE that demonstrates the problem:

use iced::alignment::Horizontal;
use iced::widget::canvas::path::arc::Arc;
use iced::widget::canvas::{Frame, Path, Stroke};
use iced::widget::{button, canvas, text, Column};
use iced::{Element, Point, Radians};

fn main() -> iced::Result {
    iced::run("Arc sweep bug demo", ArcBugDemo::update, ArcBugDemo::view)
}

#[derive(Debug, Copy, Clone)]
enum Message {
    ToggleArc,
}

#[derive(Debug, Copy, Clone)]
enum ActiveArc {
    ZeroToPi,
    PiToZero,
}

impl ActiveArc {
    fn toggle(&mut self) {
        *self = match self {
            Self::PiToZero => Self::ZeroToPi,
            Self::ZeroToPi => Self::PiToZero,
        }
    }
}

struct ArcBugDemo {
    zero_to_pi: Arc,
    pi_to_zero: Arc,
    active: ActiveArc,
}

impl Default for ArcBugDemo {
    fn default() -> Self {
        Self {
            zero_to_pi: Arc {
                center: Point::new(150.0, 150.0),
                radius: 50.0,
                start_angle: Radians(0.0),
                end_angle: Radians(std::f32::consts::PI),
            },
            pi_to_zero: Arc {
                center: Point::new(150.0, 150.0),
                radius: 50.0,
                start_angle: Radians(std::f32::consts::PI),
                end_angle: Radians(0.0),
            },
            active: ActiveArc::ZeroToPi,
        }
    }
}

impl canvas::Program<Message> for ArcBugDemo {
    type State = ();

    fn draw(
        &self,
        _state: &Self::State,
        renderer: &iced::Renderer,
        theme: &iced::Theme,
        bounds: iced::Rectangle,
        _cursor: iced::mouse::Cursor,
    ) -> Vec<canvas::Geometry<iced::Renderer>> {
        let mut frame = Frame::new(renderer, bounds.size());
        let path = Path::new(|builder| {
            builder.arc(match self.active {
                ActiveArc::ZeroToPi => self.zero_to_pi,
                ActiveArc::PiToZero => self.pi_to_zero,
            });
        });
        frame.stroke(
            &path,
            Stroke::default()
                .with_color(theme.palette().danger)
                .with_width(5.0),
        );

        vec![frame.into_geometry()]
    }
}

impl ArcBugDemo {
    fn update(&mut self, message: Message) {
        match message {
            Message::ToggleArc => self.active.toggle(),
        }
    }

    fn view(&self) -> Element<Message> {
        Column::new()
            .push(canvas(self).width(300).height(300))
            .push(button("Toggle arc").on_press(Message::ToggleArc))
            .push(text(format!(
                "{:#?}",
                match self.active {
                    ActiveArc::ZeroToPi => self.zero_to_pi,
                    ActiveArc::PiToZero => self.pi_to_zero,
                }
            )))
            .align_x(Horizontal::Center)
            .padding(10)
            .spacing(10)
            .into()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant