Skip to content

Commit 3ee19c8

Browse files
committed
Add double math class.
1 parent ea24e8f commit 3ee19c8

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

KdTreeLib/KdTreeLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ItemGroup>
4242
<Compile Include="HyperRect.cs" />
4343
<Compile Include="IPriorityQueue.cs" />
44+
<Compile Include="Math\DoubleMath.cs" />
4445
<Compile Include="Math\FloatMath.cs" />
4546
<Compile Include="Math\GeoMath.cs" />
4647
<Compile Include="Math\GeoUtils.cs" />

KdTreeLib/Math/DoubleMath.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace KdTree.Math
8+
{
9+
[Serializable]
10+
public class DoubleMath : TypeMath<double>
11+
{
12+
public override int Compare(double a, double b)
13+
{
14+
return a.CompareTo(b);
15+
}
16+
17+
public override bool AreEqual(double a, double b)
18+
{
19+
return a == b;
20+
}
21+
22+
public override double MinValue
23+
{
24+
get { return double.MinValue; }
25+
}
26+
27+
public override double MaxValue
28+
{
29+
get { return double.MaxValue; }
30+
}
31+
32+
public override double Zero
33+
{
34+
get { return 0; }
35+
}
36+
37+
public override double NegativeInfinity { get { return double.NegativeInfinity; } }
38+
39+
public override double PositiveInfinity { get { return double.PositiveInfinity; } }
40+
41+
public override double Add(double a, double b)
42+
{
43+
return a + b;
44+
}
45+
46+
public override double Subtract(double a, double b)
47+
{
48+
return a - b;
49+
}
50+
51+
public override double Multiply(double a, double b)
52+
{
53+
return a * b;
54+
}
55+
56+
public override double DistanceSquaredBetweenPoints(double[] a, double[] b)
57+
{
58+
double distance = Zero;
59+
int dimensions = a.Length;
60+
61+
// Return the absolute distance bewteen 2 hyper points
62+
for (var dimension = 0; dimension < dimensions; dimension++)
63+
{
64+
double distOnThisAxis = Subtract(a[dimension], b[dimension]);
65+
double distOnThisAxisSquared = Multiply(distOnThisAxis, distOnThisAxis);
66+
67+
distance = Add(distance, distOnThisAxisSquared);
68+
}
69+
70+
return distance;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)