Rectangular Beam top to align on top of point

I need to align the rectangular sections to align to top centered on its start and end point (green color markups in attached snip)


Query-Beam top align on top of point.gh (12.4 KB)
, please refer the grasshopper code and snip in attachments.

If I understood correctly.

not sure about this approach, but it is fixed by adjusting fx formula of plane rotation.