You can modify the script to get the proper length:
basically on one end: X= W/(tan(alpha))
where X is the lenght addition
W is the height of the beam
alpha would be the resulting angle - but it is not the angle of the beam - but also relays on the cut-off angles of the end(s)
the problem with tan is that You should filter out 90 and 270 angles.
You could also develop formula with SIN and COS to get the result
If I am not mistaken X= cos(alpha)*W/(sin(alpha))
Best Regards,
Piotr