private static bool SideIsNeighbor(this Side side, Level level, Side possibleNeighbor, bool left, out bool neighborFlowsOutward) { var line = level.Lines[side.LineIndex]; var endpointIndex = side.EndpointIndex(level, line, left); var endpointLines = level.EndpointLines[endpointIndex]; foreach (var neighborLine in endpointLines) { if (neighborLine == line) { continue; } neighborFlowsOutward = neighborLine.EndpointIndexes[0] == endpointIndex; var neighborIsClockwise = neighborFlowsOutward != left; var neighborSide = neighborLine.Side(level, neighborIsClockwise); if (neighborSide == null) { continue; } if (neighborSide == possibleNeighbor) { return(true); } } neighborFlowsOutward = false; return(false); }