This tutorial explains how to use the Vector2 class from Gazebo Math library.
C++ example
Compile the code
To compile the code, go to gz-math/examples and use cmake:
When the code is compiled, run:
The output of the program:
Code
Create a Vector2 called vec2 of doubles using the typedef Vector2d. The initial x and y values are zero. The x and y components of vec2 can be set at anytime.
The Vector2 class is a template, so you can also create a Vector2 using gz::math::Vector2<double>:
It's also possible to set initial values. Here we are using a Vector2 of floats:
We can output the contents of each vector using std::cout.
You can also get access to each component in the vector using the X(), Y() accessors or the [] operator, The operator is clamped to the range [0, 1].
The Vector2 class overloads many common operators, such as:
There are also many useful functions, such as finding the distance between two vectors.
There are more functions in Vector2. Take a look at the API
Ruby examples
This example will only work if the Ruby interface library was compiled and installed. For example, on Ubuntu:
Be sure to replace <#> with a number value, such as 8 or 7, depending on which version you need.<#>.
Modify the RUBYLIB environment variable to include the Gazebo Math library install path. For example, if you install to /usr:
Move to the examples folder:
Execute the example:
The output of the program:
Execute the example:
The output of the program:
Code
Create a Vector2 of doubles using the typedef Vector2d. It's possible to set initial values or use another object to create an identical copy.
You can get access to each component in the vector using the X(), Y() accessors.
The Vector2 class overloads many common operators, such as:
There are also many useful functions, such as finding the distance between two vectors or normalizing a vector.
You can create vectors with 3 dimensions using the typedef Vector3d:
You can also get access to each component in the vector using the X(), Y() and Z() accessors: