Clipping Lines to Rectangular Viewports in MATLAB
How to compute intersections of a line with a rectangle in MATLAB.
This is so easy, someone else should have done it already. There is some code here: http://www.mathworks.co.uk/matlabcentral/fileexchange/25528-line-clipping. However, among other things (like using input
), it doesn't check for lines parallel with the clipping window borders.
Wikipedia gives a variety of algorithms -- Liang/Barsky's algorithm is probably the one which one implement most quickly.
Here goes the code:
Now, running this:
Makes this: