public static void FitContoursToSize(double drawingWidth, double drawingHeight, FloatContour unknownContour, FloatContour dbContour, out Contour displayUnknownContour, out Contour displayDBContour, out double xOffset, out double yOffset) { if (unknownContour == null && dbContour == null) { throw new ArgumentNullException("Both contours null!"); } float xMax, yMax, xMin, yMin; if (unknownContour == null) { xMax = dbContour.MaxX(); yMax = dbContour.MaxY(); xMin = dbContour.MinX(); yMin = dbContour.MinY(); } else if (dbContour == null) { xMax = unknownContour.MaxX(); yMax = unknownContour.MaxY(); xMin = unknownContour.MinX(); yMin = unknownContour.MinY(); } else { xMax = (dbContour.MaxX() > (float)unknownContour.MaxX()) ? dbContour.MaxX() : (float)unknownContour.MaxX(); yMax = (dbContour.MaxY() > (float)unknownContour.MaxY()) ? dbContour.MaxY() : (float)unknownContour.MaxY(); xMin = (dbContour.MinX() < (float)unknownContour.MinX()) ? dbContour.MinX() : (float)unknownContour.MinX(); yMin = (dbContour.MinY() < (float)unknownContour.MinY()) ? dbContour.MinY() : (float)unknownContour.MinY(); } float xRange = xMax - xMin + 8, //***1.5 - added POINT_SIZE yRange = yMax - yMin + 8; //***1.5 - added POINT_SIZE float heightRatio = (float)(drawingWidth / yRange), widthRatio = (float)(drawingHeight / xRange); float ratio; if (heightRatio < widthRatio) { ratio = heightRatio; xOffset = (drawingWidth - ratio * xRange) / 2 - xMin * ratio; yOffset = 0 - yMin * ratio; } else { ratio = widthRatio; xOffset = 0 - xMin * ratio; yOffset = (drawingHeight - ratio * yRange) / 2 - yMin * ratio; } float ratioInverse = 1 / ratio; if (unknownContour == null) { displayUnknownContour = null; } else { displayUnknownContour = new Contour(unknownContour, ratioInverse); } if (dbContour == null) { displayDBContour = null; } else { displayDBContour = new Contour(dbContour, ratioInverse); } }