-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cs
More file actions
142 lines (121 loc) · 3.98 KB
/
Vector.cs
File metadata and controls
142 lines (121 loc) · 3.98 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharedTools
{
public struct Vector
{
private float[] _values;
public int Dimension
{
get { return _values.Length; }
}
private static int ThrowIfIncompatible(Vector a, Vector b)
{
int dimension = a._values.Length;
if (dimension != b._values.Length) throw new InvalidOperationException();
return dimension;
}
public Vector(params float[] values)
{
if (values == null) throw new ArgumentNullException("values");
if (values.Length == 0) throw new ArgumentException("values");
_values = values;
}
public float this[int index]
{
get { return _values[index]; }
set { _values[index] = value; }
}
public static Vector operator +(Vector a, Vector b)
{
int anz = ThrowIfIncompatible(a, b);
for (int x = 0; x < anz; ++x) a._values[x] += b._values[x];
return a;
}
public static Vector operator -(Vector a, Vector b)
{
int anz = ThrowIfIncompatible(a, b);
for (int x = 0; x < anz; ++x) a._values[x] -= b._values[x];
return a;
}
public static float operator *(Vector a, Vector b)
{
int anz = ThrowIfIncompatible(a, b);
float fertig = 0F;
for (int x = 0; x < anz; ++x) fertig += a._values[x] * b._values[x];
return fertig;
}
public static Vector operator *(float a, Vector b)
{
int anz = b._values.Length;
for (int x = 0; x < anz; ++x) b._values[x] *= a;
return b;
}
public static Vector operator /(Vector a, float b)
{
int anz = a._values.Length;
for (int x = 0; x < anz; ++x) a._values[x] /= b;
return a;
}
public float Betrag2()
{
int anz = _values.Length;
float fertig = 0F;
float aktuellerWert;
for (int x = 0; x < anz; ++x)
{
aktuellerWert = _values[x];
fertig += aktuellerWert * aktuellerWert;
}
return fertig;
}
public float Betrag()
{
return (float)Math.Sqrt(Betrag2());
}
public static bool IstKollinear(Vector a, Vector b)
{
int anz = ThrowIfIncompatible(a, b);
float quotient = b._values[0] / a._values[0];
for (int x = 1; x < anz; ++x)
{
if (b._values[x] / a._values[x] != quotient) return false;
}
return true;
}
public static float Schnittwinkel(Vector a, Vector b)
{
return (float)(Math.Acos(Math.Abs(a * b) / (a.Betrag() * b.Betrag())) / Math.PI * 180D);
}
public override string ToString()
{
int anz = _values.Length;
StringBuilder fertig = new StringBuilder(anz * 2);
fertig.Append(_values[0]);
for (int x = 1; x < anz; ++x) fertig.Append(", ").Append(_values[x]);
return fertig.ToString();
}
public static implicit operator Vector(float[] feld)
{
return new Vector(feld);
}
public static implicit operator float[] (Vector v)
{
return v._values;
}
public static explicit operator Vector2D(Vector v)
{
float[] vals = v._values;
if (vals.Length != 2) throw new InvalidCastException();
return new Vector2D(vals[0], vals[1]);
}
public static explicit operator Vector3D(Vector v)
{
float[] vals = v._values;
if (vals.Length != 3) throw new InvalidCastException();
return new Vector3D(vals[0], vals[1], vals[2]);
}
}
}