Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Spatial.Tests/Euclidean/Line2DTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using MathNet.Spatial.Euclidean;
using MathNet.Spatial.Units;
using NUnit.Framework;
Expand Down Expand Up @@ -56,6 +56,38 @@ public void LineDirection(string p1s, string p2s, string exs)
AssertGeometry.AreEqual(ex, line.Direction);
}

public static object[] DistanceTestCases =
{
new object[] { "-1,+1", "+1,+1", "0,0", 1.0 },
new object[] { "-1,+1", "+1,+1", "0,3", 2.0 },
new object[] { "-1,+1", "+1,+1", "0,1", 0.0 },
new object[] { "-1,0", "0,+1", "0,0", 0.70710678 },
new object[] { "-1,0", "0,+1", "-0.5,0.5", 0.0 },
new object[] { "+1,-1", "+1,+1", "0,0", 1.0 },
new object[] { "+1,-1", "+1,+1", "1,0", 0.0 },
new object[] { "+1,-1", "+1,+1", "3,0", 2.0 },
};

[TestCaseSource(nameof(DistanceTestCases))]
Copy link
Member

@jkalias jkalias Dec 27, 2023

Choose a reason for hiding this comment

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

I think the previous structure with TestCase was much better. My suggestion with the swapped scenarios was meant to be handled in the TestCase definition, not as a separate test function.

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 the comment.
Could you tell me why using TestCode is better than using TestCodeSource for this test?

Copy link
Member

Choose a reason for hiding this comment

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

I find the whole new object[] a bit overkill, that's it. I guess it's more a matter of personal taste.

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 the comment.

it's more a matter of personal taste.

Could you show the reason?

Copy link
Member

Choose a reason for hiding this comment

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

The reason for what exactly? I prefer compile-time checks and try to avoid object as much as possible

Copy link
Contributor Author

@f-frhs f-frhs Jan 7, 2024

Choose a reason for hiding this comment

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

Thanks for telling the reasons.

Done in the commit cd80cf1.
I changed the test-code as you required. I'm not sure that the revised test-code helps the reader understand at a glance that parameters are kept intact except its order.

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. This is not what I meant, we can augment the existing tests with a reversed line

var line = new Line2D(Point2D.Parse(p1s), Point2D.Parse(p2s));
// existing test

var reversedLine = new Line2D(Point2D.Parse(p2s), Point2D.Parse(p1s));
// distance test with reversed line

This way you keep the TestCase cases at a minimum

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 showing the example closely.

It is not recommended to test 2 things in a single unit-test.
Or do you want to say that these 2 tests are equivalent because of the symmetry?

Copy link
Member

Choose a reason for hiding this comment

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

It is not recommended to test 2 things in a single unit-test.

Fully agree

Or do you want to say that these 2 tests are equivalent because of the symmetry?

Basically yes

public void DistanceFromLineToPoint(string sp1, string sp2, string ps, double expectedDistance)
{
var line = new Line2D(Point2D.Parse(sp1), Point2D.Parse(sp2));
var p = Point2D.Parse(ps);

var actual = line.DistanceTo(p);
Assert.That(actual, Is.EqualTo(expectedDistance).Within(1e-6));
}

[TestCaseSource(nameof(DistanceTestCases))]
public void DistanceFromLineToPoint_SwappedP1sAndP2s(string sp1, string sp2, string ps, double expectedDistance)
{
var line = new Line2D(Point2D.Parse(sp2), Point2D.Parse(sp1)); // swapped
var p = Point2D.Parse(ps);

var actual = line.DistanceTo(p);
Assert.That(actual, Is.EqualTo(expectedDistance).Within(1e-6));
}

[TestCase("0,0", "10,10", "0,0", "10,10", true)]
[TestCase("0,0", "10,10", "0,0", "10,11", false)]
public void EqualityOperator(string p1s, string p2s, string p3s, string p4s, bool expected)
Expand Down
13 changes: 13 additions & 0 deletions src/Spatial/Euclidean/Line2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public static Line2D Parse(string startPointString, string endPointString)
return new Line2D(Point2D.Parse(startPointString), Point2D.Parse(endPointString));
}

/// <summary>
/// Returns the straight line Distance to the given point.
/// </summary>
/// <param name="p">the given point</param>
/// <returns>a distance measure</returns>
[Pure]
public double DistanceTo(Point2D p)
{
var closestPoint = ClosestPointTo(p, false); // this is Line2D, not LineSegment2D.
var result = closestPoint.DistanceTo(p);
return result;
}

/// <summary>
/// Returns the shortest line between this line and a point.
/// </summary>
Expand Down