Vector ND – C++

This is a N-Dimensional Vector class written in C++. While it can be used for any dimension, if you would like to use 3 or less dimensions including rotation functions check out my previous post (here)

VectorND.cpp

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef __VECTOR_ND_H__
#define __VECTOR_ND_H__
 
#include "math.h"
#include <string>
#include <iostream>
#include <sstream>
/*
ABOUT: 3D Vector and rotation functions. Rotations about X, Y, Z, and any arbitruary Vector
AUTHOR: Kenny Cason
WEBSITE: Ken-Soft.com
EMAIL: kenneth.cason@gmail.com
DATE: 1-19-2011
*/
 
 
struct VectorND {
 
    long double* pts;
 
    int dim;
 
    VectorND() {
        pts = new long double[0];
        dim = 0;
    }
 
    VectorND(long double x) {
        pts = new long double[1];
        pts[0] = x;
        dim = 1;
    }
 
    VectorND(long double x, long double y) {
        pts = new long double[2];
        pts[0] = x;
        pts[1] = y;
        dim = 2;
    }
 
    VectorND(long double x, long double y, long double z) {
        pts = new long double[3];
        pts[0] = x;
        pts[1] = y;
        pts[2] = z;
        dim = 3;
    }
 
    VectorND(int _dim, long double* _pts) {
        pts = new long double[_dim];
        for(int i = 0; i < _dim; i++) {
            pts[i] = _pts[i];
        }
        dim = _dim;
    }
 
    long double get(int i) {
        return pts[i];
    }
 
    long double* get() {
        return pts;
    }
 
    void set(int i, long double value) {
        pts[i] = value;
    }
 
 
    /**
     * assignment operator
     */
    void operator=(VectorND v) {
        pts = new long double[v.dim];
        for(int i = 0; i < dim; i++) {
            pts[i] = v.get(i);
        }
    }
 
    /**
     * equality operator
     */
    bool operator==(VectorND v) {
        for(int i = 0; i < dim; i++) {
            if(pts[i] != v.get(i)) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * equality operator
     */
    bool operator!=(VectorND v) {
        for(int i = 0; i < dim; i++) {
            if(pts[i] != v.get(i)) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * addition operators
     */
    void operator+=(VectorND v) {
        for(int i = 0; i < dim; i++) {
            pts[i] += v.get(i);
        }
    }
 
    VectorND operator+(VectorND v) {
        VectorND vect(v.dim, v.get());
        for(int i = 0; i < dim; i++) {
            vect.set(i, pts[i] + v.get(i));
        }
        return vect;
    }
 
    void operator++(int) {
        for(int i = 0; i < dim; i++) {
            pts[i]++;
        }
    }
 
    void operator++() {
        for(int i = 0; i < dim; i++) {
            ++pts[i];
        }
    }
 
    /**
     * subtraction operators
     */
    void operator-=(VectorND v) {
        for(int i = 0; i < dim; i++) {
            pts[i] -= v.get(i);
        }
    }
 
    VectorND operator-(VectorND v) {
        VectorND vect(v.dim, v.get());
        for(int i = 0; i < dim; i++) {
            vect.set(i, pts[i] - v.get(i));
        }
        return vect;
    }
 
    void operator--(int) {
        for(int i = 0; i < dim; i++) {
            pts[i]--;
        }
    }
 
    void operator--() {
        for(int i = 0; i < dim; i++) {
            --pts[i];
        }
    }
 
    /**
     * division operators
     */
    void operator/=(long double scalar) {
        for(int i = 0; i < dim; i++) {
            pts[i] /= scalar;
        }
    }
 
    VectorND operator/(long double scalar) {
        VectorND vect(dim, pts);
        for(int i = 0; i < dim; i++) {
            vect.set(i, pts[i] / scalar);
        }
        return vect;
    }
 
    /**
     * multiplication operators
     */
    void operator*=(long double scalar) {
        for(int i = 0; i < dim; i++) {
            pts[i] *= scalar;
        }
    }
 
    VectorND operator*(long double scalar) {
        VectorND vect(dim, pts);
        for(int i = 0; i < dim; i++) {
            vect.set(i, pts[i] * scalar);
        }
        return vect;
    }
 
    /**
     * exponent operators
     */
    void operator^=(float power) {
        for(int i = 0; i < dim; i++) {
            pts[i] = pow(pts[i], power);
        }
    }
 
    VectorND operator^(float power){
        VectorND vect(dim, pts);
        for(int i = 0; i < dim; i++) {
            vect.set(i, pow(pts[i], power));
        }
        return vect;
    }
 
    VectorND unit() {
        VectorND vect(dim,pts);
        double d;
        for(int i = 0; i < dim; i++) {
            d += pts[i] * pts[i];
        }
        d = sqrt(d);
        vect = *(this) / d;
        return vect;
    }
 
    /**
     * toString()
     */
    std::string toString() {
        std::stringstream oss;
        oss << "(";
        for(int i = 0; i < dim - 1; i++) {
            oss << pts[i] << ", ";
        }
        oss << pts[dim - 1] << ")";
        return oss.str();
    }
 
};
#endif

main.cpp

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
#include <iostream>
#include "VectorND.h";
 
using namespace std;
 
int main() {
    cout << "Vector ND" << endl;
    long double* pts = new long double[5]{1,2,3,4,5};
    long double* pts2 = new long double[5]{1,2,3,4,5};
    VectorND v(5, pts);
    VectorND v2(5, pts);
    cout << v.toString() << endl;
    v *= 3;
    cout << v.toString() << endl;
    v /= 3;
    cout << v.toString() << endl;
    v += v2;
    cout << v.toString() << endl;
    v -= v2;
    cout << v.toString() << endl;
    v++;
    cout << v.toString() << endl;
    v--;
    cout << v.toString() << endl;
 
 
    cout << (v == v2) << endl;
    cout << (v != v2) << endl;
 
    cout << (v * 3).toString() << endl;
    cout << (v / 3).toString() << endl;
    cout << (v + v2).toString() << endl;
    cout << (v - v2).toString() << endl;
 
    cout << (v == v2) << endl;
    cout << (v != v2) << endl;
 
    cout << (v ^ 4).toString() << endl;
    v ^= 4;
    cout << (v).toString() << endl;
    cout <<  v.unit().toString() << endl;
    return 0;
}
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Designed by: WordPress Themes | Thanks to best wordpress themes, Find WordPress Themes and Themes Directory