-
-
Notifications
You must be signed in to change notification settings - Fork 0
A.05 Analytic Geometry
Analytic geometry is the study of geometry through the use of a coordinate system. As explained in appendix 01, vectors can be expressed both visually and numerically. In analytic geometry, we can use vectors in equations to define geometries numerically as sets of points within a Cartesian coordinate system. This approach enables us to numerically solve geometric problems, such as determining distances, computing intersections and projections, and more.
In this tutorial, we will only provide a brief overview of the essential concepts required for building the graphic applications presented in the forthcoming tutorials. Additional topics will be discussed in subsequent tutorials and appendices. For a more comprehensive and in-depth coverage, please refer to the references listed at the end of this tutorial.
From appendix 01, we know that points and directions can be represented by vectors.
Lines can be described by a point
where
If we limit the parameter
To define a line segment, we require the two endpoints
By restricting the parameter
To measure the distance
Indeed, from trigonometry and considering the image above, we have that
Given a point
where the parameters
Indeed, all points of a parallelogram can be specified as the sum of the scaled sides applied to a point, as shown in the image below.
If we restrict
Most of the time, to define a triangle we will use three points
where
In this case, we call
Barycentric coordinates allow to specify a generic point
A plane can be specified by a point
As you can verify in the image above, a generic point
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
This equation is a well-known algebraic formula used to define a plane in a 3D Cartesian coordinate system. It's worth noting that in this equation the only unknown is the point
Often we will use the vector
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
which makes sense since this distance is calculated subtracting the length of the projection of
Observe that, in the Cartesian systems illustrated in the images c and d of the image above, both
Therefore, if
-
If
$\mathbf{n}\cdot (\mathbf{p}-\mathbf{p} _0)=\mathbf{n}\cdot \mathbf{p}+d>0$ , then$\mathbf{p}$ is in front of the plane. -
If
$\mathbf{n}\cdot (\mathbf{p}-\mathbf{p} _0)=\mathbf{n}\cdot \mathbf{p}+d<0$ , then$\mathbf{p}$ is behind the plane. -
If
$\mathbf{n}\cdot (\mathbf{p}-\mathbf{p} _0)=\mathbf{n}\cdot \mathbf{p}+d=0$ , then$\mathbf{p}$ lies in the plane.
If we have a plane
To determine if a line, a ray, or a line segment intersects a plane we can proceed as follows.
Given a line
If
Directions are free vectors, meaning their application point doesn't matter. However, when performing calculations involving directions, it can be beneficial to apply them to a common point. The image below shows that to compute the reflection
Observe that for this calculation to work correctly, the normal vector
When reflecting points with respect to a plane, we require both a point on the plane and its normal vector (that is, we need the vector
Observe that the reflection
As you can see, the reflection of points is quite similar to the reflection of directions (especially the part highlighted in red). And in fact, both cases can be handled in a unified way using the following reflection matrix with respect to a plane
Multiplying a point
In a similar way, multiplying a direction
Therefore, we can use the matrix
A point
In this case, we have all the necessary information to build the equation of the plane
On the other hand, a point
In this case, the direction of projection is
Fortunately, we can still handle both these cases in a unified manner using the following matrix of projection onto a plane
It works provided that the multiplication by this matrix is followed by a perspective division (that is, we need to consider it as a perspective projection matrix, as explained in appendix 04; the rationale behind this will be clarified shortly).
Multiplying a point
Computing the i-th component of
Therefore, we have that
As you can see,
Therefore, considering
Multiplying a vertex
$\mathbf{v}$ by$\mathbf{WS}$ (the composition between world matrix and$\mathbf{S}$ ) we get$\mathbf{s}'$ , the projection of$\mathbf{v}$ in world coordinates. It does not matter if we also multiply$\mathbf{s}'$ by$\mathbf{V}$ and\or$\mathbf{P}$ (the view and perspective projection matrices, respectively). In fact, it ends up being a simple matrix composition where we keep adding transformations without affecting the effect of previous one (see appendix 03). In other words, the perspective division will take into account the term$(\mathbf{n}\cdot\mathbf{l})$ , as well as the terms of any other projections (i.e., the w-component in homogeneous clip space will include the terms of all the perspective transformations).
However, we need to address 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 can be 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.
Transforming a vertex position using the matrix
$\mathbf{S}$ results in the w-coordinate containing the cosine between two vectors$(\mathbf{n}\cdot\mathbf{l})$ , 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
$\mathbf{l}$ when it forms an angle greater than$90°$ with$\mathbf{n}$ . This adjustment does not pose a significant problem because the equation of the line$\mathbf{r}(t)$ remains the same. The only difference lies in obtaining a distinct solution$t_s$ for the parameter$t$ in equation$(1)$ to account for the change of direction. However, substituting$t_s$ into the equation of the line still yields the point of intersection$\mathbf{s}$ .
On the other hand, multiplying a point
Computing the i-th component of
Therefore, we have that
As you can see,
This means that after the perspective division we get
So, 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
A general system of
where
We can also write it in matrix form as follows
where
If
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
Therefore, the first unknown
By performing similar calculations, we can determine the other two unknowns (
[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!