# Matrix math: How to specify camera distance so that an object with a given size is completely visible?

This is my code that draws a top down view tile based map. But Iam do not know how high the camera needs to be placed in the sky so that the map is fully visible so that nothing is cut away and I do not see any borders at the screen border.

I tried to use the formula "tan(angle) = a/b" where "angle" I guess is my FOV (45 degrees) and "a" my screenwidth and "b" the wanted height but it did not return the correct result. Is this the correct formula or can the problem lie somewhere else?

float aspectRatio = graphicsDevice.Viewport.AspectRatio;

//var height = camera.WindowSize.X / MathF.Tan(FieldOfView); // this does not give the right result
float height = 2410; // found out experimentally, todo how to calculate this?
var camPos = new Vector3(camera.ScaledWindowSize.X * 0.5f, camera.ScaledWindowSize.Y * 0.5f, height);
var camTarget = camPos;
camTarget.Z = 0;

float nearClip = 0.1f;
float farClip = 10000.0f;

var mProjection = Matrix.CreatePerspectiveFieldOfView(
fieldOfView,
aspectRatio,
nearClip,
farClip);

var mView = Matrix.CreateLookAt(camPos, camTarget, Vector3.Up)
* camera.GetViewMatrix()
* Matrix.CreateScale(1, -1, 1);