File: vector2_example.cc

package info (click to toggle)
ignition-math4 4.0.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,840 kB
  • sloc: cpp: 18,143; python: 2,716; ansic: 503; sh: 168; makefile: 16
file content (66 lines) | stat: -rw-r--r-- 2,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (C) 2012 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

#include <iostream>
#include <ignition/math.hh>

int main(int argc, char **argv)
{
  // Create a Vector2 called vec2 of doubles using the typedef Vector2d.
  // The initial x any y values are zero.\n\n";
  ignition::math::Vector2d vec2;

  // The x and y component of vec2 can be set at anytime.
  vec2.Set(2.0, 4.0);

  // The Vector2 class is a template, so you can also create a Vector2 using
  // ignition::math::Vector2<double>
  ignition::math::Vector2<double> vec2a;

  vec2a.Set(1.0, 2.0);

  // It's also possible to set initial values. This time we are using
  // a Vector2 of floats
  ignition::math::Vector2f vec2b(1.2f, 3.4f);

  // We can output the contents of each vector using std::cout
  std::cout << "Vec2: " << vec2 << "\n"
            << "Vec2a: " << vec2a << "\n"
            << "Vec2b: " << vec2b << "\n";

  // You can also get access to each component in the vector using the
  // X(), Y() accessors or the [] operator.
  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";

  // The [] operator is clamped to the range [0, 1]
  std::cout << vec2[3] << std::endl;

  // The Vector2 class overloads many common operators
  std::cout << vec2 * vec2a << "\n"
            << vec2 + vec2a << "\n"
            << vec2 - vec2a << "\n"
            << vec2 / vec2a << "\n";

  // There are also many useful function such as finding the distance
  // between two vectors
  std::cout << vec2.Distance(vec2a) << std::endl;

  // There are more functions in Vector2. Take a look at the API;
  // http://ignitionrobotics.org/libraries/ign_mat/api
}