private static GroupedText GetClosest(IBoundingBox txt, List <GroupedText> groups) { double mindist = double.MaxValue; GroupedText closest = null; foreach (GroupedText set in groups) { double xdist = 0; double ydist = 0; double dist = 0; if (!BoundingBox.IsAlignedVertically(txt, set.Boundary)) { if (txt.Left < set.Boundary.Left) { xdist = set.Boundary.Left - (txt.Left + txt.Width); } else { xdist = txt.Left - (set.Boundary.Left + set.Boundary.Width); } } if (!BoundingBox.IsAlignedHorizontally(txt, set.Boundary)) { if (txt.Top < set.Boundary.Top) { ydist = set.Boundary.Top - (txt.Top + txt.Height); } else { ydist = txt.Top - (set.Boundary.Top + set.Boundary.Height); } } dist = Math.Sqrt(xdist * xdist + ydist * ydist); if (dist < mindist) { mindist = dist; closest = set; } } return(closest); }
//private static List<GroupedText> GetGroups(IEnumerable<Tree> text) //{ // List<GroupedText> groups = new List<GroupedText>(); // if (text.Count() > 0) // { // groups.Add(new GroupedText(text.First())); // text = text.Skip(1); // foreach (Tree txt in text) // { // GroupedText nearest = GetClosest(txt, groups); // int right = nearest.Boundary.Left + nearest.Boundary.Width; // int bottom = nearest.Boundary.Top + nearest.Boundary.Height; // if ((txt.Left < right + MAX_SPACE_DIST) && (txt.Top < bottom + MAX_ROW_DIST)) // nearest.Add(txt); // else // { // GroupedText next = new GroupedText(txt); // groups.Add(next); // } // } // } // foreach (GroupedText group in groups) // group.Group.Sort(BoundingBox.CompareByTopThenLeft); // groups.Sort( CompareGroupBoundingBoxTopTheLeft ); // return groups; //} private static int CompareGroupBoundingBoxTopTheLeft(GroupedText g1, GroupedText g2) { return BoundingBox.CompareByTopThenLeft(g1.Boundary, g2.Boundary); }
//private static List<GroupedText> GetGroups(IEnumerable<Tree> text) //{ // List<GroupedText> groups = new List<GroupedText>(); // if (text.Count() > 0) // { // groups.Add(new GroupedText(text.First())); // text = text.Skip(1); // foreach (Tree txt in text) // { // GroupedText nearest = GetClosest(txt, groups); // int right = nearest.Boundary.Left + nearest.Boundary.Width; // int bottom = nearest.Boundary.Top + nearest.Boundary.Height; // if ((txt.Left < right + MAX_SPACE_DIST) && (txt.Top < bottom + MAX_ROW_DIST)) // nearest.Add(txt); // else // { // GroupedText next = new GroupedText(txt); // groups.Add(next); // } // } // } // foreach (GroupedText group in groups) // group.Group.Sort(BoundingBox.CompareByTopThenLeft); // groups.Sort( CompareGroupBoundingBoxTopTheLeft ); // return groups; //} private static int CompareGroupBoundingBoxTopTheLeft(GroupedText g1, GroupedText g2) { return(BoundingBox.CompareByTopThenLeft(g1.Boundary, g2.Boundary)); }