Cross product
lastmod 2024-03-07created 2016-08-2912 edits
| WARNING |
| This page is about the ‘cross product’, where multiplying vectors gives a vector. For the ‘outer product’, where multiplying vectors gives a matrix, see that page. |
Definition of the cross product Right-hand rule
The vector product, or cross product, is a type of binary operation between vectors in 3-dimensional space in mathematics. Unlike the scalar product, whose result is a scalar, its result is a vector. It appears in physics formulas such as angular momentum and the Lorentz force.
Uses
- Used to find a vector perpendicular to both vectors.
- Using the property above, you can find the relative position of a point to a line (left side, right side).
Relative position of a point to a line
Using the right-hand rule of the cross product (Cross-Product), this uses the property that the result becomes negative or positive depending on the positions of Vector(p1 - p0) and Vector(p2 - p0).
\(\begin{align} & \vec{v_1} = p1 - p0 = [p1_x - p0_x, p1_y - p0_y, 0] \\ & \vec{v_2} = p2 - p0 = [p2_x - p0_x, p2_y - p0_y, 0] \\ & \vec{v_1} \times \vec{v_2} = [0, 0, ((p1_x - p0_x) * (p2_y - p0_y) - (p1_y - p0_y) * (p2_x - p0_x))] \\ \end{align}\)
C++ code sample:
/**
* Obtain the relative position of point to line.
*
* @translate{ko, 라인을 기준으로 점의 상대적 위치를 확인한다.}
*
* @param p0 [in] Point 0 of line.
* @param p1 [in] Point 1 of line.
* @param check [in] Check point.
*
* @return If a positive number, the right side of the vector. @n
* if a negative number, the left side of the vector. @n
* if 0 is on the line.
*
* @remarks
* Example:
* @code
* |
* | * p2
* |
* | p0 *------>* p1
* |
* -+------------------
* |
* @endcode
* \f[
* \vec{v_1} = p1 - p0 = [p1_x - p0_x, p1_y - p0_y, 0]
* \vec{v_2} = p2 - p0 = [p2_x - p0_x, p2_y - p0_y, 0]
* \vec{v_1} \times \vec{v_2} = [0, 0, ((p1_x - p0_x) * (p2_y - p0_y) - (p1_y - p0_y) * (p2_x - p0_x))]
* \f]
* 외적(Cross-Product)의 오른손 법칙을 사용하여, Vector(p1 - p0)과 Vector(p2 - p0)의 위치에 따라 음수 또는 양수로 변하는 성질을 이용한다.
*/
template <typename T, typename Point = libtbag::geometry::BasePoint2<T> >
inline T getRelativePositionOfPointToLine(Point const & p0, Point const & p1, Point const & check)
{
return ((p1.x - p0.x) * (check.y - p0.y)) - ((p1.y - p0.y) * (check.x - p0.x));
}
enum RelativePosition
{
RELATIVE_POSITION_RIGHT,
RELATIVE_POSITION_LEFT,
RELATIVE_POSITION_ON
};
template <typename T, typename Point = libtbag::geometry::BasePoint2<T> >
inline RelativePosition getRelativePositionOfPointToLine2(Point const & p0, Point const & p1, Point const & check)
{
T const RESULT = getRelativePositionOfPointToLine<T, Point>(p0, p1, check);
if (RESULT > 0) {
return RELATIVE_POSITION_RIGHT;
} else if (RESULT < 0) {
return RELATIVE_POSITION_LEFT;
} else {
return RELATIVE_POSITION_ON;
}
}
See also
Favorite site
- Wikipedia (en) Cross product
- msparkms.tistory - Cross Product
- Derivation and proof of the vector cross product formula
- Finding the position of a point relative to a polygon
- 2DGeometry - Finding the position of a point in a space divided by a line or line segment 1 (method using the Cross product)
References
-
2DGeometry_-line_point_position-_cross_product.pdf ↩