-
Notifications
You must be signed in to change notification settings - Fork 1
/
vektorr2.h
150 lines (117 loc) · 3.9 KB
/
vektorr2.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
#ifndef _VektorR2_h
#define _VektorR2_h
#include <iostream>
#include <math.h>
// #include <stdlib.h>
// #include <fstream>
// #include <iomanip>
using namespace std;
class VektorR2
{
private:
double c[2]; // Komponenten des zweidim. Vektors
public:
VektorR2() {}
VektorR2(double x, double y) {c[0] = x; c[1] = y;}
VektorR2(const VektorR2& Vektor) {c[0] = Vektor.c[0]; c[1] = Vektor.c[1];}
double& operator [] (const int);
double operator [] (const int) const;
const bool operator == (const VektorR2&) const;
VektorR2 operator + (const VektorR2&) const;
VektorR2& operator += (const VektorR2&);
VektorR2 operator - (const VektorR2&) const;
VektorR2& operator -= (const VektorR2&);
double operator * (const VektorR2&) const;
friend VektorR2 operator * (const double, const VektorR2&);
VektorR2 operator * (const double);
VektorR2& operator *= (const double);
VektorR2 operator / (const double) const;
VektorR2& operator /= (const double);
static const VektorR2 CrossProduct(VektorR2&, VektorR2&);
void print() const {cout << c[0] << "," << c[1];};
double Norm2() const;
double Normalize();
VektorR2 Rotate(const double) const;
}; // class VektorR2
// Inline-Elementfunktionen von VektorR2
inline double& VektorR2::operator [] (const int c_index)
{
return(c[c_index]);
} // VektorR2::operator []
inline double VektorR2::operator [] (const int c_index) const
{
return(c[c_index]);
} // VektorR2::operator []
inline const bool VektorR2::operator == (const VektorR2& Vektor) const
{
return((c[0] == Vektor.c[0]) && (c[1] == Vektor.c[1]));
} // VektorR2::operator ==
inline VektorR2 VektorR2::operator + (const VektorR2& Vektor) const
{
return VektorR2(c[0] + Vektor.c[0], c[1] + Vektor.c[1]);
} // VektorR2::operator+
inline VektorR2& VektorR2::operator += (const VektorR2& Vektor)
{
c[0] += Vektor.c[0];
c[1] += Vektor.c[1];
return *this;
} // VektorR2::operator +=
inline VektorR2 VektorR2::operator - (const VektorR2& Vektor) const
{
return VektorR2(c[0] - Vektor.c[0], c[1] - Vektor.c[1]);
} // VektorR2::operator -
inline VektorR2& VektorR2::operator -= (const VektorR2& Vektor)
{
c[0] -= Vektor.c[0];
c[1] -= Vektor.c[1];
return *this;
} // VektorR2::operator -=
inline double VektorR2::operator * (const VektorR2& Vektor) const
{
return (c[0]*Vektor.c[0] + c[1]*Vektor.c[1]);
} // VektorR2::operator *
inline VektorR2 operator * (const double s, const VektorR2& Vektor)
{
return VektorR2(s*Vektor.c[0], s*Vektor.c[1]);
} // friend operator * (const double, const VektorR2&);
inline VektorR2 VektorR2::operator * (const double s)
{
return VektorR2(s*c[0], s*c[1]);
} // VektorR2::operator * (const double, const VektorR2&);
inline VektorR2& VektorR2::operator *= (const double s)
{
c[0] *= s;
c[1] *= s;
return *this;
} // VektorR2::opertor *= (const double&)
inline VektorR2 VektorR2::operator / (const double s) const
{
return VektorR2(c[0]/s, c[1]/s);
} // VektorR2::operator /
inline VektorR2& VektorR2::operator /= (const double s)
{
c[0] /= s;
c[1] /= s;
return *this;
} // VektorR2::operator /=
inline double VektorR2::Norm2() const
{
return ((double)(sqrt((double)(c[0]*c[0] + c[1]*c[1]))));
} // VektorR2::Norm2
inline double VektorR2::Normalize() {
double length = Norm2();
if(length > 0.0) *this /= length;
return length;
} // VektorR2::Normalize
inline VektorR2 VektorR2::Rotate(const double angle) const {
double current_angle;
double length;
VektorR2 v = *this;
length = v.Norm2();
v /= length;
current_angle = acos((double)v[0]);
if(v[1] < 0.0) current_angle = 2.0*M_PI - current_angle;
current_angle += (double)angle;
return length*VektorR2((double)cos(current_angle), (double)sin(current_angle));
} // VektorR2::Rotate
#endif