public bool RecognizeLoop (int startIndex, out int lastIndex)
{
lastIndex = startIndex;
if (startIndex >= _points.Count) return false;
var bb = BoundingBox;
var maxDim = Math.Max (bb.Width, bb.Height);
var segs = GetDirectionSegments (startIndex);
if (segs.Length >= 4) {
var fdir = segs [0].Direction;
var sdir = segs [1].Direction;
var ddir = sdir.Minus (fdir);
if (Math.Abs (ddir) == 1) {
//
// Complete the loop
//
var lastSeg = 2;
for (var i = 2; i < 4; i++) {
if (segs [i].Direction.Minus (sdir) == ddir) {
sdir = segs [i].Direction;
}
else {
//
// Is it "close enough"
//
var d = segs [i].StartPoint.DistanceTo (segs [0].StartPoint);
if (d < maxDim * 0.25f) {
break;
}
else {
return false;
}
}
lastSeg = i;
}
//
// Allow the loop to fold in on itself
//
var ei = segs [lastSeg].EndIndex;
if (lastSeg + 1 < segs.Length && segs [lastSeg + 1].Direction == fdir) {
ei = segs [lastSeg + 1].EndIndex;
}
//
// Trim the tail of the loop to the beginning of the loop
//
lastIndex = GetSegment (ei / 2, ei).GetClosestPoint (Points [0]);
return true;
}
}
return false;
}