A.05 - Analytic Geometry
1 - Introduction
Analytic geometry is the study of geometry using a coordinate system. As explained in appendix 01, we can represent vectors both geometrically and numerically. In analytic geometry we can use vectors in equations to numerically define geometries as set of points in a Cartesian coordinate system.
This also allows to numerically solve geometric problems such as measuring distances, calculating intersections and projections, and so on.
In this tutorial we will only scratch the surface to explain what we need to build the graphic applications presented in upcoming tutorials. However, additional information will be provided in later appendices. Also, more details can be found in the references listed at the end of this tutorial.
2 - Points and directions
From appendix 01, we know that points and directions can be represented by vectors.

3 - Lines, rays, and segments
Lines can be described by a point and a direction with the following parametric equation
where is a function of that can be used to calculate all points of a line. Indeed, setting we scale the vector , which summed to the point result in a translation of the point (see appendix 03). This translation allows to specify a new point of the line, as shown in the figure below.

If we restrict the parameter to be a positive real number (that is, ), we can define a ray starting in (the origin of the ray) that never changes direction.
We need two points and to specify the ends of a line segment. At that point we can define the vector between the ends of the line segment. This vector specifies the direction of the line segment, so that we can use it to derive the equation of the line where the line segment lies.
Restricting in the range we can specify all points of the line segment, as shown in the figure below.

3.1 - Distance between a point and a line
To measure the distance of a point from a line we can use the following formula.

Indeed, from trigonometry, and considering the figure above, we have that
4 - Parallelograms and triangles
Given a point and two non-parallel directions and (that is, ), we can define the parametric equation of a parallelogram as follows
where the parameters and are real numbers in the range .
Indeed, all points of a parallelogram can be specified as the sum of the scaled sides applied to a point, as shown in the figure below.

If we restrict and so that then becomes the parametric equation of a triangle. Indeed, considering the image above (as well as the one in the next section), if we set and then the point will cross the minor diagonal of the parallelogram, specifying a point outside the triangle with sides and .
4.1 - Barycentric coordinates
Most of the time, to define a triangle we will use three points , and , to specify its vertices. Then, we can define the vectors and . Substituting into the parametric equation of a triangle (that is, with ) we have
where , so that .
In this case we call the barycentric coordinates of a generic point inside a triangle defined by three points , and .

Barycentric coordinates allow to specify a generic point inside a triangle as a linear combination (weighted sum) of its vertices. The higher the value of a barycentric coordinate, the more the point is attracted by the related vertex.
5 - Planes
A plane can be specified by a point on the plane and a direction perpendicular to the plane called normal vector (or simply normal). A plane divides the 3D space in two half-spaces: an upper (or positive) halp-space and a lower (or negative) halp-space. The normal to a plane always points towards the upper half-space. If a point is in the upper half-space, then is in front of the plane, otherwise it is behind it.

As you can verify in the image above, a generic point of the plane must solve the following equation since the vectors and have to be orthogonal to each other for every point on the plane.
This is called equation of the plane because the set of points satisfying this equation represents the plane. The equation of the plane can also be written as follows
where .
If and , the equation of the plane can also be rewritten as
which is the well-know algebraic formula used to specify a plane in a 3D Cartesian coordinate system. Observe that here is the only unknown because we already know and , so that we also know .
5.1 - Distance between a point and a plane
Often we will use the vector to specify a plane. As explained in appendix 01, if is a unit vector then is the signed length of the orthogonal projection of onto . In that case, represents the signed distance between the plane and the origin of the 3D Cartesian coordinate system in which the plane is defined, as shown in the figure below.

In general, we have a negative distance between a point and a plane if the point is behind the plane (that is, the point is in the negative\lower half-space). Indeed, the distance of a point from a plane is given by the following equation
which makes sense since this distance is calculated subtracting the length of the projection of onto (that is, ) from the length of the projection of onto (that is, ), as illustrated in the figure below.

Observe that in the Cartesian systems c and d of the image above both and are negative because the angles are greater than .
Therefore, if is in the lower half-space, then is negative, otherwise it is positive. So, we can conclude that
-
If , then is in front of the plane.
-
If , then is behind the plane.
-
If , then lies in the plane.
5.2 - Normalizing a Plane
If we have a plane where is not a unit vector, we can’t simply normalize because (that is, depends on ). This means that to normalize we must change as well. For this purpose, we just need to divide both and by .
5.3 - Line/Plane Intersection
To check if a line, a ray, or a line segment intersects a plane we can proceed as follows.
Given a line (the equation is the same for rays and segments, as shown in section 3) and a plane , we can substitute the equation of the line into the equation of the plane to find out if both have a shared point (that is, if the line intersects the plane). For this purpose, we need to find a value for the parameter which is both finite and different from zero.
If either there are no solutions (line and plane are parallel) or there are infinite solutions (the line lies in the plane). If we find a unique, finite solution for , we can substitute this value into the equation of the line to compute the coordinates of the point of intersection. Observe that solutions for outside the range and are invalid with rays and line segments, respectively.
5.4 - Reflecting directions and points with respect to a plane
Directions are free vectors (that is, it doesn’t matter where we apply them). However, often to perform calculations on directions it can be useful to apply them to a common point. The figure below shows that to calculate the reflection of a direction with respect to the normal to a plane, we can add to the inverse of its projection onto , doubled in size. That is,

