title: How to set up quadratic equation for a ray/sphere intersection?
body: <p>I'm researching the math for a ray tracer, but I'm not following a transition that is made in just about every article I've read on the subject. This is what I have:</p>
  
  <p>Formula for a sphere:</p>
  
  <p>(X - Cx)^2 + (Y - Cy)^2 + (Z - Cz)^2 - R^2 = 0</p>
  
  <p>Where R is the radius, C is the center, and X, Y, Z are all the points in the sphere.</p>
  
  <p>Formula for a line:</p>
  
  <p>X + DxT, Y + DyT, Z + DzT</p>
  
  <p>where D is a normalized direction vector for the line and X, Y, Z are all the points on the line, and T is a parameter for some point on the line.</p>
  
  <p>By substituting the components of the line into the sphere equation, we get:</p>
  
  <p>(X + DxT - Cx)^2 + (Y + DyT - Cy)^2 + (Z + DzT - Cz)^2 - R^2 = 0</p>
  
  <p>I follow everything up to that point (at least I think I do), but then every tutorial I've read makes a jump from that to a quadratic equation without explaining it (this is copied from one of the sites, so the terms are a little different from my example):</p>
  
  <p>A = Xd^2 + Yd^2 + Zd^2</p>
  
  <p>B = 2 * (Xd * (X0 - Xc) + Yd * (Y0 - Yc) + Zd * (Z0 - Zc))</p>
  
  <p>C = (X0 - Xc)^2 + (Y0 - Yc)^2 + (Z0 - Zc)^2 - Sr^2</p>
  
  <p>I get how to then solve for T using the quadratic formula, but I don't understand how they get to the quadratic equation from the above formulas. I'm assuming that's just some piece of common math knowledge that I've long since forgotten, but googling for "How to set up a quadratic equation" hasn't really yielded anything either.</p>
  
  <p>I'd really like to understand how to get to this step before moving on, as I don't like writing code I don't fully grasp.</p>