findClosetLine method

Line findClosetLine(
  1. Line line,
  2. List<Line> allLines
)

Finds a Line which is the closets to the line from the right side. for example we can use it when we have the first name and we want to extract the last name on the right of it.

Implementation

Line findClosetLine(Line line, List<Line> allLines) {
  allLines.removeWhere(
      (element) => element.cornerList![3].x <= line.cornerList![0].x);
  allLines.removeWhere(
      (element) => element.cornerList![0].x > line.cornerList![3].x);
  allLines.removeWhere(
      (element) => element.cornerList![0].y > line.cornerList![2].y);
  if (allLines.isEmpty) return Line(text: "");
  Line result = allLines.reduce((a, b) {
    var ax = a.cornerList![0].x - line.cornerList![1].x;
    var ay = a.cornerList![0].y - line.cornerList![1].y;
    var bx = b.cornerList![0].x - line.cornerList![1].x;
    var by = b.cornerList![0].y - line.cornerList![1].y;
    return (ax * ax + ay * ay * 4 < bx * bx + by * by * 4) ? a : b;
  });
  return result;
}