As you can see in the below picture, We have a right triangle inside a big square, and within the triangle there is another small right triangle.
The question as follows: Find the length of AB "With" using BE and FGin your solution.
Well I came up with this question because I need to find AB without using the other data that we can simply take them from the image, because I am working in a project That I must use raycasting method anyway after analyzing and a lot of drawing I managed to answer the question as follows :
We have BE = 4cm and FG = 0.5cm then AB = BE / FG => AB = 4 / 0.5 => AB = 8cm
and I found this method works only if the AE ray stays within the square area, For example if point E = (10,8) this method will not work anymore as shown below :
there is some area of the triangle is outside the square and also if you see the picture you will find that FG now is big than 1 cm FG > 1cm, so this method will work for you just if FG > 0 && FG <= 1
I believe that there is another way to explain this so my question is does anyone knows the logic behind AB = BE / FG or someone who knows a better explanation or some formulas that works with this kind of triangles.
Edit
after Daniel Mathias answered my question, I realized even the second example the "method" works with it, i just got confused with another triangle my bad.


Triangles $ABE$ and $AFG$ are similar, with $\frac{AB}{BE}=\frac{AF}{FG}$. Given $AF=1$, this means:
$$\frac{AB}{BE}=\frac{1}{FG}\implies AB=\frac{BE}{FG}$$
This is true without exception. In your second image, you have $BE=10$, $FG=1.25$ and $AB=\frac{10}{1.25}=8$.