Fitting as much points as possible on a line
I think as suggested in the comments, you should split this up in several linear regressions. 

If the datapoints are ordered you could just do repeated linear regressions, always adding one point until the added point leads to a significant reduced goodeness of fit. Then you start the next linear regression until that one goes bad and you start the next one. 
At the end you can compare the angles between the two lines and see if they are (roughly) 90 degrees.