Previous Tutorial: Installation
Overview
This tutorial describes how to get started using Ignition Math with C++.
We will run through an example that determines the distance between two points in 3D space. Start by creating a bare-bones main file using the editor of your choice.
The easiest way to include Ignition Math is through the ignition/math.hh
header file. Alternatively, you can include only the header files you need. For this example, we'll take the short and easy approach.
At this point your main file should look like
#include <ignition/math.hh>
int main()
{
return 0;
}
Now let's create to 3D points with arbitrary values. We will use the ignition::math::Vector3 class to represent these points. Ignition Math provides a handy ignition::math::Vector3d type which is a typedef of Vector3<double>
. The result of this addition will be a main file similar to the following.
#include <ignition/math.hh>
int main()
{
return 0;
}
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Vector3.hh:42
Finally, we can compute the distance between point1
and point2
using the ignition::math::Vector3::Distance() function and output the distance value.
#include <ignition/math.hh>
int main()
{
double distance = point1.Distance(point2);
std::cout <<
"Distance from " << point1 <<
" to " << point2 <<
" is " <<
return 0;
}
Bonus: Vector2 Example
The following is an example program that uses Vector2 to perform some simple computation.
#include <iostream>
#include <ignition/math.hh>
int main(int argc, char **argv)
{
<< "Vec2a: " << vec2a << "\n"
<< "Vec2b: " << vec2b << "\n";
std::cout <<
"Vec2: x=" << vec2.
X() <<
" y=" << vec2.
Y() <<
"\n";
std::cout <<
"Vec2a: x=" << vec2a[0] <<
" y=" << vec2a[1] <<
"\n";
std::cout <<
"Vec2b: x=" << vec2b.X() <<
" y=" << vec2b[1] <<
"\n";
<< vec2 + vec2a << "\n"
<< vec2 - vec2a << "\n"
<< vec2 / vec2a << "\n";
}
Two dimensional (x, y) vector.
Definition: Vector2.hh:37
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:148
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:84
T X() const
Return the x value.
Definition: Vector2.hh:498
T Y() const
Return the y value.
Definition: Vector2.hh:505