Perpendiculars

I often need to construct perpendiculars in my code and found a very elegant solution to this problem in CGAL source.

Given points A(xa, ya) and B(xb, yb), perpendiculars BBccw and BBcw to the line AB can be constructed at B using either of the following points:

Bccw(xb – (yb – ya), yb + (xb – xa)) // such that ABBccw is oriented counter-clockwise

or

Bcw(xb + (yb – ya), yb – (xb – xa)) // such that ABBcw is clockwise.

This follows from the fact that the product of the slopes of perpendicular lines is -1.

Here is code to return either of these points.

Output: