public static void Fit(this WorldView world, IObject3D itemToRender, RectangleDouble goalBounds, Matrix4X4 offset) { AxisAlignedBoundingBox meshBounds = itemToRender.GetAxisAlignedBoundingBox(offset); bool done = false; double scaleFraction = .1; // make the target size a portion of the total size goalBounds.Inflate(-goalBounds.Width * .1); int rescaleAttempts = 0; while (!done && rescaleAttempts++ < 500) { RectangleDouble partScreenBounds = world.GetScreenBounds(meshBounds); if (!NeedsToBeSmaller(partScreenBounds, goalBounds)) { world.Scale *= 1 + scaleFraction; partScreenBounds = world.GetScreenBounds(meshBounds); // If it crossed over the goal reduct the amount we are adjusting by. if (NeedsToBeSmaller(partScreenBounds, goalBounds)) { scaleFraction /= 2; } } else { world.Scale *= 1 - scaleFraction; partScreenBounds = world.GetScreenBounds(meshBounds); // If it crossed over the goal reduct the amount we are adjusting by. if (!NeedsToBeSmaller(partScreenBounds, goalBounds)) { scaleFraction /= 2; if (scaleFraction < .001) { done = true; } } } } }