In the "Linear Interpolation" section This article discusses how to interpolate the values when the lines are oblique.
For instance, for Case#2 it has the following calculation:
I am confused about the cases when lines are either horizontal or vertical.
can anyone shed some light?
How can I do interpolation when lines are either horizontal or vertical?
A simple and safe way is to process the four sides in turn: if both endpoints have a value above or below the desired isolevel, there is an intersection. After considering the four sides, you get zero, two or four intersections, hence zero, one or two segments (in the case of two segments join the intersections that form two non-crossing segments).
Final remark: for best reliability, adopt the test above-or-on or below (strictly below).