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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

dot_product

Computes the dot product (or scalar product) of 2 vectors (points).

Synopsis

template<typename Point1, typename Point2>
constexpr select_coordinate_type< Point1, Point2 >::type dot_product(Point1 const & p1, Point2 const & p2)

Parameters

Type

Concept

Name

Description

Point1 const &

Any type fulfilling a Point Concept

p1

first point

Point2 const &

Any type fulfilling a Point Concept

p2

second point

Returns

the dot product

Header

Either

#include <boost/geometry.hpp>

Or

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

Examples

Calculate the dot product of two points

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/arithmetic/dot_product.hpp>
#include <boost/geometry/geometries/adapted/boost_array.hpp>

namespace bg = boost::geometry; 1

BOOST_GEOMETRY_REGISTER_BOOST_ARRAY_CS(cs::cartesian)


int main()
{
     double dp1,dp2,dp3,dp4;
     bg::model::point<double, 3, bg::cs::cartesian> point1(1.0, 2.0, 3.0);
     bg::model::point<double, 3, bg::cs::cartesian> point2(4.0, 5.0, 6.0);

     //Example 1
     dp1 = bg::dot_product(point1, point2);

     std::cout << "Dot Product 1: "<< dp1 << std::endl;

     bg::model::point<double, 2, bg::cs::cartesian> point3(3.0, 2.0);
     bg::model::point<double, 2, bg::cs::cartesian> point4(4.0, 7.0);

     //Example 2	
     dp2 = bg::dot_product(point3, point4);

     std::cout << "Dot Product 2: "<< dp2 << std::endl;

     boost::array<double, 2> a =  {1, 2};
     boost::array<double, 2> b =  {2, 3};

     //Example 3
     dp3 = bg::dot_product(a, b);

     std::cout << "Dot Product 3: "<< dp3 << std::endl;

     return 0;

}

1

Convenient namespace alias

Output:

Dot Product 1: 32
Dot Product 2: 26
Dot Product 3: 8

PrevUpHomeNext