File: vector.md

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 (132 lines) | stat: -rw-r--r-- 3,228 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
\page vector Vector example

This tutorial explains how to use the `Vector` classes from Ignition Math library.

## C++ example

### Compile the code

To compile the code, go to `ign-math/examples` and use `cmake`:

```{.sh}
git clone https://github.com/ignitionrobotics/ign-math/ -b ign-math6
cd ign-math/examples
mkdir build
cd build
cmake ..
make
```

When the code is compiled, run:

```{.sh}
./vector2_example
```

The ouput of the program:

```{.sh}
Vec2: 2 4
Vec2a: 1 2
Vec2b: 1.2 3.4
Vec2: x=2 y=4
Vec2a: x=1 y=2
Vec2b: x=1.2 y=3.4
4
2 8
3 6
1 2
2 2
2.23607
```

### Code

Create a `Vector2` called `vec2` of doubles using the typedef `Vector2d`. **The initial x and y values are zero**. The x and y component of `vec2` can be set at anytime.

\snippet examples/vector2_example.cc constructor


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

\snippet examples/vector2_example.cc constructor2

It's also possible to set initial values. Here we are using a `Vector2` of floats:

\snippet examples/vector2_example.cc constructor3

We can output the contents of each vector using `std::cout`.

\snippet examples/vector2_example.cc stdout

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]`.

\snippet examples/vector2_example.cc access

The `Vector2` class overloads many common operators, such as:

\snippet examples/vector2_example.cc operators

There are also many useful function such as finding the distance between two vectors.

\snippet examples/vector2_example.cc distance

**There are more functions in Vector2. Take a look at the [API](https://ignitionrobotics.org/libs/math)**

## Ruby examples

This example will only work if the Ruby interface library was compiled and installed. Modify the `RUBYLIB` environment variable to include the Ignition Math library install path. For example, if you install to `/usr`:

```{.sh}
export RUBYLIB=/usr/lib/ruby:$RUBYLIB
```

Execute the examples:

```{.sh}
ruby vector2_example.rb
ruby vector3_example.rb
```

### Code

Create a `Vector2` of doubles using the typedef `Vector2d`. It's possible to set initial values or use another object to create a identical copy.

```{.rb}
va = Ignition::Math::Vector2d.new(1, 2)
```

You can get access to each component in the vector using the `X()`, `Y()` accessors.

```{.rb}
printf("va = %f %f\n", va.X(), va.Y())
printf("vb = %f %f\n", vb.X(), vb.Y())
printf("vc = %f %f\n", vc.X(), vc.Y())
```

The `Vector2` class overloads many common operators, such as:

```{.rb}
vb += va
printf("vb += va: %f %f\n", vb.X(), vb.Y())
```

There are also many useful functions, such as finding the distance between two vectors or normalizing a vector.

```{.rb}
vb.Normalize
printf("vb.Normalize = %f %f\n", vb.X(), vb.Y())
printf("vb.Distance(va) = %f\n", vb.Distance(va))
```

You can create vectors with 3 dimensions using the typedef `Vector3d`:

```{.rb}
v1 = Ignition::Math::Vector3d.new(0, 0, 0)
```

You can also get access to each component in the vector using the `X()`, `Y()` and `Z()` accessors:

```{.rb}
printf("v =: %f %f %f\n", v1.X(), v1.Y(), v1.Z())
```