-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
192 lines (148 loc) · 6.51 KB
/
Vector.h
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
/********************************************************************
Vector.h Header File
Vector Algebra Objects, Methods, and Procedures
Donald H. House April 17, 1997
Visualization Laboratory
Texas A&M University
Copyright (C) - Donald H. House. 2005
*********************************************************************/
#ifndef _H_Vector
#define _H_Vector
#include "Utility.h"
using namespace std;
/* Vector Descriptions and Operations */
class Vector2d;
class Vector3d;
class Vector4d;
class Vector;
class Vector2d {
public:
double x, y;
Vector2d(double vx = 0, double vy = 0);
Vector2d(const Vector2d &v);
double& operator[](int i);
const double& operator[](int i) const;
operator Vector3d();
operator Vector4d();
operator Vector();
void print() const;
void print(int w, int p) const; // print with width and precision
double norm() const; // magnitude of vector
double normsqr() const; // magnitude squared
Vector2d normalize() const; // normalize
void set(double vx = 0, double vy = 0); // set assuming y = 0
void set(const Vector2d &v);
/* Vector2d operator prototypes */
friend Vector2d operator-(const Vector2d& v1); // unary negation of vector
friend Vector2d operator+(const Vector2d& v1, const Vector2d& v2);//addition
friend Vector2d operator-(const Vector2d& v1, const Vector2d& v2);//subtract
friend Vector2d operator*(const Vector2d& v, double s); // scalar mult
friend Vector2d operator*(double s, const Vector2d& v);
friend double operator*(const Vector2d& v1, const Vector2d& v2); // dot
friend Vector2d operator^(const Vector2d& v1, const Vector2d& v2); //compt *
friend Vector3d operator%(const Vector2d& v1, const Vector2d& v2); // cross
friend Vector2d operator/(const Vector2d& v, double s); // division by scalar
friend short operator==(const Vector2d& one, const Vector2d& two); // eq
friend ostream& operator<< (ostream& os, const Vector2d& v);
};
class Vector3d {
public:
double x, y, z;
Vector3d(double vx = 0, double vy = 0, double vz = 0);
Vector3d(const Vector3d &v);
double& operator[](int i);
const double& operator[](int i) const;
operator Vector4d();
operator Vector();
void print() const;
void print(int w, int p) const; // print with width and precision
double norm() const; // magnitude of vector
double normsqr() const; // magnitude squared
Vector3d normalize() const; // normalize
void set(double vx = 0, double vy = 0, double vz = 0); // set
void set(const Vector3d &v);
/* Vector3d operator prototypes */
friend Vector3d operator-(const Vector3d& v1); // unary negation
Vector3d operator+(const Vector3d& v2) const; // vector addition
friend Vector3d operator-(const Vector3d& v1, const Vector3d& v2); // subtract
friend Vector3d operator*(const Vector3d& v, double s); // multiply
friend Vector3d operator*(double s, const Vector3d& v);
friend double operator*(const Vector3d& v1, const Vector3d& v2); // dot
friend Vector3d operator^(const Vector3d& v1, const Vector3d& v2); // compt *
friend Vector3d operator%(const Vector3d& v1, const Vector3d& v2); // cross
friend Vector3d operator/(const Vector3d& v, double s); // division by scalar
friend short operator==(const Vector3d& one, const Vector3d& two); // equ
friend ostream& operator<< (ostream& os, const Vector3d& v);
};
class Vector4d {
public:
double x, y, z, w;
Vector4d(double vx = 0, double vy = 0, double vz = 0, double vw = 0);
Vector4d(const Vector4d &v);
double& operator[](int i);
const double& operator[](int i) const;
operator Vector();
void print() const;
void print(int w, int p) const; // print with width and precision
double norm() const; // magnitude of vector
double normsqr() const; // magnitude squared
Vector4d normalize() const; // normalize
Vector4d wnorm() const; // normalize to w coord.
void set(double vx = 0, double vy = 0, double vz = 0, double vw = 0); // set
void set(const Vector4d &v);
/* Vector4d operator prototypes */
friend Vector4d operator-(const Vector4d& v1); // unary negation
Vector4d operator+(const Vector4d& v2) const; // vector addition
friend Vector4d operator-(const Vector4d& v1, const Vector4d& v2); //subtract
friend Vector4d operator*(const Vector4d& v, double s); // multiply
friend Vector4d operator*(double s, const Vector4d& v);
friend double operator*(const Vector4d& v1, const Vector4d& v2); // dot
friend Vector4d operator^(const Vector4d& v1, const Vector4d& v2); // compt *
friend Vector4d operator%(const Vector4d& v1, const Vector4d& v2); // cross
friend Vector4d operator/(const Vector4d& v, double s); // divide by scalar
friend short operator==(const Vector4d& one, const Vector4d& two); // equ
friend ostream& operator<< (ostream& os, const Vector4d& v);
};
class Vector {
protected:
int N;
double *v;
public:
Vector(int vN = 0, double *vx = NULL);
Vector(const Vector& V);
Vector(double vx, double vy);
Vector(double vx, double vy, double vz);
Vector(double vx, double vy, double vz, double vw);
~Vector();
void setsize(int vN);
double& operator[](int i);
const double& operator[](int i) const;
operator Vector2d();
operator Vector3d();
operator Vector4d();
int getn() const;
void print() const;
void print(int w, int p) const; // print with width and precision
double norm() const; // magnitude of vector
double normsqr() const; // magnitude squared
Vector normalize() const; // normalize
void set(double *vx); // set
void set(const Vector &v);
void set(double vx, double vy);
void set(double vx, double vy, double vz);
void set(double vx, double vy, double vz, double vw);
/* Vector operator prototypes */
const Vector& operator=(const Vector& v2); // assignment
friend Vector operator-(const Vector& v1); // unary negation
friend Vector operator+(const Vector& v1, const Vector& v2); // vector add
friend Vector operator-(const Vector& v1, const Vector& v2); // vector sub
friend Vector operator*(const Vector& v, double s); // scalar multiply
friend Vector operator*(double s, const Vector& v);
friend Vector operator^(const Vector& v1, const Vector& v2); // component *
friend double operator*(const Vector& v1, const Vector& v2); // dot product
friend Vector operator%(const Vector& v1, const Vector& v2); // cross product
friend Vector operator/(const Vector& v, double s); // division by scalar
friend short operator==(const Vector& one, const Vector& two); // equality
friend ostream& operator<< (ostream& os, const Vector& v);
};
#endif