Skip to content

Commit 300ee55

Browse files
author
NetGnome
committed
vectors should be minimally functional...
1 parent 27d8cb3 commit 300ee55

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

BehaviorLibrary/Components/Utility/UtilityVector.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,51 @@ namespace BehaviorLibrary.Components.Utility
77
{
88
public class UtilityVector
99
{
10-
public UtilityVector(params float[] values){}
11-
}
12-
}
10+
public UtilityVector(params float[] values){
11+
this.values = values;
12+
}
13+
14+
public float[] values{ get; set; }
15+
16+
public float magnitude{
17+
get{
18+
float mag = 0;
19+
for (int i = 0; i < this.values.Length; i++)
20+
mag += this.values [i] * this.values [i];
21+
22+
return (float) Math.Sqrt (mag);
23+
}
24+
}
25+
26+
public UtilityVector normalize(){
27+
if (this.values.Length <= 0)
28+
return null;
29+
30+
UtilityVector vec = new UtilityVector();
31+
vec.values = new float[this.values.Length];
32+
this.values.CopyTo (vec.values, 0);
33+
34+
float mag = vec.magnitude;
35+
36+
for (int i = 0; i < vec.values.Length; i++)
37+
vec.values [i] = vec.values [i] / mag;
38+
39+
return vec;
40+
}
41+
42+
public float dot(UtilityVector vector){
43+
if (this.magnitude == 0 || vector.magnitude == 0)
44+
return -2;
45+
46+
UtilityVector a = this.normalize ();
47+
UtilityVector b = vector.normalize ();
48+
49+
float val = 0;
50+
51+
for (int i = 0; i < this.values.Length; i++)
52+
val += a.values [i] * b.values [i];
53+
54+
return val;
55+
}
56+
}
57+
}

Tests/UtilityTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,29 @@ public void teardown()
6060
_log.closeLog();
6161
}
6262

63+
[Test]
64+
public void test_vector(){
65+
_log.enterScope ();
66+
67+
UtilityVector vec1 = new UtilityVector (0, 1, 2, 3, 4, 5);
68+
69+
float mag = vec1.magnitude;
70+
_log.logDebug ("mag: " + mag);
71+
VerificationPoint.VerifyTrue ("verify mag gte 0", true, mag >= 0);
72+
73+
VerificationPoint.VerifyTrue ("norm is not null", true, vec1.normalize () != null);
74+
75+
UtilityVector vec2 = new UtilityVector (5, 4, 3, 2, 1, 0);
76+
77+
float dot = vec1.dot (vec2);
78+
_log.logDebug ("dot: " + dot);
79+
VerificationPoint.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1));
80+
81+
_log.exitScope ();
82+
}
83+
6384
[Test]
64-
public void test_case_1()
85+
public void test_1()
6586
{
6687
_log.enterScope();
6788

0 commit comments

Comments
 (0)