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

Add points iterators for primitives #307

Merged
merged 1 commit into from
Apr 6, 2020

Conversation

rfuest
Copy link
Member

@rfuest rfuest commented Apr 5, 2020

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 ./build.sh (Linux/macOS only) and make sure it passes. If you use Windows, check that CI passes once you've opened the PR.

PR description

This PR adds Primitive::points which returns an iterator over all points inside a closed shape or on a line.

To keep the changes in this PR small the new iterators are mostly copies of the existing ones without the styling features. Future PRs can reduce the code duplication, but some of these changes depend on other features that aren't implemented yet.

@jamwaffles
Copy link
Member

This PR looks good to me, but can you talk about about how it will support #182 (comment)? I'd just like to get a picture of where this is headed.

@rfuest
Copy link
Member Author

rfuest commented Apr 5, 2020

The points iterators aren't directly influenced by a PrimitiveStyle because they work on "raw" Primitives and not styled primitives. The same behavior will also replicated by the hit testing function, which will essentially return true if a point is contained in primitive::points().

To make it easier to use the same functions for styled primitives additional functions can be added. For example, Rectangle::stroke_area(&self) -> Rectangle and Rectangle::fill_area(&self) -> Rectangle, would return offset versions of the original rectangle that define the outer edge of the styled primitives fill and stroke. Having this functions would make it easy to implement custom drawing routines:

let c = Circle::new(...).into_styled(...);
let stroke_area = c.stroke_area();
let fill_area = c.fill_area();
stroke_area.points()
   .map(|p| if fill_area.contains(p) { FILL_COLOR }  else { STROKE_COLOR })
   .draw(&mut display);

@jamwaffles
Copy link
Member

For example, Rectangle::stroke_area(&self) -> Rectangle and Rectangle::fill_area(&self) -> Rectangle, would return offset versions of the original rectangle that define the outer edge of the styled primitives fill and stroke.

That's really elegant, I like it :)

Copy link
Member

@jamwaffles jamwaffles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR looks good to me, although I'm on Windows atm so haven't tested it.

@rfuest rfuest merged commit d3d3411 into embedded-graphics:master Apr 6, 2020
@rfuest rfuest mentioned this pull request Apr 7, 2020
15 tasks
jamwaffles pushed a commit that referenced this pull request Apr 15, 2020
@rfuest rfuest deleted the points branch July 20, 2020 14:41
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.

2 participants