Skip to main content

Analytical Geometry

Geoffrey Hunter
mbedded.ninja Author

How To Check If A Point Is Inside A Rectangle?

The Area Method

  1. Form a triangle between the point and every pair of consecutive vertices on the rectangle.
  2. Calculate the area of this triangle
  3. Sum up the areas of all of these triangles.
  4. 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.
  5. 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

  1. Travel around the vertices of the rectangle in a clockwise fashion.
  2. 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.
  3. 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).
  4. If one or more angles is negative, then the point is outside the rectangle.

The Product Of Vectors Method

This uses vector notation:

(0<AMAB<ABAB)(0<AMAD<ADAD)(0 < AM \cdot AB < AB \cdot AB) \wedge (0 < AM \cdot AD < AD \cdot AD)

where:
AA, BB and BB are 3 adjacent vertices of the rectangle
MM is the vector representation of the point that could be inside the rectangle
<< is the logical less-than operator
\wedge 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 x1x_1 and x2x_2 which form a line in 3D space, and a third point x0x_0 in 3D space, the shortest distance dd between the point x0x_0 and the line is given by:

d=(x0x1)×(x0x2)x2x1d = \frac{|(x_0 - x_1) \times (x_0 - x_2)|}{|x_2 - x_1|}

where:
×\times is the cross-product
vector| vector | is the magnitude of the vector

The Long Way

Assume we have points p1\mathbf{p1} and p2\mathbf{p2} which form a line, and we want to find the shortest distance from this line to a point p3\mathbf{p3}.

A diagram showing a 3D point to line distance problem.

We will first find the point p4\mathbf{p4} which is the closest point to p3\mathbf{p3} which is on the line. Once we have this, finding the distance of the line which goes from p3\mathbf{p3} to p4\mathbf{p4} will be trivial.

The point p4\mathbf{p4} can be expressed in parametric form using the equation for a line from p1\mathbf{p1} to p2\mathbf{p2}:

\mathbf{p4} = u(\mathbf{p2} - \mathbf{p1}) \label{eq:point4} \tag{1}

We want to find uu so we can then find P4P4. Since there are two unknowns, we will need another equation. We can form one from the knowledge that the P1P2P1 P2 line and the line that connects the point P3P3 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 \eqrefeq:point4\eqref{eq:point4} into \eqrefeq:dotproduct\eqref{eq:dot-product} gives us:

(\mathbf{p3} - \mathbf{p1} - u(\mathbf{p2} - \mathbf{p1})) \bullet (\mathbf{p2} - \mathbf{p1}) = 0 \label{eq:substituted} \tag{3}

Expanding \eqrefeq:substituted\eqref{eq:substituted} gives us:

p3(p2p1)p1(p2p1)u(p2p1)(p2p1)=0\mathbf{p3} \bullet (\mathbf{p2} - \mathbf{p1}) - \mathbf{p1} \bullet (\mathbf{p2} - \mathbf{p1}) - u(\mathbf{p2} - \mathbf{p1})\bullet(\mathbf{p2} - \mathbf{p1}) = 0

Factorizing and realizing that (p2p1)(p2p1)(\mathbf{p2} - \mathbf{p1})\bullet(\mathbf{p2} - \mathbf{p1}) is the norm squared:

(p3p1)(p2p1)up2p12=0(\mathbf{p3} - \mathbf{p1}) \bullet (\mathbf{p2} - \mathbf{p1}) - u\Vert\mathbf{p2} - \mathbf{p1}\Vert^2 = 0

Re-arrange for uu:

u=(p3p1)(p2p1)p2p12u = \frac{(\mathbf{p3} - \mathbf{p1}) \bullet (\mathbf{p2} - \mathbf{p1})}{\Vert\mathbf{p2} - \mathbf{p1}\Vert^2}

We can now work out a value for uu, given the values of the three points we know! Once a value for uu is found, we can then calculate the location of point p4\mathbf{p4} using \eqrefeq:point4\eqref{eq:point4}.

Once p4\mathbf{p4} is found, the distance between the point p3\mathbf{p3} and the line is simply the distance of the line p3p4\mathbf{p3p4}:

d=p4p3d = \Vert\mathbf{p4} - \mathbf{p3}\Vert

How To Calculate The Angle Between Two Lines In 3D

The one is relatively easy. Given the lines l1=a1t+b1l_1 = a_1 t + b_1 (both are in parametric form), we can use the dot product rule. We don't need to use b1`b_1` or b2b_2, we just need the directional component of each line, a1a_1 and a2a_2 (think of these as the vectors for each line). Then we can use the dot product rule, and we just need to solve for θ\theta:

cosθ=abab\cos \theta = \frac{\b{a} \cdot \b{b}}{|\b{a}||\b{b}|}