//Check hashmap for lines that are equivalent. Note that we need to check one epsilon above and below the actual slope //since we're defining two lines as equivalent if they're within an epsilon of each other. int CountEquivalentLines(Dictionary <Double, List <Line> > linesBySlope, Line line) { double key = Line.FloorToNearestEpsilon(line.Slope); int count = CountEquivalentLines(linesBySlope[key], line); count += linesBySlope.ContainsKey(key - Line.Epsilon) ? CountEquivalentLines(linesBySlope[key - Line.Epsilon], line) : 0; count += linesBySlope.ContainsKey(key + Line.Epsilon) ? CountEquivalentLines(linesBySlope[key + Line.Epsilon], line) : 0; return(count); }
// insert line into hashmap void InsertLine(Dictionary <Double, List <Line> > linesBySlope, Line line) { List <Line> lines; double key = Line.FloorToNearestEpsilon(line.Slope); if (!linesBySlope.ContainsKey(key)) { lines = new List <Line>(); linesBySlope.Add(key, lines); } else { lines = linesBySlope[key]; } lines.Add(line); }