Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

cross_product

Computes the cross product of two vectors.

Description

All vectors should have the same dimension, 3 or 2.

Synopsis

template<typename P, std::enable_if_t< dimension< P >::value !=3||! traits::make< P >::is_specialized, int >>
P cross_product(P const & p1, P const & p2)

Parameters

Type

Concept

Name

Description

std::enable_if_t< dimension< P >::value !=3||! traits::make< P >::is_specialized, int >

-

Must be specified

P const &

p1

first vector

P const &

p2

second vector

Returns

the cross product vector

Header

#include <boost/geometry/arithmetic/cross_product.hpp>

Examples

Calculate the cross product of two points

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/arithmetic/cross_product.hpp>

namespace bg = boost::geometry; 1

int main()
{
     //Example 1 2D Vector

     bg::model::point<double, 2, bg::cs::cartesian> p1(7.0, 2.0);
     bg::model::point<double, 2, bg::cs::cartesian> p2(4.0, 5.0);

     bg::model::point<double, 2, bg::cs::cartesian> r1;

     r1 = bg::cross_product(p1,p2);

     std::cout << "Cross Product 1: "<< r1.get<0>() << std::endl;  //Note that the second point (r1.get<1>) would be undefined in this case

     //Example 2 - 3D Vector 

     bg::model::point<double, 3, bg::cs::cartesian> p3(4.0, 6.0, 5.0);
     bg::model::point<double, 3, bg::cs::cartesian> p4(7.0, 2.0, 3.0);

     bg::model::point<double, 3, bg::cs::cartesian> r2;

     r2 = bg::cross_product(p3,p4);

     std::cout << "Cross Product 2: ("<< r2.get<0>() <<","<< r2.get<1>() <<","<< r2.get<2>() << ")"<< std::endl;

     return 0;
}

1

Convenient namespace alias

Output:

Cross Product 1: 27
Cross Product 2: (8,23,-34)

PrevUpHomeNext