Triangle and Trigonometry.
The perspective is a projection of the real 3D over a glass in front of your eye. What you see on the 2D glass is exactly what you see on your drawing above in 2D.
The actual calculation uses angular size of the closer horizontal line, but for that, you need to know the distance between your eye to this line. Lets assume this Distance (D) is 5cm. A = CloserHorizontalLine. B = FarHorizontalLine. A Angular size = AAS = 2*arctan(size/2*distance). AAS on glass =2*arctan(2/10)=22.6°. BAS (on glass) = 2*arctan(1/10)=11.4°. Now, from the left side of the A to the left side of the B on glass, is 0.5cm, Half of difference A to B (HDAB). Now we have a rectangle triangle made by this HDAB a 90° line to the back of real rectangle (C) and the hypotenuse we don't care about, but it makes a 11.4°/2 with the C. Now it is easy to calculate C = HDAB/tan(BAS/2) = 0.5/tan(11.4/2) = 5. To make a formula of it, it will be C = ((A-B)/2)/arctan(tan(B/2D)), eliminating arctan and tan, it results in C=(A-B)*2 / B/2D = C=(A-B)*2D/2B = C=(A-B)*D/B. If A=2 B=1 D=5, C=5, If A=3 B= D=5, C=10. I may be wrong.