Analytical Geometry
How To Check If A Point Is Inside A Rectangle?
The Area Method
- Form a triangle between the point and every pair of consecutive vertices on the rectangle.
- Calculate the area of this triangle
- Sum up the areas of all of these triangles.
- If the sum of these areas is greater than the area of the rectangle, then the point is outside the rectangle, else the sum of the areas must equal the area of the rectangle and the point is either inside or on the edge of the rectangle.
- To differentiate between a point that is on the edge of the rectangle vs. one that is inside it, look at all the triangle areas. If any are 0, then the point is on the edge of the rectangle, otherwise it is inside the rectangle.
This method works not just for a rectangle but for any polygon!
The Angle Method
- Travel around the vertices of the rectangle in a clockwise fashion.
- For each line segment, calculate the angle between this line and a line formed from the point and the first vertex which makes up the line.
- If all the angles are positive (incl. 0), then the point is inside the rectangle. If all the angles are positive and one or more is 0, then the point is on the edge of the rectangle (if one is 0, then it is on the edge, if two are 0, then the point is on one of the rectangle’s corners).
- If one or more angles is negative, then the point is outside the rectangle.
The Product Of Vectors Method
This uses vector notation:
where:
, and are 3 adjacent vertices of the rectangle
is the vector representation of the point that could be inside the rectangle
is the logical less-than operator
is the logical AND operator
Notice that this method does not use point C at all! It projects the point M onto the line AB and AD and makes sure that these projections lie somewhere on the line.
How To Find The Distance Between A Line And A Point In 3D
Given two points and which form a line in 3D space, and a third point in 3D space, the shortest distance between the point and the line is given by:
where:
is the cross-product
is the magnitude of the vector
The Long Way
Assume we have points and which form a line, and we want to find the shortest distance from this line to a point .
We will first find the point which is the closest point to which is on the line. Once we have this, finding the distance of the line which goes from to will be trivial.
The point can be expressed in parametric form using the equation for a line from to :
\mathbf{p4} = u(\mathbf{p2} - \mathbf{p1}) \label{eq:point4} \tag{1}
We want to find so we can then find . Since there are two unknowns, we will need another equation. We can form one from the knowledge that the line and the line that connects the point to this line will be perpendicular, which means that the dot product will be 0. We can write this as:
\begin{equation} (\mathbf{p3} - \mathbf{p4}) \bullet (\mathbf{p2} - \mathbf{p1}) = 0 \label{eq:dot-product} \tag{2} \end{equation}
Substituting into gives us:
(\mathbf{p3} - \mathbf{p1} - u(\mathbf{p2} - \mathbf{p1})) \bullet (\mathbf{p2} - \mathbf{p1}) = 0 \label{eq:substituted} \tag{3}
Expanding gives us:
Factorizing and realizing that is the norm squared:
Re-arrange for :
We can now work out a value for , given the values of the three points we know! Once a value for is found, we can then calculate the location of point using .
Once is found, the distance between the point and the line is simply the distance of the line :
How To Calculate The Angle Between Two Lines In 3D
The one is relatively easy. Given the lines (both are in parametric form), we can use the dot product rule. We don’t need to use or , we just need the directional component of each line, and (think of these as the vectors for each line). Then we can use the dot product rule, and we just need to solve for :