I have an algorithm for checking whether one line is contained within another. The lines I have are made up of an ordered array of points, which are just data structures with values for x, y and z. The lines are no necessarily straight. The subline does not need to be going in the same direction as the main line.
The algorithm I have currently is:
public static bool IsSubline(Point[] subline, Point[] line)
{
int prevIndex = int.MaxValue;
bool contains = false;
foreach (Point point in subline)
{
for (int i = 0; i < line.Length; i++)
{
if (Equal(point, line[i]))
{
if (prevIndex == int.MaxValue || System.Math.Abs(prevIndex - i) == 1)
{
prevIndex = i;
contains = true;
break;
}
}
contains = false;
}
if (!contains)
{
break;
}
}
return contains;
}
Equal
is:
internal static bool Equal(Point point1, Point point2, float epsilon = CoordinateEpsilon)
{
return System.Math.Abs(point1.x - point2.x) < epsilon &&
System.Math.Abs(point1.y - point2.y) < epsilon &&
System.Math.Abs(point1.z - point2.z) < epsilon;
}
I feel like this could be done in a faster way, anyone have any suggestions?