Improve contour FindPointInPolygon() reliability (2).
This commit is contained in:
@@ -6,10 +6,42 @@
|
||||
|
||||
namespace TriangleNet.Tools
|
||||
{
|
||||
using System;
|
||||
using TriangleNet.Geometry;
|
||||
|
||||
public static class IntersectionHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Check if a given test point lies on a segment.
|
||||
/// </summary>
|
||||
/// <param name="a">The segment start point.</param>
|
||||
/// <param name="b">The segment end point.</param>
|
||||
/// <param name="test">The point to test.</param>
|
||||
/// <param name="eps">Threshold to test collinearity (default = 1e-12).</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsPointOnSegment(Point a, Point b, Point test, double eps = 1e-12)
|
||||
{
|
||||
// The cross product.
|
||||
double cross = (test.Y - a.Y) * (b.X - a.X) - (test.X - a.X) * (b.Y - a.Y);
|
||||
|
||||
// Check if points are collinear.
|
||||
if (Math.Abs(cross) > eps) return false;
|
||||
|
||||
// The dot product (projection of test point onto segment).
|
||||
double dot = (test.X - a.X) * (b.X - a.X) + (test.Y - a.Y) * (b.Y - a.Y);
|
||||
|
||||
// Check if test point is actually between a and b (left of a).
|
||||
if (dot < 0) return false;
|
||||
|
||||
// Length of the segment.
|
||||
double ab = (b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y);
|
||||
|
||||
// Check if test point is actually between a and b (right of b).
|
||||
if (dot > ab) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute intersection of two segments.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user