# Sample Code: Set exact world fit with no gaps

**URL:** <https://forum.techsoft3d.com/t/sample-code-set-exact-world-fit-with-no-gaps/1409>\
**Category:** HOOPS Visualize Desktop\
**Tags:** code, sample, learn, knowledge-base, viewing\
**Created:** [May 17, 2022, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-set-exact-world-fit-with-no-gaps/1409 "2022-05-17T04:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [May 17, 2022, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-set-exact-world-fit-with-no-gaps/1409/1 "2022-05-17T04:00:00Z")

</div>

**Version:** HPS 2020  
**Language:** C#  
**Description:**

This sample code behaves similarly to `View::FitWorld()`, but it does not include gaps.

```auto
void SetExactWorldFit(Canvas _canvas)
{
	HPS::SegmentKey viewKey = _canvas.GetFrontView().GetSegmentKey();
	HPS::Rectangle screenExtent;
	
	// SprocketPath is a key path from the model segment up to the canvas window key, including all segments and include keys along the way
	HPS::SprocketPath sprkPath = SprocketPath(_canvas);
	
	// Compute a 2D extent box around the model in Screen Space 
	sprkPath.GetKeyPath().ComputeExtent(screenExtent);
	
	// Compute the center point of box in Screen Space
	HPS::Point lefttop = HPS::Point(screenExtent.left, screenExtent.top, 0);
	HPS::Point bottomright = HPS::Point(screenExtent.right, screenExtent.bottom, 0);
	HPS::Point center = HPS::Point((lefttop.x + bottomright.x) / 2.0f,
		(lefttop.y + bottomright.y) / 2.0f,
		0);
	
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, center, HPS::Coordinate::Space::Camera, center);
	center.z = 0;
	
	// Get information on current camera
	CameraKit out_kit;
	sprkPath.GetKeyPath().ShowNetCamera(out_kit);
	HPS::Point pos;
	out_kit.ShowPosition(pos);
	
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::World, pos, HPS::Coordinate::Space::Camera, pos);
	HPS::Point delta = Point(pos.x - center.x, pos.y - center.y, pos.z - center.z);
	
	// move camera
	viewKey.GetCameraControl().Dolly(-delta.x, -delta.y, 0);
	
	// Calculate and set camera field for an exact fit
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, lefttop, HPS::Coordinate::Space::Camera, lefttop);
	sprkPath.GetKeyPath().ConvertCoordinate(HPS::Coordinate::Space::Window, bottomright, HPS::Coordinate::Space::Camera, bottomright);
	
	HPS::Point vector = Point(lefttop.x - bottomright.x, lefttop.y - bottomright.y, lefttop.z - bottomright.z);
	vector.x = abs(vector.x);
	vector.y = abs(vector.y);
	
	viewKey.GetCameraControl().SetField(vector.x, vector.y);
	
	_canvas.UpdateWithNotifier().Wait();
}

```
