-
Notifications
You must be signed in to change notification settings - Fork 199
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
Adds Within trait and fixes more Contains edge cases #884
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::algorithm::Contains; | ||
|
||
/// Tests if a geometry is completely within another geometry. | ||
/// | ||
/// In other words, the [DE-9IM] intersection matrix for (Self, Rhs) is `[T*F**F***]` | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use geo::{point, line_string}; | ||
/// use geo::algorithm::Within; | ||
/// | ||
/// let line_string = line_string![(x: 0.0, y: 0.0), (x: 2.0, y: 4.0)]; | ||
/// | ||
/// assert!(point!(x: 1.0, y: 2.0).is_within(&line_string)); | ||
/// | ||
/// // Note that a geometry on only the *boundary* of another geometry is not considered to | ||
/// // be _within_ that geometry. See [`Relate`] for more information. | ||
/// assert!(! point!(x: 0.0, y: 0.0).is_within(&line_string)); | ||
/// ``` | ||
/// | ||
/// `Within` is equivalent to [`Contains`] with the arguments swapped. | ||
/// | ||
/// ``` | ||
/// use geo::{point, line_string}; | ||
/// use geo::algorithm::{Contains, Within}; | ||
/// | ||
/// let line_string = line_string![(x: 0.0, y: 0.0), (x: 2.0, y: 4.0)]; | ||
/// let point = point!(x: 1.0, y: 2.0); | ||
/// | ||
/// // These two comparisons are completely equivalent | ||
/// assert!(point.is_within(&line_string)); | ||
/// assert!(line_string.contains(&point)); | ||
/// ``` | ||
/// | ||
/// [DE-9IM]: https://en.wikipedia.org/wiki/DE-9IM | ||
pub trait Within<Other> { | ||
fn is_within(&self, b: &Other) -> bool; | ||
} | ||
|
||
impl<G1, G2> Within<G2> for G1 | ||
where | ||
G2: Contains<G1>, | ||
{ | ||
fn is_within(&self, b: &G2) -> bool { | ||
b.contains(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{point, Rect}; | ||
#[test] | ||
fn basic() { | ||
let a = point!(x: 1.0, y: 2.0); | ||
let b = Rect::new((0.0, 0.0), (3.0, 3.0)).to_polygon(); | ||
assert!(a.is_within(&b)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an example of the kind of test that was failing before this change: