File: vector2_example.cc

package info (click to toggle)
ignition-math 6.10.0%2Bds3-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,252 kB
  • sloc: cpp: 28,553; python: 7,309; ansic: 1,463; ruby: 910; sh: 63; makefile: 18
file content (80 lines) | stat: -rw-r--r-- 2,590 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * 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.
 *
*/
//! [complete]
#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";
  // The x and y component of vec2 can be set at anytime.
//! [constructor]
  ignition::math::Vector2d vec2;
  vec2.Set(2.0, 4.0);
//! [constructor]

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

  vec2a.Set(1.0, 2.0);
  //! [constructor2]

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

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

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

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

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

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

  // There are more functions in Vector2. Take a look at the API:
  // https://ignitionrobotics.org/libs/math
}
//! [complete]