Skip to content

Cross product

WARNING

이 문서는 벡터끼리 곱하면 벡터를 얻게 되는 ‘외적’(cross product)에 관한 것입니다. 벡터끼리 곱하면 행렬을 얻게 되는 ‘외적’(outer product)에 대해서는 해당 문서를 참조하십시오.

벡터곱의 정의 오른손 법칙

벡터곱(vector곱, 영어: cross product) 또는 외적(外積)은 수학에서 3차원 공간의 벡터들간의 이항연산의 일종이다. 연산의 결과가 스칼라인 스칼라곱과는 달리 연산의 결과가 벡터이다. 물리학의 각운동량, 로런츠 힘등의 공식에 등장한다.

용도

  • 두 벡터에 모두 수직인 벡터를 구할 때 사용한다.
  • 위 성질을 사용하여 선과 점의 상대적 위치(왼쪽편, 오른쪽편)를 구할 수 있다.

선과 점의 상대적 위치

외적(Cross-Product)의 오른손 법칙을 사용하여, Vector(p1 - p0)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++ 코드 샘플:

/**
 * 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

References


  1. 2DGeometry_-line_point_position-_cross_product.pdf