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

Fix arc sector bounding box #441

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Conversation

jamwaffles
Copy link
Member

Hi! Thank you for helping out with Embedded Graphics development! Please:

  • Check that you've added passing tests and documentation
  • Add a simulator example(s) where applicable
  • Add a CHANGELOG.md entry in the Unreleased section under the appropriate heading (Added, Fixed, etc) and appropriate crate (embedded-graphics, simulator, tinytga, tinybmp) if your changes affect the public API
  • Run rustfmt on the project
  • Run just build (Linux/macOS only) and make sure it passes. If you use Windows, check that CI passes once you've opened the PR.

PR description

Fix Arc/Sector bounding boxes.

TODO

  • Off-by-one on right/bottom edges of Arc bounding box:

    arc-oob

  • Unit tests

Note that there are some errors around stroke widths as can be seen on the right side here:

oob-sector

Due to stroke corners lying outside the bounding box. IMO this should be fixed, but later. Perhaps once thick polylines is merged so the Line::extents() method can be reused.

Closes #405

@jamwaffles jamwaffles added this to In progress in 0.7.0-alpha.1 via automation Sep 19, 2020
@jamwaffles jamwaffles removed this from In progress in 0.7.0-alpha.1 Sep 19, 2020
@jamwaffles jamwaffles added this to In progress in 0.7.0-alpha.2 via automation Sep 25, 2020
@jamwaffles jamwaffles force-pushed the fix-arc-sector-bounding-box branch 4 times, most recently from a190165 to a034f04 Compare October 2, 2020 08:38
@jamwaffles
Copy link
Member Author

I'm getting my ass kicked by something that I'm sure has a simple solution: more off-by-one errors. I've tried various things but as a baseline I have this code in arc.rs (not pushed):

let radius = Real::from(self.diameter) / 2.into();

Point::new(
    i32::from(angle.cos() * radius),
    -i32::from(angle.sin() * radius),
) + Size::new_equal(radius.into())

The sin/cos stuff is fine, but as far as I can see the final point rarely lies on the actual arc (or circle, same thing) because the point is transformed not from the true center of the arc but somewhere near it (+/- 1) due to rounding. @rfuest you've done some magic with circles in the same vein as this, so could you offer some guidance perhaps?

@rfuest
Copy link
Member

rfuest commented Nov 11, 2020

I did take a very brief look at this and I think that at least part of the problem is in the existing code. I would expect an 90 degree arc starting at 0 degrees to exactly draw one quadrant of a full circle. But in my test this only worked correctly for arcs with odd diameters. Arcs with even diameters were missing one row of pixels at the bottom.

The circle code internally uses coordinates that are scaled by a factor of 2 (e.g. in Circle::center_2x). This way it is possible to use an effective resolution of half a pixel for the calculations, which is required to represent the accurate center of a circle with even diameter.

@rfuest rfuest mentioned this pull request Nov 14, 2020
4 tasks
@jamwaffles jamwaffles removed this from In progress in 0.7.0-alpha.2 Nov 15, 2020
@jamwaffles jamwaffles mentioned this pull request Jan 14, 2021
4 tasks
@cedeber cedeber mentioned this pull request Oct 14, 2022
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bounding boxes for Arc and Sector don't take the angles into account
2 participants