# Thread: Zoom to fit screen

1. ## Re: Zoom to fit screen

Is it possible to see the code that finally worked? I have the same problem to solve.

Thanks much!

2. ## Re: Zoom to fit screen

'sin' does not just give better results, it is the right trigonometric function to use.

For the code you need to know:

* width:height aspect ratio (width / height as floating point number as used by gluPerspective)
* center and radius of the bounding sphere for the scene
* direction of the camera (unit length)
* fov in y (as used by gluPerspective)

The code would be as follows:

Code :
```half_min_fov_in_radians = 0.5 * (fov * PI / 180);

if (aspect < 1.0)
{
// fov in x is smaller
half_min_fov_in_radians = atan(aspect * tan(half_min_fov_in_radians));
}

distance_to_center = radius / sin(half_min_fov_in_radians);
eye = center - dir * distance_to_center;```

You may also take advantage of that information to fit your zfar clipping plane (if you do not want to keep it too far).

Code :
```zfar = distance_to_center + radius;

if (zfar < 1.5 * znear)
{
// Keep zfar always bigger than znear
zfar = 1.5 * znear;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•