Calculate the position and rotation needed to have a camera look at a certain region I have a 2D region defined by 4 points in 3D space. I want to position my camera so that it looks at the region, with a settable variable determining which side it looks at, where looks at means that the camera view covers the entire region to be looked at, and as little beyond that as possible. How do I calculate the orientation and position of the camera?
I have a 2D region defined by 4 points in 3D space. I want to position my camera so that it looks at the region, with a settable variable determining which side it looks at,...


The first thing you need to do for this is to define a convention that specifies which side is the front and which one is the back of your plane depending on the ordering of your nodes. With triangles, you usually use the winding order. So you can define that if you look at the front of a triangle, the points have to be in clockwise order. In conclusion, if you see that the nodes of the triangle are ordered counter-clockwise, you look at the back.


  How do I calculate the orientation and position of the camera?


Once you have this convention, you have to calculate the normal of the surface. You can get it by calculating the cross product of 2 vectors. For example, if your points are named A, B, C, D, take the vectors A->B and A->C and calculate their cross product to get the surface normal. Be aware, that the cross product is sensitive to the vector/node order. So make sure the selected points result in a normal that points away from your front side. 

Now, your camera orientation is the negative normal direction if you look at the front and the normal's direction if you look at the back.

Then you calculate the center point of your quadriliteral. There are several ways to do this, so just pick your favorite method or search the internet for it. Your camera position C has to be on the line that has the same direction as your surfaces' unit normal N and goes through the center point P of your surface. So it can be calculated by

$$
C = P + d \cdot N
$$

where d is the distance of the camera to point P. The distance depends on your camera's field of view and the size of your plane. In your special case, where the camera looks directly onto your surface, the camera/screen plane is parallel to your surface. So you can use the intercept theorem to calculate the minimal necessary camera distance so that all points of your plane are inside the cameras FOV.

Remark:

The described method leaves you with one degree of freedom for your camera that is chosen more or less randomly. The rotation angle around its view direction. Depending on the size and shape of your plane, the rotation angle also influences the camera's distance to the plane. So you might want to define an additional constraint to always get the same result. For example, you could say, that your surfaces side A-B has to be parallel to your screens left-hand side.