Ray-Casting algorithm in Ray-triangle intersection Currently I started studying about ray-casting when I came across this following problem based on ray-triangle intersection. The problem was:
You are provided with a triangle with vertices ,(x1,y1,z1), (x2,y2,z2) and (x3,y3,z3). A ray with origin (a1,b1,c1) and direction (a2,b2,c2) is also given. Your task is to find: 
1)Whether or not the ray intersects the triangle
2) If the ray intersects the triangle, what's the point of intersection and also find the distance of theat point from the origin of the ray
Examples:
1) For (x1,y1,z1)=(-2,2,6), (x2,y2,z2)=(2,2,6), (x3,y3,z3)=(0,-4,6) , (a1,b1,c1)=(1,0,0),(a2,b2,c2)=(-0.2,0,1) the answer is: coordinates of intersection:(-0.2,0,6) and the distance of the origin of the ray from the point of intersection is 6.12
2) For (x1,y1,z1)=(-2,2,1), (x2,y2,z2)=(2,2,1), (x3,y3,z3)=(0,-4,1) , (a1,b1,c1)=(0,0,0),(a2,b2,c2)=(0,0,1) the answer is: coordinates of intersection:(0,0,1) and the distance of the origin of the ray from the point of intersection is 1 
3) For (x1,y1,z1)=(-10,-2.3,0), (x2,y2,z2)=(4.4,20.3,9.5), (x3,y3,z3)=(9.8,-10,0) , (a1,b1,c1)=(0,0,0),(a2,b2,c2)=(0.68,-1.14,1.82) the answer is: coordinates of intersection:(0.67, -1.12, 1.79) and the distace of the origin of the ray from the point of intersection is 2.22 
All I need is to understand the general equations to solve this question. I need to know how the equations are formed and how the problem is solved. This seems a pretty hard question for me.
 A: The ray can be parameterized by the equation $\mathbf{y}(t) = (a_1,b_1,c_1) + t(a_2,b_2,c_2)$ for all $t\geq 0$. The boundaries of the triangle may be parameterized by
\begin{align}
\mathbf{s}_1(\tau) &= (x_1,y_1,z_1) + \tau(x_2-x_1,y_2-y_1,z_2-z_1)\\
\mathbf{s}_2(\tau) &= (x_2,y_2,z_2) + \tau(x_3 - x_2,y_3-y_2,z_3-z_2)\\
\mathbf{s}_3(\tau) &= (x_3,y_3,z_3) + \tau(x_1 - x_3,y_1 - y_3,z_1-z_3)
\end{align}
for all $\tau\in[0,1]$. The triangle lies in a plane with normal vector 
$$
\mathbf{n} = (x_2-x_1,y_2-y_1,z_2-z_1)\times(x_2 - x_3,y_2 - y_3,z_2-z_3)
$$
If $(a_2,b_2,c_2) \cdot \mathbf{n} \neq 0$, then the line intersects the plane for some $t\in\mathbb{R}$. The intersection point with the ray, if it exists, is given by solving $\mathbf{n}\cdot(\mathbf{y}(t) - (x_1,y_1,z_1)) = 0$ for $t\geq 0$. Lastly, you have to check if the intersection point lies on the triangle. To do this let $\mathbf{y}$ be the intersection point and suppose it does not lie on a boundary of the triangle (if it does then you are done). Define the line $\mathbf{x}(t) = \mathbf{y} + t(\mathbf{s}_1(0.5) - \mathbf{y})$ for all $t\geq 0$. If $\mathbf{x}(t) = \mathbf{s}_2(\tau)$ or $\mathbf{x}(t) = \mathbf{s}_3(\tau)$ have a solution for $t \geq 0$ and $\tau\in[0,1]$, then the point is exterior to the triangle, otherwise it is interior. The distance from the intersection point to the origin of the ray is simply $\|\mathbf{y} - (a_1,b_1,c_1)\|$.
