public static bool AlmostRectangle(Ngon target, double percent_diff = 0.05) { IntRect bounds = GetBounds(target); double area = Math.Abs(Clipper.Area(target)); return(1.0 - area / bounds.Area() < percent_diff); }
private void cmd_optimal_rotation(int handle) { Ngon hull = polygon_lib[handle].GetTransformedPoly()[0]; int n = hull.Count; double best_t = 0; int best = 0; long best_area = long.MaxValue; bool flip_best = false; for (int i = 0; i < n; i++) { double t = GeomUtility.AlignToEdgeRotation(hull, i); Mat3x3 rot = Mat3x3.RotateCounterClockwise(t); Ngon clone = hull.Clone(rot); IntRect bounds = GeomUtility.GetBounds(clone); long area = bounds.Area(); double aspect = bounds.Aspect(); if (area < best_area) { best_area = area; best = i; best_t = t; flip_best = aspect > 1.0; } } double flip = flip_best ? Math.PI * 0.5 : 0; IntPoint around = hull[best]; cmd_translate(handle, (double)-around.X, (double)-around.Y); cmd_rotate(handle, best_t + flip); cmd_translate(handle, (double)around.X, (double)around.Y); }