But your program only works with triangles which have a rectangle between A and B and B must be right of A and C must be below A.
It doesn't work for the points in OP's post

For a more general...