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 Line2D.TryIntersect to pass nullable Point2D #187

Merged
merged 4 commits into from
Mar 27, 2023
Merged

Fix Line2D.TryIntersect to pass nullable Point2D #187

merged 4 commits into from
Mar 27, 2023

Conversation

f-frhs
Copy link
Contributor

@f-frhs f-frhs commented Apr 29, 2021

This pull-request is asking for help to solve #178.

The problem is that LineSegment2D.TryIntersect() always passes by reference Point2D of intersection, regardless of whether the two LineSegment2D actually have a valid intersection or not.

In my fix, LineSegment2D.TryIntersect() passes by ref Point2D?, which can indicate that there is no intersection.
My fix used #111 as reference.

I'm not sure that this breaking change is acceptable or not.
If there is another way to solve this, I'm happy to learn it.

I considered these fix-plans:

  1. Leave it as it is
  2. Pass by ref default(Point2D)
  3. Pass by ref Nullable<Point2D>
  4. Pass by ref Point2D.Null (kinda)
  5. Return Nullable<Point2D> instead of bool

/// <returns>True if an intersection exists; otherwise false</returns>
[Pure]
public bool TryIntersect(LineSegment2D other, out Point2D intersection, Angle tolerance)
public bool TryIntersect(LineSegment2D other, out Point2D? intersection, Angle tolerance)
{
Copy link
Member

Choose a reason for hiding this comment

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

Initialization intersection = null;

{
if (this.IsParallelTo(other, tolerance))
{
intersection = default(Point2D);
intersection = null;
Copy link
Member

Choose a reason for hiding this comment

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

Not needed here if the initialization is done at the function entry point

intersection = p + (t * r);
return true;
}
else
Copy link
Member

Choose a reason for hiding this comment

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

You can simplify all this by simply return intersection != null; and completely removing the else case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for a comment.

How about the following case?
83840266-f4d1c880-a6b2-11ea-8137-56f9fee366af

This comes from #178.

Copy link
Member

@jkalias jkalias Mar 28, 2023

Choose a reason for hiding this comment

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

If I am not mistaken, this is exactly the bug fix in this PR, correct? There is no intersection in your example, you can verify it using LineSegment2DTests.IntersectWithTest2

I helped a bit in the implementation of this PR if you don't mind, this has already been merged to master.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for a comment.
Your understanding is correct that we can verify it by using LineSegment2DTests.IntersectWithTest2.

I'm happy with the merged code. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

Thank YOU for opening the issue and providing a PR 😃

@jkalias jkalias marked this pull request as ready for review March 24, 2023 16:34
@jkalias jkalias merged commit 698a833 into mathnet:master Mar 27, 2023
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