It also sometimes helps to plot these things out on paper and manually work them out (with a calculator.. lol..) and then apply that to the code.
I do not have any code on hand, but this will be somewhat complicated by the fact that you're working in a 3D space. If it were 2D then you could ignore the Z1 and Z2 co-ordinates....
:: Specs: AMD Athlon XP 1600+ (1.4GHz), 512MB PC133 SDRAM, Leadtek Geforce 2MX SH 64MB, SB-Live!, LG SWrks 775N 17", MS Natural Pro KB, MS Optical Mouse, Windows XP Pro (SP1), DirectX 9.0b ::