Imagine your camera lens as having only one glass element. Now draw an imaginary triangle from the boundaries of the image sensor to the center of this lens. You have traced out an image triangle. If the camera sports an APS-C format, the base of this triangle is the same as the horizontal dimension of the imaging chip. In other words, 24mm. The height of this triangle is the focal length, let’s set it at 30mm.
The ratio of the height to base of this triangle is 30 ÷ 24 = 1.25.
You can now draw another triangle, call it the object triangle. Its apex is the center of lens. The apex of the image triangle and the apex of the image triangle are identical as to their angle (same degree of angle). In other words, these triangles are similar in shape, all angles being the same.
If we know the dimensions of an object we are about to image, we can compute the subject distance needed to just fit the boundaries of the sensor.
Suppose the object is a candy bar 200mm long. Set the length of the base of the object triangle to this value, 200mm. Now the height of this object triangle is easily computed as its ratio is also 1.25.
Thus, the camera to subject distance is 200 X 1.25 = 250mm. This is just an approximation (A good one).
Why is it not spot on? A modern camera lens is somewhat complex. It is constructed using several glass elements, each different as to their power. This makes determining the measuring points complicated. We measure object distance from a point called the front nodal. We don’t know where this point is actually located. As you work this problem and compose, you can refine your guess as to its location.
You will solve this problem using a ratio you compute after putting in your actual values as to sensor width and focal length.