# Sample Code: Set Optimized Camera Position and Camera Target Ratio in 3DF Part Viewer

**URL:** <https://forum.techsoft3d.com/t/sample-code-set-optimized-camera-position-and-camera-target-ratio-in-3df-part-viewer/1418>\
**Category:** HOOPS Visualize Desktop\
**Tags:** code, sample, knowledge-base, animation\
**Created:** [October 11, 2021, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-set-optimized-camera-position-and-camera-target-ratio-in-3df-part-viewer/1418 "2021-10-11T04: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:** [October 11, 2021, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-set-optimized-camera-position-and-camera-target-ratio-in-3df-part-viewer/1418/1 "2021-10-11T04:00:00Z")

</div>

**Language: C++**

Ideally, the camera should be set up such that the “distance from the camera position to the camera target should be 2.5 times the field width” – per our docs under “[Camera Set Up Guidelines](https://docs.techsoft3d.com/3df/latest/prog_guide/3dgs/03_2_viewing_modelling_cameras.html?#camera-setup-guidelines)”:

To that end, below is a brief code sample which can be executed as an extra slot code in the HOOPS 3DF Part Viewer:

1. Open “samples\_v14x.sln” found in the root directory of the 3DF service package.
2. In the VS project “hoopspartviewer\_v14x”, navigate to the C++ file called “CSolidHoopsView.cpp”. The user slots are at the bottom of the file. Add the following code:

```auto
void CSolidHoopsView::OnExtraSlot1()
{
	HCamera old_camera;

	HC_Open_Segment_By_Key(m_pHView->GetSceneKey()); {
		HC_Show_Net_Camera(&old_camera.position, &old_camera.target, &old_camera.up_vector, &old_camera.field_width,
			&old_camera.field_height, old_camera.projection);

		//Calculate view vector and normalize it
		HVector v(old_camera.target.x - old_camera.position.x, old_camera.target.y - old_camera.position.y, old_camera.target.z - old_camera.position.z);
		HVector vn;
		HC_Compute_Normalized_Vector(&v, &vn);

		//Calculate the optimized distance (based on recommended on 2.5x)
		float new_distance = 2.5 * old_camera.field_width;

		//Set the new camera position taking into account the 2.5x ratio
		HVector new_position(old_camera.target.x + new_distance * -vn.x, old_camera.target.y + new_distance * -vn.y, old_camera.target.z + new_distance * -vn.z);

		//Set the new optimized camera position
		HC_Set_Camera_Position(new_position.x, new_position.y, new_position.z);

	} HC_Close_Segment();

	m_pHView->Update();
}

```