Observe that the normal needs to be a unit vector because we used the orthogonal projection of onto to calculate (see appendix 01). However, we can always normalize the normal vector using the method presented in section 5.2.
To reflect points with respect to a plane, we need a point on the plane in addition to its normal (that is, we need the vector that represents the plane). Indeed, as you can see in the figure below, to calculate the reflection of a point with respect to the plane , we can add to the inverse of the projection of onto , doubled in size. That is,

Observe that the reflection can be considered as a translation of along the inverse direction of the normal. In this case the normal doesn’t need to be a unit vector. However, if that’s the case, we have
As you can see, the reflection of directions is very similar to the reflection of points (especially if you look at the red expression above). And indeed, we can handle both these cases in the same way with the help of the following matrix of reflection with respect to a plane .
Multiplying a point by we can check that the result is the reflected point .
In a similar way, multiplying a direction by we can check that the result is the reflected direction .
Therefore, the matrix allows to reflect both points and directions with respect to a plane , provided is a unit vector (remember that you can always normalize it).
5.5 - Projecting points onto a plane
A point can be projected onto a plane along a particular direction , as illustrated in the following image.

In this case, we have all the necessary information to build the equation of the plane , and the equation of the line passing through the point as well . Therefore, we can simply calculate their point of intersection using the method presented in section 5.3. Suppose we find a valid solution for the parameter , then we can substitute into the equation of the line to compute the coordinates of the point of intersection .
On the other hand, a point can also be projected onto a plane with respect to another point (observe that the w-component now is 1).

In this case, the direction of projection is , so that equation becomes
Fortunately, we can still handle both these cases in the same way with the help of the following matrix of projection onto a plane , provided that the multiplication by this matrix is followed by a perspective division (that is, we consider it as a perspective projection matrix, as explained in appendix 04; more on this shortly).
Multiplying a point by , and considering , we have
Computing the i-th component of we get
Therefore, we have that
As you can see, differs from (equation ) by a factor of . That is, we need to divide by to get . Fortunately, the last component of is
so that, considering as a perspective projection matrix, we can get after the rasterizer stage, which automatically performs the perspective division (that is, it divides the components of each vertex in input by its w-component; see appendix 04). This means that, if we have objects we want to project onto a plane, we can multiply their world matrix by in the vertex shader to take advantage of the following perspective division performed by the rasterizer.
Multiplying a vertex by (the composition between world matrix and ) we get , the projection of in world coordinates. It does not matter if we also multiply by and\or (the view and perspective projection matrices, respectively). Indeed, it ends up being a simple matrix composition where we add transformations without affecting the effect of each transformation (see appendix 03). In other word, the perspective division will take into account the term , and the terms of any other possible projections as well, because the w-component in homogeneous clip space will contain all of them.
However, we need to solve a problem: when we use the perspective division to calculate the projection of a vertex onto a plane (in addition to its perspective projection onto the projection window), the vertex could be incorrectly clipped by the rasterizer. Indeed, as explained in appendix 04, clipping is performed in homogeneous clip space, where if the w-coordinate is negative, the related vertex is considered outside the visible region, and discarded by the clipping algorithm.

Unfortunately, in this case we have that the w-coordinate contains the cosine between two vectors , so that we can often get a negative value, as illustrated in the following image.

To solve this problem we can simply reverse the direction of projection when it forms an angle greater than with . That’s not a big problem because the equation of the line is still the same, as well as the point of intersection. However, we will get a different solution for the parameter in equation to take into account the change of direction. Substituting into the equation of the line, still returns the point of intersection .
On the other hand, multiplying a point by , and considering as a point, we have
Computing the i-th component of we get
Therefore, we have that
As you can see, differs from (equation ) by a factor of , which is exactly the value of the last component of
This means that after the perspective division we get , the projection of with respect to the point . Observe that in this case won’t be negative if forms an angle greater than with because the dot product is between and the inverse of the normal. Obviously, will be negative if the angle is less than . However, even in this case we hardly reverse one of the two vectors because usually we are mainly interested in projecting onto the front face of the plane.
Therefore, we just showed we can handle both cases (projection onto a plane along a direction and with respect to a point) with the help of the matrix .
6 - Cramer’s rule
A general system of linear equations with unknowns and coefficients can be written as
where are the unknowns, are the coefficients of the system, and are the constant terms.
We can also write it in matrix form as follows
where is an matrix consisting of the coefficients of the system, is a column vector with the unknowns, and is a column vector with the constant terms.
If (i.e., if is a square matrix), then we can find the i-th unknown by dividing the determinat of by the determinant of . We can build the matrix starting from , and replacing its i-th column with , the column vector of the constant terms.
Below is a practical demonstration showing the validity of this formula.
Suppose we have a system of 3 linear equations with 3 unknowns .
Assuming the matrix has a non-zero determinant , and remembering that if we add a scaled column to another column of a matrix the determinant does not change (see appendix 03), we have
Therefore, the first unknown is
By similar calculations we can compute the other two unknowns ( and ).
References
[1] Practical Linear Algebra: A Geometry Toolbox (Farin, Hansford)
[2] Introduction to 3D Game Programming with DirectX 12 (Luna)
[3] Real-Time Rendering (Haines, Möller, Hoffman)
[4] Jim Blinn’s Corner: A Trip Down the Graphics Pipeline (Blinn)
If you found the content of this tutorial somewhat useful or interesting, please consider supporting this project by clicking on the Sponsor button. Whether a small tip, a one time donation, or a recurring payment, it’s all welcome! Thank you!