First, I’ll give a quick to the point answer, then if you want more background, I give a more detailed answer below. Note, I am not a mathematician. I just wanted to share what worked for me. So now onto…

**The quick answer:**

Given Point1 (x1, y1, z1) and Point2 (x2, y2, z2) what is the line that pass through them.

y as a function of x

Equation 1:

y = [(y2-y1)/(x2-x1)](x-x1) + y1

z as a function of x

Equation 2:

z = [(z2-z1)/(x2-x1)](x-x1) + z1

If **x1 = x2** you will end up with a **zero** in the **denominator** of Equation 1 and Equation 2. You will have to use Equation 3 to find z, so instead of specify x you will have to specify y.

z as a function of y

Equation 3:

z = [(z2-z1)/(y2-y1)](y-y1) + z1

Also if **x1=x2** and **y1=y2**, you have a line in the z direction (parallel to the z axis) where the x1 plane intersections the y1 plane (or at the x1,y1 point). So the equation of your line is simple any z point with x=x1 and y=y1. Note, since x1=x2 and y1=y2, they are interchangeable.

**The detailed answer:**

Give two points in 3 dimension space what is the generalized equation of the line that passes through them. Well, I search the web for the answer, but I could not find what I was looking for. There were vector forms and parametric forms. But what I was looking for was a formula that I could put in an x value and get the corresponding y and z values. I did find an example of a Cartesian form, but it was derived using specific points. I needed a generalized equation for two points which were unknown. My two points were going to be moving around, and the connecting line would need to move with them. So finally I derived an equation using the slope-intercept form (see Equation 4 below).

Equation 4:

y = mx+b

m = the slope of the line or rise over run

b = the intercept, point where the line crosses the y axis

My initial reasoning was since it was a line, for every x value there would be only one y or z value. Another way to look at it…if you simply look at the xy plane, you will have a projection of a simple line onto the xy plane from that xyz line. Once I had the y value, I could switch to looking at just the xz plane or the yz plane again dealing with a simple line.

So dealing with just the xy plane, the slope of the line (see Equation 5) is the change in y (rise) over the change in x (run).

Equation 5:

m = (y2-y1) / (x2-x1)

To find the intercept, I solved Equation 4 for b and then substituted in one on my points (x1,y1,z1). Note either point would work.

Equation 6:

b = y1 – mx1

Then I substituted Equation 6 into Equation 4 and consolidated the slope term.

Equation 7:

y=mx+(y1-mx1) = m(x-x1)+y1

Then I substituted Equation 5 into equation 7

Equation 8:

y=[(y2-y1) / (x2-x1)] (x-x1)+y1

This equation works except when x1=x2 (maybe there is a math wiz out there that knows of a work around, but I am not a math wiz. I was just trying to solve a problem). When x1=x2, then the 3D line is confined to the plane perpendicular to the x axis which intersects the x axis at x1. But within in that plane x can no longer yield information about y. So you cannot define the line without y or z information. The equation breaks. You will get a divide by zero error (imagine part of your equation going to infinity).

The formula for z is derived in a similar fashion either as a function of x or y. For the slope and intercept, you would use the x (or y) and z coordinates. Therefore, Equation 2 and Equation 3 are derived.

Since this was an computer code experiment on my part, I used if…then statements to deal with the x1=x2 and y1=y2 conditions switching formulas as my 3D points moved through space.